https://i0.wp.com/eblogarithm.com/wp-content/uploads/2016/10/Adding-Two-Matrices-Vertically-and-Horizontally-in-C-Language.png?fit=640%2C360

Adding Two Matrices Vertically and Horizontally in C Language

https://gyazo.com/54244c07bf4feb317edd9662835e4a0e

 

Hi guys , I just tried to code to add two matrices vertically and horizontally .
Check the screenshot I’ve attached above .

I hope you like it , and just let me know in comment if you have any better solution .

 

#include<stdio.h>
int main()
{
int m,n,i,j;
printf(“Rows of Matrix: “);
scanf(“%d”, &m);
printf(“Column of Matrix: “);
scanf(“%d”, &n);
int ar[m][n], arr[m][n];

printf(“First Matrix: \n”);

for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf(“%d”, &ar[i][j]);
}
printf(“\n”);
}

printf(“Second Matrix : \n”);

for(i=0; i<m; i++){
for(j=0; j<n; j++){
scanf(“%d”, &arr[i][j]);
}
printf(“\n”);
}

int ar1[m+m][n], ar2[m][n+n],p=0;

for(i=0; i<2*m; i++){
if(i<m){
for(j=0; j<n; j++){
ar1[i][j]=ar[i][j];
}
}
else{
for(j=0; j<n; j++){
ar1[i][j]=arr[p][j];
}
p++;
}
}

for(i=0; i<m; i++){
p=0;
for(j=0; j<2*n; j++){
if(j<n)
ar2[i][j]=ar[i][j];

else{

ar2[i][j]=arr[i][p];
p++;
}

}
}

printf(“Vertically Added Matrix”);
printf(“\n”);
for(i=0; i<2*m; i++){
for(j=0; j<n; j++)
printf(“%d  “, ar1[i][j]);
printf(“\n”);
}

printf(“\n”);
printf(“Horizontally Added Matrix”);
printf(“\n”);
for(i=0; i<m; i++){
for(j=0; j<2*n; j++)
printf(“%d  “, ar2[i][j]);
printf(“\n”);
}

return 0;
}

(Visited 22 times, 1 visits today)



There are no comments

Add yours

Leave a Reply

%d bloggers like this: