3 回答
snowmanJS
TA贡献89条经验 获得超53个赞
//此程序可以计算所有维度的二维数组外侧元素的和。
//编译时请使用g++编译器,并勾选C++11 选项。
#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"请输入数组的行数:";
cin>>row;
cout<<"请输入数组的列数:";
cin>>col;
cout<<endl;
const int ROW=row;
const int COL=col;
int a[ROW][COL]={0};
int i=0;
//使用范围for语句为数组赋值
for(auto &row : a)
{
++i;
cout<<"请在第"<<i<<"行输入"<<COL<<"个整数:"<<endl;
for(auto &col: row)
cin>>col;
}
cout<<endl<<"输入的数组为"<<endl;
//使用范围for语句输出数组
for(auto &row : a)
{
for(auto col: row)
cout<<col<<" ";
cout<<endl;
}
int sum=0;
for(int j=0;j!=ROW;++j)
for(int k=0;k!=COL;++k)
{
if(j==0 || j==ROW-1 || k==0 || k==COL-1)
sum+=a[j][k];
}
cout<<endl<<"数组外层元素和为:"<<sum<<endl;
return 0;
}
夜色催耕
TA贡献7条经验 获得超7个赞
#include <stdio.h>
main()
{int a[3][4],i,j,s=0;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
{scanf("%d",&a[i][j]);
if(i==0||i==2||j==0||j==3)
s+=a[i][j];
}
printf("s=%d\n",s);
}
- 3 回答
- 0 关注
- 2280 浏览
添加回答
举报
0/150
提交
取消
