C++博客 :: 首页 :: 新随笔 ::  ::  :: 管理

指针操作二维数组

Posted on 2011-05-27 21:44 Kevin_Zhang 阅读(320) 评论(0)  编辑 收藏 引用 所属分类: C/C++
题目描述:
    从键盘输入一个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;
}




只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理