Diagonal Sum of Array with random inputs
//Diagonal Sum of Array with random inputs.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
void main()
{
int A[4][4], sum=0,i,j;
clrscr();
//Taking Input for A
randomize();
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=random(100);
if(i==j)
sum+=A[i][j];
}
}
//Pinting the values of A array.
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<setw(5)<<A[i][j];
}
cout<<endl;
}
cout<<"\nThe sum of left diagonals of Array is : "<<sum<<endl;
getch();
}
/*
Output
Enter the value for A[1][0]: 4
Enter the value for A[1][1]: 5
Enter the value for A[1][2]: 6
Enter the value for A[2][0]: 7
Enter the value for A[2][1]: 8
Enter the value for A[2][2]: 9
Enter the value for B[0][0]: 11
Enter the value for B[0][1]: 12
Enter the value for B[0][2]: 13
Enter the value for B[1][0]: 14
Enter the value for B[1][1]: 15
Enter the value for B[1][2]: 16
Enter the value for B[2][0]: 17
Enter the value for B[2][1]: 18
Enter the value for B[2][2]: 19
1 2 3 11 12 13 12 14 16
4 5 6 14 15 16 18 20 22
7 8 9 17 18 19 24 26 28
*/
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
void main()
{
int A[4][4], sum=0,i,j;
clrscr();
//Taking Input for A
randomize();
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=random(100);
if(i==j)
sum+=A[i][j];
}
}
//Pinting the values of A array.
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<setw(5)<<A[i][j];
}
cout<<endl;
}
cout<<"\nThe sum of left diagonals of Array is : "<<sum<<endl;
getch();
}
/*
Output
Enter the value for A[1][0]: 4
Enter the value for A[1][1]: 5
Enter the value for A[1][2]: 6
Enter the value for A[2][0]: 7
Enter the value for A[2][1]: 8
Enter the value for A[2][2]: 9
Enter the value for B[0][0]: 11
Enter the value for B[0][1]: 12
Enter the value for B[0][2]: 13
Enter the value for B[1][0]: 14
Enter the value for B[1][1]: 15
Enter the value for B[1][2]: 16
Enter the value for B[2][0]: 17
Enter the value for B[2][1]: 18
Enter the value for B[2][2]: 19
1 2 3 11 12 13 12 14 16
4 5 6 14 15 16 18 20 22
7 8 9 17 18 19 24 26 28
*/
No comments
Post your comments