Problem Statement
Write a C program that reads the order N(≤ 20) of a square matrix over data of type
Write a C program that reads the order N(≤ 20) of a square matrix over data of type
double. Then it reads the matrix in row-major order in a 2-D array (type double).
It prints the input data,
It prints the matrix in diagonal
order,
It prints the matrix in anti-diagonal order, and
It prints the matrix in anti-clockwise spiral order
i.e when the metrics is
1 2 3
4 5 6
7 8 9
then Diagonal
order: 7 4 8 1 5 9 2 6
3
Anti-Diagonal
order: 1 2 4 3 5 7 6 8
9
Anticlockwise-Spiral order: (odd order matrix) 1, 4, 7, 8, 9, 6, 3, 2, 5
and so on
#include<stdio.h>
int main()
{
int i,j,n,count;
printf("enter the value of n \n");
scanf("%d",&n);
double a[n][n];
printf("enter the values rowwise \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%lf",&a[i][j]);
}
}
printf("\nyour value are are read as\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%lf ",a[i][j]);
}
printf("\n");
}
printf("\nnow i print the matrics in diagonal order \n");
i=n-1;
j=0;
while(i!=0||j!=(n-1))
{
count=0;
while(i<n&&j<n)
{
printf("%lf ",a[i][j]);
i++;
j++;
count++;
}
if(j==count&&count==i)
{
i=0;
j=1;
}
else if(j==count&&j!=i)
{
i=i-count-1;
j=0;
}
else if(i==count&&i!=j)
{
j=j-count+1;
i=0;
}
if(i==0&&j==(n-1))
{
printf("%lf ",a[i][j]);
}
}
return 0;
}
No comments:
Post a Comment