我的气泡排序程序出现几个错误,显示最大,最小和平均值(C ++)

问题描述 投票:0回答:1

我正在为家庭作业编写程序代码,应该输入学生的身高进行排序,打印最大,最小的排序结果和平均值,但是我遇到了几个错误,但是我设法得到排序和最大值完成,但是最小值是错误的,我必须获取数组的平均值,但是我还没有弄清楚如何使程序读取用户键入的值。如果有人设法帮助我,我将非常感激。

#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;
}
c++ arrays bubble-sort
1个回答
0
投票

作为对数字列表进行升序排序的结果,最小值是列表的第一项,而最大值是列表的最后一项,因此min = height [0]和max = height [9]排序后。

© www.soinside.com 2019 - 2024. All rights reserved.