The code is to store rows and columns of matrix in a linear manner in another array.
Example: Given a matrix:
a b c d
e f g h
i j k l
m n o p
The task is to store values in a new matrix:
Matrix H contains:
a b c d
e f g h
i j k l
m n o p
Matrix V contains:
a e i m
b f j n
c g k o
d h l p
The code I have written is:
#include<stdio.h>
int main() {
int m, n;
char a[10][10];
scanf("%d%d", &m, &n);
char horizontal[m][10], vertical[n][10];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf(" %c", &a[i][j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
horizontal[i][j] = a[i][j];
vertical[i][j] = a[j][i];
}
}
printf("horizontal values are:\n");
for (int i = 0; i < m; i++) {
printf("%s\n", horizontal[i]);
}
printf("vertical values are:\n");
for (int i = 0; i < m; i++) {
printf("%s\n", vertical[i]);
}
}
The output when I execute this code is:
4
4
a b c d
e f g h
i j k l
m n o p
horizontal values are:
abcd
efgh
ijklñ²b
mnopb
vertical values are:
aeim
bfjn
cgko
dhlpb
What is wrong in this code?
char horizontal[m][10] = { 0 };etc.