Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to make the actress transposed in C language

+1 vote
asked May 2, 2021 by paulo magalhaes (1,480 points)
edited May 3, 2021 by paulo magalhaes
Write a program that reads all the elements of an N × N matrix and shows the matrix and its transposition on the screen. Note: The size (N) of the matrix must be read from the keyboard and its allocation must be dynamic through the malloc function.

#include <stdio.h>

#include <stdlib.h>

#define T_BUFFER 512

int main(void)

{

    int *numeros, q_linhas = 0, q_colunas = 0, linhas, colunas;

    char buffer[T_BUFFER];

    /* Pega a quantidade de linhas da matriz */

    do {

        printf("Digite a quantidade de linhas da matriz\n");

        if (fgets(buffer, T_BUFFER, stdin) != NULL) {

            if (buffer[0] != '\n') {

                if (sscanf(buffer, "%i", &q_linhas) != 1) {

                    printf("Digite um numero\n");

                }

            } else {

                printf("Digite uma entrada valida!\n");

            }

        }

    } while (q_linhas <= 0);

    /* Pega a quantidade de colunas da matriz */

    do {

        printf("Digite a quantidade de colunas da matriz\n");

        if (fgets(buffer, T_BUFFER, stdin) != NULL) {

            if (buffer[0] != '\n') {

                if (sscanf(buffer, "%i", &q_colunas) != 1) {

                    printf("Digite um numero\n");

                }

            } else {

                printf("Digite uma entrada valida!\n");

            }

        }

    } while (q_colunas <= 0);

    /* Reserva memoria para a matriz */

    numeros = malloc(sizeof(*numeros)*q_linhas*q_colunas);

    if (numeros == NULL) {

        perror("Erro ao reservar memoria!");

        exit(EXIT_FAILURE);

    }

    /* Lê a matriz do úsuario */

    for (linhas = 0; linhas < q_linhas; linhas++) {

        for (colunas = 0; colunas < q_colunas; colunas++) {

            int pega_novamente = 1;

            /* Pega um numero */

            do {

                printf("[%i][%i]\n", linhas, colunas);

                if (fgets(buffer, T_BUFFER, stdin) != NULL) {

                    if (buffer[0] != '\n') {

                        if (sscanf(buffer, "%i", &numeros[(linhas*q_colunas)+colunas]) == 1) {

                            pega_novamente = 0;     /* Sai do "do {} while()" */

                        }

                    } else {

                        printf("Digite uma entrada valida!\n");

                    }

                }

            } while (pega_novamente);

        }

    }

    /* Mostra a matriz */

    printf("Matriz digitada\n");

    for (linhas = 0; linhas < q_linhas; linhas++) {

        for (colunas = 0; colunas < q_colunas; colunas++) {

            printf("%i,", numeros[(linhas*q_colunas)+colunas]);

        }

        putchar('\n');

    }

    getchar();      /* Espera o enter */

  /* Libera a memoria */

    free(numeros);

    return(0);

}

2 Answers

0 votes
answered May 4, 2021 by Peter Minarik (86,180 points)

There are multiple problems here.

  • Apparently you do not understand the concept of a matrix. (You read one line and one column in your code, not line * column numbers.)
  • You don't seem to understand what the transpose of a matrix is. (You're trying to print the same matrix that you store, not the transpose of it.)
  • When you allocate memory, you (wrongly) allocate lines * columns integer pointers, while you should have allocated lines * columns integers (not pointers). For your luck, they have the same size (4-8 bytes depending on 32/64 bit architecture). Nevertheless, the logic is wrong.
    Furthermore, malloc returns a void*, you need to cast it to int pointer.
A simple solution (no user input check) would be something like this:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows;
    printf("Please enter N (the number of columns and rows) for a square matrix: ");
    scanf("%d", &rows);
    const int columns = rows;
    
    int **matrix = (int**)malloc(sizeof(int*) * rows);  // Row-major matrix
    for (int rowIndex = 0; rowIndex < rows; rowIndex++)
    {
        matrix[rowIndex] = (int*)malloc(sizeof(int) * columns);
        for (int columnIndex = 0; columnIndex < columns; columnIndex++)
        {
            printf("[%d][%d] = ", rowIndex + 1, columnIndex + 1);
            scanf("%d", &matrix[rowIndex][columnIndex]);
        }
    }

    for (int rowIndex = 0; rowIndex < rows; rowIndex++)
    {
        for (int columnIndex = 0; columnIndex < columns; columnIndex++)
            printf("%d\t", matrix[columnIndex][rowIndex]); // The transpose of a matrix is where the rows are turned into columns and vice versa

        printf("\n");
    }

    for (int rowIndex = 0; rowIndex < rows; rowIndex++)
        free(matrix[rowIndex]);
    free(matrix);
    
    return 0;
}
0 votes
answered May 6, 2021 by 2005 A41104 (190 points)
#include<stdio.h>

int main()

{

int a[20][20], i, j, m, n;

printf("enter size of row and column : ");

scanf("%d%d",&m,&n);

printf("enter array elements : ");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

scanf("%d",&a[i][j];

}

}

printf("array elements are : );

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

printf("%3d",a[i][j]);

}

}

printf("transpose matrix is : );

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

printf("%d",a[j][i]);

}

}

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...