C++ 计算二维阵列的平均值、最小值和最大值
我应该编写一个程序,读取并填充一个包含5个学生ID及其分数的数组,然后用学生ID打印平均、最小和最大分数,例如: 学生1:111 56 学生2:22298 学生3:33390 学生4:44468 学生5:55588 平均:80 排名#1:222 98 排名#5:111 56 我的程序给了我一个混乱的值(平均值是328.4,秩1:444555,秩5:55588),还有一个调试错误(运行时检查失败#2-围绕变量“a”的堆栈是corrupter),这里是C++ 计算二维阵列的平均值、最小值和最大值,c++,arrays,function,2d,C++,Arrays,Function,2d,我应该编写一个程序,读取并填充一个包含5个学生ID及其分数的数组,然后用学生ID打印平均、最小和最大分数,例如: 学生1:111 56 学生2:22298 学生3:33390 学生4:44468 学生5:55588 平均:80 排名#1:222 98 排名#5:111 56 我的程序给了我一个混乱的值(平均值是328.4,秩1:444555,秩5:55588),还有一个调试错误(运行时检查失败#2-围绕变量“a”的堆栈是corrupter),这里是 #include <iostream&g
#include <iostream>
using namespace std;
void avg(double a[4][1]);
void minMax(double a[4][1]);
void main () {
double a[4][1];
cout << "Enter 5 students' IDs and marks:\n";
int studentNum = 1;
for (int r=0; r<5; r++) {
cout << "Student" << studentNum << ": ";
studentNum++;
for (int c=0; c<2; c++)
cin >> a[r][c]; }
avg(a);
minMax(a); }
void avg(double a[4][1]){
double sum=0.0;
for (int r=0; r<5; r++) {
for (int c=1; c<2; c++) // does not include the ID column
sum = sum + a[r][c]; }
double avg = sum/5; // number of students = 5
cout << "Average: " << avg << endl; }
void minMax (double a[4][1]) {
double min = a[0][1];
double max = a[0][1];
int minID = a[0][0];
int maxID = a[0][0];
for (int r=0; r<5; r++) {
for (int c=1; c<2; c++) {
if (a[r][c] < min){
min = a[r][c];
minID = a[r][0]; }
if (a[r][c] > max){
max = a[r][c];
maxID = a[r][0]; } } }
cout << "Rank#1: " << maxID << " " << max << endl;
cout << "Rank#5: " << minID << " " << min << endl; }
#包括
使用名称空间std;
平均无效(双a[4][1]);
void minMax(双a[4][1]);
空干管(){
双a[4][1];
cout错误是:
double a[4][1];
应该是:
double a[5][2];
删除代码会使下面的答案变得无用,不要这样做。