问题及代码:
/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:夏焕哲
* 完成日期:2014 年 11 月 20 日
* 版 本 号:v1.0
*
* 问题描述:折腾二维数组。
* 输入描述: 建立一个二维数组,先赋值前十个,在敲入剩下的10个。
* 程序输出: 如图所示。
*/
#include <iostream>
using namespace std;
int main()
{
int i,j,m;
int a[5][4]={{0,1},{4,5},{8,9},{12,13},{16,17}};
cout<<"请输入十个整数:"<<endl;
for(i=0;i<=4;i++)
{
for(j=2;j<=3;j++)
{
cin>>a[i][j];
}
}
cout<<"数组中的值为:"<<endl;
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"将所有元素值乘以3:"<<endl;
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
a[i][j]=a[i][j]*3;
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"行序优先输出:"<<endl;
for(j=0;j<=3;j++)
{
for(i=0;i<=4;i++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"输出所有偶数:"<<endl;
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
m=a[i][j];
if(m%2==0)
cout<<"a["<<i<<"]"<<"["<<j<<"]"<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"角标和为3的倍数的为:"<<endl;
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
if((i+j)%3==0)
cout<<"a["<<i<<"]"<<"["<<j<<"]"<<a[i][j]<<" "<<endl;;
}
cout<<endl;
}
return 0;
}
运行结果: