/*题目:编写函数addto,求一个10*10的二维整型数组中所
有偶数的和,并返回结果。 输出结果为2550 */
#include <iostream>
#include <fstream>
using namespace std;
int addto(int b[][10], int len) //疑问一:(int b[][10],int len)表示的是什么意思?
{
int sum=0;
/**********Program**********/ //疑问二:一下大括号内的程序段表示意思
for(int i=0 ; i<10 ; i++)
for(int j=0 ; j<10 ; j++)
if(b[j]%2==0)
sum += b[j] ;
/********** End **********/
return sum;
}
int grading();
int main()
{
int result;
int a[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, //疑问三: 此处 int a[10][10]= 这些数字的含义
{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36, 37, 38, 39, 40},
{41, 42, 43, 44, 45, 46, 47, 48, 49, 50},
{51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
{61, 62, 63, 64, 65, 66, 67, 68, 69, 70},
{71, 72, 73, 74, 75, 76, 77, 78, 79, 80},
{81, 82, 83, 84, 85, 86, 87, 88, 89, 90},
{91, 92, 93, 94, 95, 96, 97, 98, 99, 100}};
result = addto(a,10);
cout << "给定的二维整型数组中所有偶数的和是:" ;
cout << result << endl;
grading();
return 0;
}
int grading()
{
int a[10][10], result;
fstream ifile, myfile;
ifile.open ("in.txt",ios::in);
myfile.open("out.txt",ios::out);
for (int k=0;k<4;k++)
{
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
ifile>>a[j];
result = addto(a,10);
myfile<<result<<endl;
}
ifile.close();
myfile.close();
return 0; |