How to transpose a matrix without using another matrix? - arrays

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.

Related

multiplication of matrices using while loop in c

here below i have given multiplication of matrices in c language using for loop but can any help me make a more simplified version or can any help me make it using while loop
i want a simplified version
i want a code in while loop
:) just learning
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
system("cls");
printf("enter the number of row=");
scanf("%d", &r);
printf("enter the number of column=");
scanf("%d", &c);
printf("enter the first matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("enter the second matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("multiply of the matrix=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
mul[i][j] = 0;
for (k = 0; k < c; k++)
{
mul[i][j] += a[i][k] * b[k][j];
}
}
}
//for printing result
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
return 0;
}
It will not make code simpler only harder to read.
One of the loops example:
printf("multiply of the matrix=\n");
i = 0;
while (i < r)
{
j = 0;
while(j < c)
{
mul[i][j] = 0;
k = 0;
while(k < c)
{
mul[i][j] += a[i][k] * b[k][j];
k++;
}
j++;
}
i++;
}

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

How to transpose matrix with malloc and print it

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

Swapping rows in Gauss Jordan method

Suppose in Gauss Elimination Method the first value of matrix, A[0][0]=0
Then how can I swap row 1 of matrix "A" with row 2, so that i get the correct result ?
Assuming a simpel 2d-array build the way as shown below: just exchange the rows.
#include <stdio.h>
#include <stdlib.h>
#define S_MATRIX_DIM 5
int main()
{
int **A;
int *tmp;
int i, j, entry;
entry = 0;
A = malloc(S_MATRIX_DIM * sizeof(int *));
for (i = 0; i < S_MATRIX_DIM; i++) {
A[i] = malloc(S_MATRIX_DIM * sizeof(int));
for (j = 0; j < S_MATRIX_DIM; j++) {
A[i][j] = entry++;
}
}
puts("Matrix A =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
tmp = A[0];
A[0] = A[1];
A[1] = tmp;
puts("Matrix A, row exchanged =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
for (i = 0; i < S_MATRIX_DIM; i++) {
free(A[i]);
}
free(A);
exit(EXIT_SUCCESS);
}
keep a temp value from the same type;
swap value by value,
example:
temp=a[i][j];
a[i][j]=a[i+1][j];
a[i+1][j]=temp;

Resources