#include<stdio.h>
#include<conio.h>
int main(){
int r1,c1,r2,c2,sum;
printf("Enter the number of rows and colums for first matrix ");
scanf ("%d ,%d",&r1,&c1);
int a[r1][c1];
int b [r2][c2];
int i,j,k;
for(i=0;i<r1;i ){
for(j=0;j<c1;j ){
k=((i 1)*10) (j 1);
printf("enter element [a %d ] = ",k);
scanf("%D",&a[i][j]);
}
}
printf("the first matrix is : \n");
printf("\t");
for(i=0;i<r1;i ){
for(j=0;j<c1;j ){
printf("%d ",a[i][j]);
}
printf("\n \t");
}
printf("\n") ;
printf("Enter the number of rows and colums for second matrix ");
scanf ("%d ,%d",&r2,&c2);
if (r1!=r2 || c1!=c2){
printf("Only matrices of similar orders can be added or subtracted ");
}
else{
for(i=0;i<r2;i ){
for(j=0;j<c2;j ){
k=((i 1)*10) (j 1);
printf("enter element [a %d ] = ",k);
scanf("%d",&b[i][j]);
}
}
printf("the second matrix is : \n");
printf("\t");
for(i=0;i<r1;i ){
for(j=0;j<c1;j ){
printf("%d ",b[i][j]);
}
printf("\n \t");
}
printf("\n the added matrix is \n \t");
for(i=0;i<r1;i ){
for(j=0;j<c1;j ){
printf("%d ",a[i][j] b[i][j]);
}
printf("\n \t");
}
}
}
as you can see the first matrix is display correctly but the second one is faulty , i don't know where i went wrong because i used the same logic for both please help. the 2nd matrix seems faulty and i cant figure out why ? i repeated the same process i did for matrix 1 but it doesn't want to work.
CodePudding user response:
int main(){
int r1,c1,r2,c2,sum;
printf("Enter the number of rows and colums for first matrix ");
scanf ("%d ,%d",&r1,&c1);
int a[r1][c1];
int b [r2][c2];
Question: what are the values of r2
and c2
at the last line?
Answer: they are undefined, so anything can happen.