Search code examples
c++arraysbubble-sort

I'm having a couple of errors on my bubble sorting program which displays the maximum, minimun and average (C++)


I'm doing a programing code for a homework, you are supposed to enter student's height to be sorted, printing the maximum, minimum the sorting result and the average, but I'm having a couple of errors, I managed to get the sorting and maximum done, but the minimum is wrong and I have to obtain the average on an array but I haven't figured out how to make the program read the values typed by the user. I would be very thankful if someone managed to help me.

#include <iostream>
#include <conio.h>
#include <stdio.h> 
#include <stdlib.h>

    float average (float ave1, float ave2, float ave3, float ave4, float ave5, float ave6, float ave7, float ave8, float ave9, float ave10)
    {
        float result = 0;
        result = ((ave1 + ave2+ ave3 + ave4 + ave5 + ave6 + ave7 + ave8 + ave9 + ave10) /10);
        result (result);
    }
//This is my failed attempt to code the average array
int main() {
    float height[10];
    float max = 0;
    float min = 0;

    int i, j;

    for (i = 0; i < 10; i++)
    {
        cout << "What's student " << i + 1 <<" height?" << endl;
        cin >> height [i];
    }

    max = min = height[0];

    for (i = 0; i < 10; i++) //Bubble Sort
    {
        for(j = 0; j < 9; j++)
        if (height[j] > height [j+1])
        {float temp;

                temp = height [j];
                height [j] = height [j+1];
                height [j+1] = temp;
            if (height [i] < min)
            {
                min = height [i];
            }
            if (height [i] > max)
            {
                max = height [i];
            }
    }
    }
    height [i] = ave1 = ave2 = ave3 = ave4 = ave5 = ave6 = ave7 = ave8 = ave9 = ave10 ;

    cout << "The tallest student's height is: " << max << " feet" << endl;
    cout << "The shortest student's height is: " << min << " feet" << endl;
    cout << "Sorted heights are: ";
    for (i = 0; i < 10; i++)
    {
        cout << height [i] << "; ";
    }
    cout << "Height average: " << endl;
    cout << average;

    return 0;
}

Solution

  • As a result of sorting a list of numbers in ascending order the min value Is the first item of the list and the hight value Is the last item of the list, so min=height[0] and max=height [9] after sortng.

    1. In your code remove this lines:

        if (height [i] < min)
          {
              min = height [i];
          }
          if (height [i] > max)
          {
              max = height [i];
          }
      
    2. after line:

      float min=0;
      

      Add:

      float acum=0;
      
    3. Replace this line:

      height [i] = ave1 = ave2 = ave3 = ave4 = ave5 = ave6 = ave7 = ave8 = ave9 = ave10 ;
      

      By :

       for(i=0;i<10;i++)
           acum+=height[i];
      
       min=height[0];
       max=height[9];
      
    4. AND finally Replace;

       cout << average;
      

      By

       cout<< acum/10;