Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Please this is my program i need to enter the column to have the matrix, the tr

ID: 3863610 • Letter: #

Question

(Please this is my program i need to enter the column to have the matrix, the transpose and the multiplication I need help, when I try to run it give me the 28944 segmentation fault)

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int i, j,x, n, m, k;     /* variable integers*/
    int A[10][10];          /* integer array for matrix A*/
    int At[10][10];        /*array for matrix At*/
    int prod[10][10];    /* array for matrix ( produit of A*At)*/
    int b[10][10];
    int sum =0;     /* varible integer for elememt of matrix prod*/
    printf("Enter the number of columns: ");/* input the number of columns*/
    scanf("%d", &n);                       // read the input
        if ((n<=0)|| (n>10))            /*statement on the value of n*/
                {
                        fprintf(stderr, "Sorry this number must be between 1 and 10 "); /*output directory*/
                        exit(0);   /* exit the program*/
                }
        else
                k=0;                    /* initialization of k to being 0*/
        while ( fscanf( stdin, "%d", &A[k/n][k%n]) != EOF) /* while loop*/
              {
                                k++;
                  }
        if (k%n !=0)
                {
                         fprintf(stderr, "Invalid matric for insufficient of elememts. ");
                 exit(1);               /* quit the program*/
                }
        else
                         m = k/n;               /* value assignment*/
        printf("The matrix A is: "); /* printf the statement*/
        for(j=0;j<n;j++)
        {
            printf("%3d ",A[i][j]);
            printf(" ");
        }
        for(i=0; i<m; i++)
        for(j=0; j<n;j++)
        b[j][i]=A[i][j];
        printf("The transposed array is: ");
        for (i=0; i<m; i++)                     /*outer for loop*/
            {
            for (j=0; j<n; j++)             /*inner loop*/
                {
                    At[j][i]=A[i][j];   /* switch element's positions*/
                }
            }

            for (i=0; i<n; i++)                     /*outer for loop*/

               {
                    for (j=0; j<m; j++)             /*inner for loop*/
                    {
                        printf("%5d", At[i][j]); /*print out elements*/
                    }
                    printf(" ");   /* print new line*/
                }
                for (i=0; i<m; i++)                     /* outer loop*/
                    {
                        for (j=0; j<m; j++)              /* inner loop*/
                            {
                            for (x=0; x<n; x++) /* inner loop*/
                                {
                                    sum =sum + A[i][x] * At[x][j]; /*assignment*/
                                }
                        prod[i][j] = sum;       /* assignment*/
                        sum =0;                         /*reinitialisation*/
                            }
                    }
          printf("The product of A by At is: ");/* print a statement*/

          for (i=0; i<m; i++)
            {
                for (j=0; j<m; j++)
                    {
                        printf("%5d", prod[i][j]); /* print element of product*/
                    }
                    printf(" ");                                 /* print new lines */
            }
    return 0;
}     /*end program*/

Explanation / Answer


#include <stdio.h>
#include <conio.h>
void read(int x[ ][10] , int nr , int nc) ;
void multiply(int a[ ][10] , int b[ ][10] , int c[ ][10] , int l , int m , int n) ;
void transpose(int x[ ][10] , int y[ ][10] , int nr , int nc) ;
void show(int y[ ][10] , int nr , int nc) ;


void main()
{
int a[10][10] , b[10][10] , c[10][10] , nra , nca , nrb , ncb ;
int x[10][10] , y[10][10] , m , n ;

clrscr();

printf("Performing Matrix Multiplication ") ;
printf("Enter the number of rows and columns of first matrix: ") ;
scanf("%d %d", &nra , &nca) ;

printf("Enter the number of rows and columns of second matrix: ") ;
scanf("%d %d", &nrb , &ncb) ;

if(nca==nrb)
{
printf("Enter elements of first matrix row-wise: ") ;
read(a,nra,nca) ;

printf("Enter elements of second matrix row-wise: ") ;
read(b,nrb,ncb) ;

multiply(a,b,c,nra,nca,ncb);
printf("Result of matrix multiplication is: ") ;
show(c,nra,ncb) ;
}
else
printf("Matrices A and B do not conform to multiplication") ;

printf(" Performing Matrix transpose ") ;
printf("Enter the number of rows and columns: ") ;
scanf("%d %d", &m , &n) ;
printf("Enter elements of the matrix row-wise: ") ;
read(x , m , n) ;
transpose(x , y , m , n) ;
printf("Transpose of given matrix is: ") ;
show(y,n,m) ;

getch() ;
}

void read(int x[ ][10] , int nr , int nc)
{
int r , c ;
for(r=0 ; r<nr ; r++)
for(c=0 ; c<nc ; c++)
scanf("%d" , &x[r][c]) ;
}

void multiply(int a[ ][10] , int b[ ][10] , int c[ ][10] , int l , int m , int n)
{
int i , j , k ;
for(i=0 ; i<l ; i++)
for(j=0 ; j<n ; j++)
{
c[i][j]=0 ;
for(k=0 ; k<m ; k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j] ;
}
}

void transpose(int x[ ][10] , int y[ ][10] , int nr , int nc)
{
int r , c ;
for(r=0 ; r<nr ; r++)
for(c=0 ; c<nc ; c++)
y[c][r]=x[r][c] ;
}

void show(int y[ ][10] , int nr , int nc)
{
int r , c ;
for(r=0 ; r<nr ; r++)
{
for(c=0 ; c<nc ; c++)
printf("%d " , y[r][c]) ;
printf(" ") ;
}
}

==============

Expected output

=============