题目描述:
从键盘输入一个3X3的整数矩阵,输出该矩阵并求出主对角线元素的和。
源代码:

从键盘输入一个3X3的整数矩阵,输出该矩阵并求出主对角线元素的和。
源代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
// 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;
}

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->








































