求二维数组中全部元素之和。

来源:百度知道 编辑:UC知道 时间:2024/06/22 17:15:28
是一个程序哦 完整的 谢谢了 大虾们…

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void main()
{
srand((unsigned int)time(NULL));
int a[5][5],sum=0;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
a[i][j]=rand()%20;
sum+=a[i][j];
}
cout<<"随机生成的二维数组为:"<<endl;
for(i=0;i<5;i++)
{
for(int j=0;j<5;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<"总和为:"<<endl;
cout<<sum<<endl;

}

不知道你要使用什么语言,要求二维数组中全部元素之和
可以依次遍历所有的数组元素,使用一个统计器进行累计就行了
如:ASP的
Dim MyArray(1,1),MyItem_I,MyItem_J,MyCount
MyArray(0,0)=1
MyArray(0,1)=2
MyArray(1,0)=3
MyArray(1,1)=4
For MyItem_I=0 To UBound(MyArray,2)
For MyItem_J=0 To UBound(MyArray,1)
MyCount=MyCount+MyArray(MyItem_I,MyItem_J)
Next
Next
Respo