c++は3 x 3行列の対角線の和を求めます
957 ワード
#include <iostream>
#include <time.h>
using namespace std;
int z[3][3];
int i,j;
int r=0;
int l=0;
void input();
void main() {
cout<<"this code from zxl"<<endl;
srand((unsigned)time(NULL));// c++
input();
for( i=0; i<=2; i++) {
for( j=0; j<=2; j++) {
if(i==j)
r = r+z[i][j]; //
if(i+j==2)
l= l+z[i][j]; //
}
}
for( i=0; i<=2; i++) {
for( j=0; j<=2; j++) {
cout<<z[i][j]<<" ";
} cout<<endl;
}
cout<<r<<" "<<l<<endl;
}
void input(){
for(int i=0; i<=2; i++) {
for(int j=0; j<=2; j++) {
z[i][j]=rand()%10;
}
}
}