我在获取二维数组元素的最小值,最大值和平均值时遇到了问题。获取二维数组中的平均值,最小值和最大值
我有一个包含学生和成绩的二维数组。
我使用rand生成等级。例如当我输入2,2打印出我
Courses : 01 02 Average Min Max
ID
01 8 50 29
02 74 59 29
,我的平均功能取第一个平均值,并且不会取其他平均值。
这是我的代码;
int A[30][30];
int findAverage(int noOfStudents ,int noOfGrades){
float sum,average;
for (int i = 0 ; i < noOfGrades ; i++) {
for (int j = 0; j<noOfStudents; j++) {
sum += A[i][j];
}
average = sum/noOfGrades;
// cout << " " << format(average);
sum = 0;
return format(average);
}
在这里我如何使用它
int main() {
int noOfCourses , noOfStudents;
cin >> noOfCourses >> noOfStudents;
cout << "Courses : " ;
for (int i = 0; i < noOfCourses; i++) {
if (i+1 >= 10) {
cout << i+1 << " ";
}else{
cout <<"0" << i+1 << " ";
}
}
cout << "Average Min Max";
for(int i=0; i<noOfStudents; i++) { //This loops on the rows.
for(int j=0; j<noOfCourses; j++) { //This loops on the columns
A[i][j] = genGrade();
}
}
cout << "\n ID " << endl;
for(int i=0; i<noOfStudents; i++) { //This loops on the rows.
if (i+1 >= 10) {
cout <<" " << i+1 << " ";
}else{
cout <<" 0" << i+1 << " ";
}
//cout <<" 0" << i+1 << " ";
for(int j=0; j<noOfCourses; j++) { //This loops on the columns
if (A[i][j] >= 10 && A[i][j] <=99) {
cout <<" " << A[i][j] << " ";
}
if(A[i][j] < 10) {
cout <<" " << A[i][j] << " ";
}
if (A[i][j] == 100) {
cout << A[i][j] << " ";
}
}
cout <<" "<<findAverage(noOfStudents,noOfCourses);
cout << endl;
}
}
我在做什么错?另外我怎么能得到每阵列的最小值,最大值?
什么是'A'的声明? –
int A [30] [30]; ,对不起:) –
在使用它之前,你还没有初始化'sum',并且在外循环的第一次迭代中返回。有没有理由不使用'std :: vector'来存储数据,'std :: accumulate'来加起来呢?您可以通过'min_element'和'max_element'获得最小值和最大值。 –