0

I would like to sort an array of floats using bubble sort in C. I would like this function to return this sorted array, and be assigned to my variable testSorted. However, when I print testSorted the output is 0.0000. Can anyone please help out here?

    float * bubbleSort(float statsArray[], int n){
    
            float swap;
    
            for ( int a = 0;  a < n-1; a++)
            {
                for (int b = 0; b < n - a - 1; b++)
                {
                    if (statsArray[b] < statsArray[b+1])
                    {
                        swap  = statsArray[b];
                        statsArray[b] = statsArray[b+1];
                        statsArray[b+1] = swap;
                    }
    
                }
            }
    
            return statsArray;
    }
    
    void main() {
    
      float test[SIZE] = { 34, 201, 190, 154,   8, 194,   2,   6,
                           114, 88,   45,  76, 123,  87,  25,  23,
                           200, 122, 150, 90,   92,  87, 177, 244,
                           201,   6,  12,  60,   8,   2,   5,  67,
                           7,  87, 250, 230,  99,   3, 100,  90};
    
      /*  Variable Declarations */
    
      float average;
      float * testSorted;
      int p;
      float maximum, minimum;
    
      p = 40;
    
      testSorted =  bubbleSort(test, p);
      printf(testSorted);

      return 0;
}
2
  • 2
    You cannot simply printf(float_array). You should consider writing a loop to print the elements individually with %f placeholder in the format Commented Jan 10, 2021 at 17:29
  • What else did you expect it to print? Commented Jan 10, 2021 at 17:29

1 Answer 1

1

printf(testSorted); is quite broken, because the first argument of printf is supposed to be a pointer to a char * format string, not an array of floats.

Instead, to print an array, you need to do something like the following:

printf("{ ");
for (int a = 0; a < SIZE; a++) {
  printf("%f ", test[a]);
}
printf("}");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.