I want to write a program that does the following:

reads and integer matrix from stdin and outputs the input matrix, its transpose and multiplication of the input matrix with its transpose.

I limited the matrix to a 10x10.
I want to ask the user to enter a number of columns and then ask the user to input the elements in each row. I am currently stuck trying to understand what kind of stream function to use in order to store each number in each element of the matrix.

This is what I have:

    #include<stdio.h>

    int main(void)
    {       // n - # of columns, i - counter.
            int n, i, j, A[10][10];

            //queries user for number columns
            printf("Please enter the number of columns: ");
            scanf("%d", &n);

            //checks if # of 0<n<10. if not, returns error and exits
            if(n>10 || n<0)
            {
                    printf("Number of columns not supported\n");
                    exit(1);
            }


             //loop to enter the elements in each row
             for (i = 0 ; i < n; i++)
             {
                    for(j = 0; j < n; j++)
                    printf("Please enter the elements in row %d\n", i+1);
                    j = fgetc(stdin);
                    A[j][i];
                    printf("%d", A[j][i]);
             }




return 0;
}

I know this is very incorrect but I am very new to programming.

Thanks!

Recommended Answers

All 2 Replies

lines 22-27: if statements that have more than one line require { and } around them.

line 24: use a different variable because j is used as loop counter on line 22.

line 25: That line does nothing. You need something like this: A[j][i] = X; where X is the value return by scanf() in the previous line.

This is my transformed code.
I skipped the part I had trouble with and continued with the main goals of the program.
I need to change the way the program operates twice:
1. To find out from the user input in each row the number of columns the matrix has.
2. To do the matrix multiplication between A and At.

I am not quite sure where to start with 1.

#include<stdio.h>

int main(void)
{       // n - # of columns, i - counter.
        int n, i, j, A[10][10], At[10][10], m, MM[10][10], sum, k;

        //queries user for number columns
        printf("Please enter the number of columns: ");
        scanf("%d", &n);

        //checks if # of 0<n<10. if not, returns error and exits
        if(n>10 || n<0)
        {
                printf("Number of columns not supported\n");
                exit(1);
        }

        //queries user for number of rows
        printf("Please enter the number or rows: ");
        scanf("%d", &m);

        if(m>10 || m<0)
        {
                printf("Number of rows not supported\n");
                exit(1);
        }

        //loop to enter the elements in each row
        printf("Enter elements of matrix: ");
        for (i = 0 ; i < n; i++)
        {
                for(j = 0; j < m; j++)
                {       //printf("Please enter the elements in row %d\n", i+1);
                        //A[j][i] = fgets(j, 100, stdin);
                        //printf("%d", A[j][i]);
                        scanf("%d", &A[j][i]);
                }
        }

        //print matrix on screen
        printf("The Matrix you entered is:\n");
        for(i = 0; i < n; i++)
        {
                for(j = 0; j <n; j++)
                {
                        printf("[%d]", A[j][i]);
                }
                printf("\n");
        }

        //Matrix transpose
        for (i = 0; i < n; i++)
        {       for(j = 0; j < n; j++)
                {
                        At[i][j] = A[j][i];
                }
        }

        //Print Matrix Transpose
        printf("The matrix transpose is: \n");
        for (i = 0; i < m; i++)
        {
                for(j = 0; j < n; j++)
                {
                        printf("[%d]", At[j][i]);
                }
                printf("\n");
        }

        //Matrix Multiplication
        printf("The product of the matrices is: \n");
        for(i = 0; i < n; i++)
        {
                for(j = 0; j < m; j++)
                {       MM[i][j]=0;
                }
        }
        for(i = 0; i < n; i++)
         {
                for(j = 0; j < m; j++)
                {
                        sum = 0;
                        for(k = 0; k < n; k++)
                        {
                                sum = sum + A[i][k]*At[k][j];
                                MM[i][j] = sum;
                        }
                }
        }

        //Print Multiplied Matrices
        for( i =0; i < n; i++)
        {
                for( j = 0; j < m; j++)
                {
                        printf("[%d]", MM[i][j]);
                }
                printf("\n");
        }


return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.