1

I am trying to create a program to find the transpose of a matrix my dynamic memory allocation. However, while entering the elements of the matrix I can't input more than one element, its only taking the a[0][0] as the input and the program is ending after that.

#include <stdio.h>
#include <stdlib.h>

void createMatrix(int **, int, int);
void inputElements(int **, int, int);
void transpose(int **, int **, int, int);
void display(int **, int, int);

void main()
{
    int **matrix, **trans, rows, cols;
    printf("\nEnter number of rows in the matrix: ");
    scanf("%d", &rows);
    printf("\nEnter number of columns in the matrix: ");
    scanf("%d", &cols);
    createMatrix(matrix, rows, cols);
    createMatrix(trans, cols, rows);
    inputElements(matrix, rows, cols);
    transpose(matrix, trans, rows, cols);
    printf("\nMATRIX:\n");
    display(matrix, rows, cols);
    printf("\nTRANSPOSE OF THE MATRIX:\n");
    display(trans, rows, cols);
}

void createMatrix(int **a, int r, int c) //for allocating memory for the matrix
{
    int i, j;
    a = (int **)malloc(sizeof(int *) * r);
    for(i = 0; i < r; i++)
        a[i] = (int *)malloc(sizeof(int) * c);
}

void inputElements(int **a, int r, int c) //for entering matrix elements
{
    int i, j, t;
    for(i = 0; i < r; i++)
    {
        for(j = 0; j < c; j++)
        {
            printf("\nEnter matrix element[%d][%d]: ", i + 1, j + 1);
            fflush(stdin);
            getchar();
            scanf("%d", &(a[i][j]));
        }
    }
}

void transpose(int **a, int **t, int r, int c) //for finding out the transpose of the matrix
{
    int i, j;
    for (i = 0; i < c; i++)
    {
        for (j = 0; j < r; j++)
            t[i][j] = a[j][i];
    }
}

void display(int **a, int r, int c) //for displaying the matrix
{
    int i, j;
    for (i = 0; i < r; i++)
    {
        printf("\n");
        for (j = 0; j < c; j++)
            printf("\t%d", a[i][j]);
    }
}

There are many posts about scanf in loops I've seen, but I wasn't able to connect my issue with any of them.

  • 1
    Be very wary of [Using `fflush(stdin)`](https://stackoverflow.com/q/2979209/15168). And always check the return value from `scanf()` — you don't know when it fails. Consider using `fgets()` or POSIX `getline()` to read lines and then [`sscanf()` in a loop](https://stackoverflow.com/q/3975236/15167) to parse the data. It often makes error reporting easier. The `getchar()` in the loop is weird — that's likely to delete the first character of the number. The `%d` format skips white space, including newlines — only 3 conversion specifiers do not do that (`%c`, `%[…]` scan sets, and `%n`). – Jonathan Leffler May 25 '22 at 22:22
  • `a = (int **)malloc(sizeof(int *) * r);` `a` is a local variable being modified here, and does not modify the value in the calling function that was passed in. – Christian Gibbons May 25 '22 at 22:38
  • @ChristianGibbons I have passed a in the function createMatrix(int **a, int r, int c), which I am changing. How can it not modify the value in the calling function then. I did't get you. Can you please explain me and what can I do alternatively? – Mir Miracle May 26 '22 at 15:13
  • 1
    `a` is a copy of a pointer. You are changing that local copy to point somewhere, but the pointer in the calling function is not modified by this. – Christian Gibbons May 26 '22 at 16:34
  • @ChristianGibbons What is the right way to do it then? – Mir Miracle May 26 '22 at 21:47
  • Add another layer of indirection. `void createMatrix(int ***a, int r, int c)` ... `*a = (int **)malloc(sizeof(int *) * r);` ... `createMatrix(&matrix, rows, cols);` – Christian Gibbons May 26 '22 at 22:03

0 Answers0