Change column into row after a given number N - c

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;
}

Related

Why is the multiplication doubling in this C loop?

The code is supposed to take inputs to form a 3x3 Matrix and then multiply each term by the diagonal element of that line, but, for some reason that i don't know, it multiplies two times by the diagonal when the column index is bigger than the row index.
#include <stdio.h>
#define R 3
int a[R][R], i, j;
int main(void) {
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
printf("\nInsira o n%i%i ", i, j);
scanf("%i", &a[i][j]);
}
}
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
a[i][j] = a[i][j] * a[i][i];
}
}
for (i = 0; i < R; i++) {
printf("\n");
for (j = 0; j < R; j++) {
printf("%i ", a[i][j]);
}
}
}
input:
9 8 7
6 5 4
3 2 1
output:
81 648 567
30 25 100
3 2 1
The diagonal value for a given row is being changed before that row has been fully multiplied, so once the column goes past the diagonal, the multiplies are using the new value of that diagonal rather than the old value.
You can fix it (and improve the speed) as follows:
for (i = 0; i < R; i++) {
int tmp = a[i][i];
for (j = 0; j < R; j++) {
a[i][j] *= tmp;
}
}
Also, as mentioned, both i and j should be local variables.

Delete duplicate rows and columns of matrix

I need to delete (not skip while printing) the rows and columns of a matrix that appear more than once in program, and I should print only first row from the top that appears more than once or the first column from the left that appears more than once.
Example input:
1 2 3 2
4 5 6 5
1 2 3 2
7 8 9 8
After deleting:
1 2 3
4 5 6
7 8 9
Here's my code:
#include <stdio.h>
int main() {
int i, j, m, n,row,col, mat[200][200];
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
scanf("%d", &mat[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < m; j++) {
if (mat[i][j] == mat[i++][j++])
row--;
if (mat[j][i] == mat[j++][i++])
col--;
}
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Do you have any idea how to make the algorithm work for this task? Mine has mistakes.
Would you please try the following:
#include <stdio.h>
#define ROWS 200
#define COLS 200
#define TRUE 1
#define FALSE 0
/*
* delete k'th row from the m x n matrix
*/
void deleterow(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (i = k; i < m - 1; i++) {
for (j = 0; j < n; j++) {
mat[i][j] = mat[i + 1][j];
}
}
}
/*
* delete k'th columns from the m x n matrix
*/
void deletecol(int mat[ROWS][COLS], int m, int n, int k)
{
int i, j;
for (j = k; j < n - 1; j++) {
for (i = 0; i < m; i++) {
mat[i][j] = mat[i][j + 1];
}
}
}
int main() {
int i, j, m, n,row,col, mat[ROWS][COLS];
int iref, jref; // reference indexes to compare
int match; // flag to show if the row/col duplicates
// read input matrix
scanf("%d %d", &m, &n);
row = m; col = n;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &mat[i][j]);
// examine row by row
for (iref = 0; iref < m; iref++) {
// compare rows below iref and remove the row if duplicates
for (i = iref + 1; i < m; i++) {
match = TRUE;
for (j = 0; j < n; j++) {
if (mat[i][j] != mat[iref][j]) {
match = FALSE;
break;
}
}
if (match) {
deleterow(mat, m, n, i);
m--;
}
}
}
// examine column by column
for (jref = 0; jref < n; jref++) {
// compare columns more right than jref and remove the col if duplicates
for (j = jref + 1; j < n; j++) {
match = TRUE;
for (i = 0; i < m; i++) {
if (mat[i][j] != mat[i][jref]) {
match = FALSE;
break;
}
}
if (match) {
deletecol(mat, m, n, j);
n--;
}
}
}
// see the result
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%2d%s", mat[i][j], j == n - 1 ? "\n" : " ");
}
}
return 0;
}
Output with the provided example:
1 2 3
4 5 6
7 8 9
[Explanation]
As for the operations of rows:
First, focus on the top row as a "reference". The row is indexed by
the variable iref which is assigned to 0 at first.
Then compare the remaining rows with the reference, changing
the row index i from iref+1 (just below the reference row) to n-1
(the bottom row).
If a row duplicates with the reference, remove the row with the
deleterow() function and decrement the row size m by one.
The modification of m affects the for loops which compare the
loop variables with m, meaning the matrix size is updated immediately.
This is a preferable nature of the for loop (IMHO).
If the comparizon reaches the bottom row, increment iref and repeat
the comparisons again.
Finally every row has been compared to each other and the duplicates have
been deleted.
Then perform the similar operations with columns.

Creating a matrix that has its elements as the sum of the indices of that element

