I need to change the matrix columns into rows after a given number N. For example if N = 3 then the row is n * 2 given in this exercise. Now after the 3rd column every other column needs to be a row. I know how to transpose a matrix but I get confused how to do it after given N. Code:
#include <stdio.h>
int main() {
int n;
int a[n][n * 2];
int b[n * 2][n];
scanf("%d", &n);
for(int i = 0;i < n; i++) {
for(int j = 0; j < n * 2; j++) {
scanf("%d", a[i][j]);
}
}
for(int i = 0; i < n * 2; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = b[j][i];
}
}
return 0;
}
Example for n = 3.
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
I need to achieve
1 2 3
7 8 9
13 14 15
4 5 6
10 11 12
16 17 18
First square of the matrix (array) is left unchanged, so both arrays have the same starting. Later part is shifted n units left and n units down, so the following code should help you to solve the problem:
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i + n][j] = a[i][j + n];
}
}
As both set of for loops have the same kind of variable changes, we can converge them into one, as @Chris Turner did. The whole code should look something like this:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a[n][n * 2];
int b[n * 2][n];
for(int i = 0;i < n; i++) {
for(int j = 0; j < n * 2; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
b[i + n][j] = a[i][j + n];
}
}
return 0;
}
Firstly you need to initialise n
before you use it. In the code below, n
has no defined value, so using it to define the size of your arrays leads to undefined behaviour.
int n;
int a[n][n * 2];
int b[n * 2][n];
You can move the array definitions until after you've set n
Secondly, when using scanf
to read in an int
you have to pass in a pointer to the variable, so this line is wrong and your compiler should be throwing up warnings about it
scanf("%d", a[i][j]);
it should be
scanf("%d", &a[i][j]);
if you want to copy from a
into b
you need to do it the right way around
you've got which is copying from b
to a
and also goes out of bounds as i
goes up to n*2
and a
's first index is just n
.
a[i][j] = b[j][i]
what you want is just this.
b[i][j] = a[j][i]
But that doesn't solve your problem as that is just transposing and you're trying to split the matrix in half and stack it differently. So to start with you need to copy the first n
by n
elements from a
to b
like this.
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
and then to copy the second chunk you can use the same loops and offset by n
b[i+n][j] = a[i][j+n];
After all these changes your code should look like this:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a[n][n * 2];
int b[n * 2][n];
for(int i = 0;i < n; i++) {
for(int j = 0; j < n * 2; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = a[i][j];
b[i+n][j] = a[i][j+n];
}
}
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With