获取 2D 数组中数字的平均、最小值和最大值
Getting the Average,Min, and Max of numbers in 2D array
我在获取 2D 数组元素的最小值、最大值和平均值时遇到问题。
我有一个包含学生和成绩的 2D 数组。
我正在用兰特生成成绩。例如,当我输入 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;
}
}
我做错了什么?另外,我如何获得每个数组的最小值,最大值?
我强烈建议使用容器来完成此任务。例如,您可以执行以下操作
typedef std::vector<float> grades;
std::vector<grades> student_grades;
//populate
for(const grades& gr : student_grades) {
float min, max, avg;
std::tie(min, max)=std::minmax(gr.begin(), gr.end());
avg=std::accumulate(gr.begin(), gr.end(), 0.0) / gr.size());
std::cout << "Min" << min << " Max: " << max << " Avg: " << avg << std::endl;
}
http://en.cppreference.com/w/cpp/algorithm/minmax
http://en.cppreference.com/w/cpp/algorithm/accumulate
http://en.cppreference.com/w/cpp/utility/tuple/tie
对于初学者来说,你从循环内部返回:
for (int i = 0 ; i < noOfGrades ; i++) {
for (int j = 0; j<noOfStudents; j++) {
...
}
...
return ...;
}
你能看到外部循环如何只执行一次吗?
问题是在你的findAverage
函数中,你在循环行的循环中有一个 return 语句。如果您想要一个非常简单的修复,请在 findAverage
函数中添加另一个参数,以指示要计算哪一行的平均值。
int findAverage(int course ,int noOfGrades ){
float sum,average;
for (int j = 0; j<noOfStudents; j++) {
sum += A[course][j];
}
average = sum / noOfGrades;
return format(average);
}
并像这样称呼它
cout <<" "<<findAverage(i,noOfCourses);
相关文章:
- 查找矩阵C++中每一列和每一行的最小和最大元素
- 如何在数组中交换最小和最大的位置?
- 需要使用模板查找数组的第二个最小和最小值
- 使用 std::min_element、std::max_element 查找矢量中的最小和最大元素
- 为什么我不能使用最小和最大这两个词作为变量名称?
- 在运行时为随机分布类成员设置最小和最大边界?
- 如何使用气泡排序从最小到最大对 4 个数组进行排序? C++
- 如何拒绝 cin 中的字符输入并定义最小和最大整数值
- 类型 *阵列的最小或最大值的类功能 *
- C++ 100 万个变量名称的最小和最大变量名称长度是多少
- 想要以正弦曲线的形式在循环中连续将值从最小变为最大
- 数组内存地址总是按最小到最大的顺序排列吗
- 使用向量而不是数组从最小到最大对数字进行排序C++
- 在 c++ 中给出两个整数向量(大小和类型相同),我想从最小到最大元素对一个进行排序,并更改第二个向量的顺序
- 在用户定义结构的向量中查找最小和最大元素的索引
- 二进制搜索,用于查找排序数组中比给定值最小和最大的元素
- c++中的函数如何以最小的复制返回值或引用?
- 我如何确定我可以传递给编译器选项的最小和最大值
- 如何为每个光栅带设置最小和最大颜色值(使用GDAL)
- 在c++中最小化分支-如果值是非零则递增