用c++编写求二维数组中每行元素的平均值的函数。调用该函数求array54每行元素的平均值。要求:1数据输入应该在主函数中完成。 2求平均值的函数可以设计成求5行4列的数组的每行平均值。
#include <iostream>
using namespace std;
void rowAvg(int arr[][4], int rows) {
for (int i = 0; i < rows; i++) {
double sum = 0;
for (int j = 0; j < 4; j++) {
sum += arr[i][j];
}
double avg = sum / 4;
cout << "Row " << i+1 << " avg: " << avg << endl;
}
}
int main() {
int array[5][4];
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
cin >> array[i][j];
}
}
rowAvg(array, 5);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/bWbl 著作权归作者所有。请勿转载和采集!