Write a program that fills the right-to-left diagonal of asquare matrix with zer
ID: 3610375 • Letter: W
Question
Write a program that fills the right-to-left diagonal of asquare matrix with zeros, the lower right triangle with -1s and theupper left triangle with +1s. The output of the program, assuming a6 by 6 matrix is shown below: 1 1 1 1 1 0 1 1 1 1 0 -1 1 1 1 0-1-1 1 1 0-1-1-1 1 0-1-1-1-1 0-1-1-1-1-1 Write a program that fills the right-to-left diagonal of asquare matrix with zeros, the lower right triangle with -1s and theupper left triangle with +1s. The output of the program, assuming a6 by 6 matrix is shown below: 1 1 1 1 1 0 1 1 1 1 0 -1 1 1 1 0-1-1 1 1 0-1-1-1 1 0-1-1-1-1 0-1-1-1-1-1Explanation / Answer
#include<stdio.h>
#include <conio.h>
int main()
{int n,i,j;
int mat[10][10];
printf("How large is your matrix? ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
mat[i][j]=-1; //start the matrix with all -1, now the bottom half is set
for(j=0;j<n;j++) //put the 1's in if n=6 5 1's row 0, 4 1'srow 1 etc
for(i=n-j-2;i>=0;i--)
mat[j][i]=1;
for(j=0;j<n;j++) //this does the diagnol if n=6 6-0-1=5 lastrow 6-1-1=4 next to last row
mat[n-j-1][j]=0; //starting with the bottom row
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
printf("%3d ",mat[i][j]);
printf(" ");
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.