2D Arrays L 2 0 - L 2 1: 04/28/2023 Cse 1001 Department of Cse 1
2D Arrays L 2 0 - L 2 1: 04/28/2023 Cse 1001 Department of Cse 1
2D Arrays L 2 0 - L 2 1: 04/28/2023 Cse 1001 Department of Cse 1
L20-L21
n“); for(j=0;j<n;j++)
rowsum[i]=rowsum[i]+a[i][j];
scanf(“%d %d”,&m, &n);
}
//Reading
printf("\n“);
printf("enter elements for a \n“);
for (i=0;i<m;i++){
for(j=0;j<n;j++)
scanf(“%d”, &a[i][j]);
04/28/2023 CSE 1001 Department of CSE 12
Row Sum & Column Sum of a matrix
//Display
//Column sum
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
{
for(j=0;j<n;j++)
colsum[j]=0;
printf("\t %d“,a[i][j]);
for(i=0;i<m;i++)
printf(“->“)
colsum[j]=colsum[j]+a[i][j];
printf(“%d\n”,rowsum[i]);
}
}
printf("\n“);
for(i=0;i<n;i++)
printf("\t %d”,colsum[i]);
04/28/2023 CSE 1001 Department of CSE 13
Row Sum & Column Sum of a matrix
n“); for(j=0;j<n;j++){
816 618
//Matrix is Magic square or not 357 753
void main() 492 294
{
int mag[10][10], i, j, row, col, rowsum[10], colsum[10];
int pd=0, sd=0, k, x=0, b[100];
clrscr();
printf("enter dimension \n“);
scanf(“%d %d”,&row,&col);
if(row!=col) // checking for square matrix
{
printf(“matrix is not square”);
getch(); exit(0);}
• 2 Dimensional Arrays
Display a Matrix:
Reading a Matrix: int a[100][100];
int a[100][100]; for(i=0;i<m;i++)
for(i=0;i<m;i++) {
{ for(j=0;j<n;j++)
for(j=0;j<n;j++) printf(“%d”,a[i][j]);
scanf(“%d”,&a[i][j]); printf(“ “);
} printf(“\n”);
}
04/28/2023 CSE 1001 Department of CSE 28
Tutorials