int main() {
int n = 0;
int matrix[n][n];
printf("Insert the order of the matrix:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
for a 2x2 matrix the output should be 0 1 1 2, but it is 1 2 1 2, and for a 3x3 matrix it should be 0 1 2 1 2 3 2 3 4 but it shows 2 3 4 2 3 4 2 3 4
The issue is that my output is always just my first row of the matrix, repeated n times. Any help?
#include <stdio.h>
int main() {
int n = 0;
printf("Insert the order of the matrix:");
scanf("%d", &n);
int matrix[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
}
You declare the array with size n equal zero. Then the length of a row is zero, hence the offset for each consecutive row is zero, too. That means all rows are stored at the same place. As a final result, the last row's values overwrite all the previous rows.
Do input n before declaring matrix[n][n].

C language 2d array fill diagonal with numbers from 1 to n

I have a 2d array filled with 0's and i'm trying to fill the main diagonal with numbers from 1 to n, this is the main code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int m, n, i, j;
printf("Number of rows and columns:");
scanf("%d", &n);
int a[n][n];
for (i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1;
printf("The matrix is:\n");
for (i = 0; i < n; i++)
{
printf(" \n ");
for(j = 0; j < n; j++)
{
printf(" %d\t ", a[i][j]);
}
}
}
What I've tried to do is to fill the diagonal manually, but that's not what I want to do. I want to make it fill itself automatically. I need to do it without using any functions.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = ((i == j) * (i + 1));
}
}
The simplest way is to add this part after you fill the matrix with zeros.
for (i = 0; i < n; i++)
arr[i][i] = i + 1;

Transforming a 3x3 two-dimensional array in a 6x6 symmetrical two-dimensional array

I'm trying to solve an exercise that requires:
- fill randomly a 3x3 two-dimensional array
- transform the array in a second one with dimension 6x6:
1&nbsp 2&nbsp 3&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp1&nbsp 2&nbsp 3 &nbsp3 &nbsp2&nbsp 1&nbsp
4&nbsp 5&nbsp 6&nbsp&nbsp&nbsp&nbsp&nbsp-> 4&nbsp 5&nbsp 6 &nbsp 6 &nbsp5 4&nbsp
7 &nbsp8&nbsp 9&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp7&nbsp 8&nbsp 9 &nbsp 9 &nbsp8&nbsp 7
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp7&nbsp 8&nbsp 9 &nbsp 9 &nbsp8 &nbsp7
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 4&nbsp 5 &nbsp6 &nbsp 6&nbsp 5&nbsp 4
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp1&nbsp 2&nbsp 3 &nbsp 3&nbsp 2&nbsp 1
I can't get it working tho' I think the logic must be right.
#include <stdio.h>
#include <stdlib.h>
#define DIM 3
int main()
{
int i, j, a[DIM][DIM],a1[DIM][DIM], a2[DIM][DIM], a3[DIM][DIM], b[2*DIM][2*DIM];
srand(time(NULL));
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
a[i][j] = rand() % 10;
}
}
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
for (i = 0; i < DIM; i++)
{
for (j = 0; j < DIM; j++)
{
a1[i][j] = a[i][DIM - 1 - j];
a2[i][j] = a[DIM - 1 -j][j];
a3[i][j] = a2[i][DIM - 1 - j];
if(i < DIM && j < DIM)
b[i][j] = a[i][j];
if(i < DIM && j >= DIM)
b[i][j] = a1[i][j];
if(i >= DIM && j < DIM)
b[i][j] = a2[i][j];
if(i >= DIM && j >= DIM)
b[i][j] = a3[i][j];
}
}
for (i = 0; i < 2*DIM; i++)
{
for (j = 0; j < 2*DIM; j++)
{
printf("%d ", b[i][j]);
}
printf("\n");
}
return 0;
}
Here are a few points that should help you to get where you want to go:
You are making four copies of a three by three array.
You need a nested for loop to iterate over the original array
Inside the for loop you will be making four assignments, one for each copy
Each copy has a different location
When you make the copies you will need to add an offset to each index
This is best done by just using the index of what was the value in the upper left corner
Each copy goes in different directions
Whenever a dimension is going in the wrong dimension, you can simply subtract the index
Anything else?
If you always fill the 6 X 6 matrix in the same way shown in the example, then you can use the following code,
m=0;
fl1=0;
for (i = 0; i < 6; i++)
{
n=0;
fl2=0;
for (j = 0; j < 6; j++)
{
b[i][j] = a[m][n];
if(n==3)
{
fl2=1;
}
if(fl2==0)
{
n++;
}
else
{
n--;
}
}
if(m==3)
{
fl1=1;
}
if(fl1==0)
{
m++;
}
else
{
m--;
}
}
for (i = 0; i < DIM*2; i++){
for (j = 0; j < DIM*2; j++){
b[i][j]=a[i-(i>=DIM)*((i-DIM)*2+1)][j-(j>=DIM)*((j-DIM)*2+1)];
}
}

Resources