有一个3*3的整型二维数组,写一个函数,当主函数调用此函数后,能求出平均...
发布网友
发布时间:2024-10-04 12:16
我来回答
共1个回答
热心网友
时间:2024-10-04 12:19
#include <iostream>
using namespace std;
void num_calc(int array[3][3], double & aver_num, int & max_num, int & min_num);
int main()
{
int a[3][3] = { 1, 2, 3,
7, 8, 9,
4, 5, 6 };
int max_num, min_num;
double aver_num;
num_calc(a, aver_num, max_num, min_num);
// print the number, you also can use "printf" with library stdio.h included.
cout << "The average number is: " << aver_num << endl;
cout << "The max number is: " << max_num << endl;
cout << "The min number is: " << min_num << endl;
}
void aver_calc(int array[3][3], double & aver_num)
{
int sum = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
sum += array[i][j];
}
aver_num = sum / 9.0;
}
void max_calc(int array[3][3], int & max_num)
{
max_num = array[0][0];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if(max_num < array[i][j])
max_num = array[i][j];
}
}
void min_calc(int array[3][3], int & min_num)
{
min_num = array[0][0];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if(min_num > array[i][j])
min_num = array[i][j];
}
}
void num_calc(int array[3][3], double & aver_num, int & max_num, int & min_num)
{
aver_calc(array, aver_num);
max_calc(array, max_num);
min_calc(array, min_num);
}