題目描述:
從鍵盤輸入一個3X3的整數矩陣,輸出該矩陣并求出主對角線元素的和。
源代碼:
// test3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
using namespace std;


int main(int argc, char* argv[])
{
int a[3][3],i,j,s=0;
int (* ptr)[3];
ptr=a;
cout<<"請輸入矩陣的值:";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
cin>>*(*(ptr+i)+j);
if(i==j)
s+=*(*(ptr+i)+j);

}
cout<<"輸出矩陣:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<ptr[i][j]<<" ";
cout<<endl;

}
cout<<"矩陣主對角線元素的和為:"<<s<<endl;
return 0;
}


從鍵盤輸入一個3X3的整數矩陣,輸出該矩陣并求出主對角線元素的和。
源代碼:


































