RSS

PROGRAM TO FIND THE SUM OF MAIN DIAGONAL ELEMENTS (USING POINTERS)

21 Nov

//THIS PROGRAM FINDS THE SUM OF MAIN DIAGONAL ELEMENTS IN A MATRIX (USING POINTERS)

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int size;
cout<<“\nENTER THE SIZE OF THE MATRIX \n”;
cin>>size;
int **arr = new int *[size];

for(int i =0; i<size; i++)
{
arr[i] = new int[size];
}

cout<<“\nINPUT THE MATRIX \n”;

for(i =0; i<size; i++)
{
for(int j =0; j<size; j++)
cin>> *(*(arr + i) + j);
}

cout<<“\nFINDING THE SUM OF DIAGONAL ELEMENTS IN THE MATRIX \n”;

int sum = 0;

for(i = 0; i<size; i++)
for(int j = 0; j<size; j++)
if(i == j)
sum += *(*(arr+i) + j);

cout<<“\nTHE SUM OF DIAGONAL ELEMENTS IS \n”<<sum;
getch();
}

Advertisements
 
Leave a comment

Posted by on November 21, 2015 in Matrices, Pointers and Arrays

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: