C/C++ 课题解答(2)

二维整型数组10*10,计算每行、每列的平均值

#include <iostream>
using namespace std;
#include <time.h>
int main()
{
	int arrayOfInt[10][10];
	srand(time(NULL));
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			arrayOfInt[i][j] = rand() % 100 + 1;
			cout.setf(ios::left);
			cout.width(4);
			cout << arrayOfInt[i][j] << " ";
		}
		cout << endl;
	}
	cout << endl;

	int total = 0;
	float averageOfRow[10];
	for (int i = 0; i < 10; i++)
	{
		total = 0;
		for (int j = 0; j < 10; j++)
		{
			total += arrayOfInt[i][j];
		}
		averageOfRow[i] = (float)total / 10;
		cout << "第[" << i + 1 << "]行平均值:" << averageOfRow[i] << endl;
	}
	
	float averageOfColumn[10];
	for (int i = 0; i < 10; i++)
	{
		total = 0;
		for (int j = 0; j < 10; j++)
		{
			total += arrayOfInt[j][i];
		}
		averageOfColumn[i] = (float)total / 10;
		cout << "第[" << i + 1 << "]列平均值:" << averageOfColumn[i] << endl;
	}

	system("pause");
	return 1;
}

猜你喜欢

转载自blog.csdn.net/u012156872/article/details/115109698