How to transpose matrix with malloc and print it - c

I want my code to scan value of rows and cols and then to print the matrix (I don't care from the values in the matrix) and to print the transpose matrix.
What is my problem with mine code?
the code :
#include <stdio.h>
#include <string.h>
void main()
{
int rows1, colums1, i, j;
int**matrix1, **transpose_matrix1;
printf_s("enter rows and colums\n");
scanf_s("%d%d", &rows1, &colums1);
matrix1 = malloc(sizeof(int*)*rows1);
for (i = 0; i < rows1; i++)
matrix1[i] = malloc(sizeof(int)*colums1);
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
matrix1[i][j] = i+j;
puts("matrix: ");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < colums1; j++)
{
printf_s("%d ", matrix1[i][j]);
}
printf_s("\n");
}
transpose_matrix1 = malloc(sizeof(int*)*colums1);
for (i = 0; i < colums1; i++)
transpose_matrix1[i] = malloc(sizeof(int)*rows1);
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
transpose_matrix1[j][i] = matrix1[i][j];
puts("transpose matrix:");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < colums1; j++)
{
printf_s("%d ", transpose_matrix1[i][j]);
}
printf_s("\n");
}
}
Input:
2 3
0 1 2
1 2 3
Output:
enter rows and colums
matrix:
transposematrix:
0 1 -33686019
1 2 -33686019

When you initialize the transpose matrix you do it like
transpose_matrix1[j][i] = matrix1[i][j];
Note the order in which you use j and i in transpose_matrix1[j][i].
Then when you print you use transpose_matrix1[i][j]. Note that the order of j and i have changed, even though the looping around both are the same. You need to use the same order for j and i in both loops.

You have confused yourself a little with what is a colum and what is a row
here is a fixed version:
void print_matrix(int ** matrix, int rows, int colums){
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < colums; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int transpose_rows1, transpose_colums1, rows1, colums1, i, j;
int**matrix1, **transpose_matrix1;
printf("enter rows and colums\n");
scanf("%d%d", &rows1, &colums1);
matrix1 = (int**)malloc(sizeof(int*)*rows1);
for (i = 0; i < rows1; i++)
matrix1[i] = (int*)malloc(sizeof(int)*colums1);
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
matrix1[i][j] = i+j;
puts("matrix: ");
print_matrix(matrix1, rows1, colums1);
transpose_rows1 = colums1;
transpose_colums1 = rows1;
transpose_matrix1 = (malloc(sizeof(int*) * transpose_rows1));
for (i = 0; i < transpose_rows1; i++)
transpose_matrix1[i] = (malloc(sizeof(int) * transpose_colums1));
for (i = 0; i < rows1; i++)
for (j = 0; j < colums1; j++)
transpose_matrix1[j][i] = matrix1[i][j];
puts("transpose matrix:");
print_matrix(transpose_matrix1, transpose_rows1, transpose_colums1);
return 0;
}
In order to not confuse yourself in the future, try to use function and names with the exact meaning.
It is easier to create a function to print the array than to write the code twice and then thinking twice about what is the row now and what is the column.
And also, it makes it easier if the variable name colums1 is always a column of a matrix, not a column on one and a row on the other, so just create a new variable for the second array column and row with the correct data.

In a transponsed matrix, the number of rows is the number of columns of the normal matrix and the number of columns is equal to the number of rows of the normal matrix. Therefore, i should go from 0 to columns1 and j from 0 to rows1:
for (i = 0; i < colums1; i++)
for (j = 0; j < rows1; j++)
transpose_matrix1[i][j] = matrix1[j][i];
puts("transpose matrix:");
for (i = 0; i < colums1; i++)
{
for (j = 0; j < rows1; j++)
{
printf("%d ", transpose_matrix1[i][j]);
}
printf("\n");
}

Related

How to correctly print the result of matrices multiplication in different cases?

I'm making a program that multiplies 3 matrices and print the outcome. The program can input several cases of multiplication and each case can determined their own number of NxN matrix (n). However, if I input 2 cases, first with n=2 and second with n=3, the output of the first case will have a 3x3 matrix with row 3 and column 3 only have 0s. How do I fix this problem?
#include <stdio.h>
int main(){
int t, n, i, j, k, l, a[50][50][10], b[50][50][10], c[50][50][10], d[50][50][10], e[50][50][10];
scanf ("%d", &t);
for (l=1; l<=t; l++){
scanf("%d", &n);
// matrix a
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j][l]);
}
}
// matrix b
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j][l]);
}
}
// matrix c
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & c[i][j][l]);
}
}
//Multiplication
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
d[i][j][l] = 0;
for (k = 0; k < n; k++) {
d[i][j][l] += a[i][k][l] * b[k][j][l];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
e[i][j][l] = 0;
for (k = 0; k < n; k++) {
e[i][j][l] += d[i][k][l] * c[k][j][l];
}
}
}
}
//Printing the product
for (l=1; l<=t; l++){
printf ("Case #%d:\n", l);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", e[i][j][l]);
}
printf("\n");
}
}
return 0;
}
This is the expected output.

How to transpose a matrix without using another matrix?

Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.

How to get access to each row and column in rectangular matrix

Good day to everybody. My task is to determine if a rectangular matrix has two rows of positive elements. I write the code below. At end I try to chect statement about positive row, but it's not working at all. Please explain me how to correct get the access to the each row and column in matrix, and meaybe edit my code.
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#define M 3
#define N 4
int main(){
setlocale(LC_ALL, "Rus");
float a[M][N]; //set matrix with 3 row and 4 column
int i, j; // row and column index
int count;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
scanf_s("%f", &a[i][j]);
}
for (i = 0; i < M; i++){
printf("%d-я строка:", i + 1);
for (j = 0; j < N; j++)
printf("%f", a[i][j]);
printf("\n");
}
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
_getch();
return 0;
}
When counting the number of positive elements in a row, you need to set the count back to zero for each row.
So instead of:
count = 0;
for (i = 0; i < M; i++){
for (j = 0; j < N; j++)
if (a[i][j] > 0){
count++;
printf("%d", count);
}
}
you need
for (i = 0; i < M; i++){
count = 0;
for (j = 0; j < N; j++) {
if (a[i][j] > 0){
count++;
}
}
printf("row %d has %d positive elements\n", i, count);
}

Merge two 2d arrays into one 1d array in C

I have taken two 2d arrays but the output is very different from expected.It should merge two 2d arrays into one 1d array.I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] = array1[i-3][j];
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
Please try this code,To Merge two 2d arrays into one 1d array in C
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? (arraySum[i][j] = array2[i][j]) : (arraySum[i][j] = array1[i-3][j]);
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I hope this code will be usefull.
Thank you.
I believe you're trying to be too smart with ternary operator, you can do it simpler way:
if (i < 3)
arraySum[i][j] = array2[i][j];
else
arraySum[i][j] = array1[i-3][j];
Listen to your compiler it would've told you what was wrong if you've compiled with -Wall -Wextra.
And if you insist on using ternary then this would probably be clearer:
arraySum[i][j] = (i < 3) ? array2[i][j] : array1[i-3][j];

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