Computer Concept & Programming In C - Unit IV - 2

Q.3 Write a program in C that accepts N x N matrix as input and prints transpose of this matrix.                                                                    (AKTU. 2008-09)
Ans. # include <stdio.h>
# include <conio.h>
void main ( )
{
int a[10] [10];
int i, j, n;
clrscr ( );
printf (“\n Enter single dimension of square Matrix”);
scanf (“%d”, & n);
printf (“\n Now enter the elements of the matrix one by one”, n);
for (i = 0, i < n; i++)
for (j = 0; j < n; j++)
{
printf (“\n Enter a [%d] [%d]”, i, j);
scanf (“%d \”, & a [i] [j]);
};
for (i = 0, i < n; i++)
for (j = 0; j < n; j++)
{
int temp = 0;
temp = a[i] [j];
a[i] [j] = temp;
};
printf (:\n The elements of transpose matrix are:\n”);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
printf (“\n a [%d] [%d] = “, i, j, a[i] [j]);
getch ( );
}

Q.4 Write a program in C to multiply TWO matrices of N x N dimension.                                             (AKTU. 2008-09)
Related Questions -
Q. The two martices A and B  of floating number are given. Write the program in C language to find the multiplication of the transpose of A and B i.e., . (AKTU. 2013-14)
Ans. The program for the matrix multiplication is as shown below –
# include <stdio.h>
# include <conio.h>
# define N 2
void main ( )
{
double a[N] [N];
double b[N] [N];
double c[N] [N];
int i, j, k;
clrscr ( );
printf (“\n Enter elements of a:\n”);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
   printf (“\n Enter a[%d] [ %d]”, i, j);
      scanf (“%lf”, & a[i][j]);
    };
printf (“\n Enter elements of b:\n”);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
    {
printf (“\n Enter b[%d][%d]”, i, j);
scanf (“\n %lf”, & b[i][j]);
     };
// first make sure that c is blank.
for (i = 0; i < N; i++);
for (j = 0; j < N; j++);
c[i][j] = 0.0;
// performing matrix multiplication and
// storing the product in c
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
for (k = 0; k < N; k++)
     c[i][j] + = a[i][k] * b[k][j];
printf (“\n The product of the two matrices a and b is as follows:\n”);
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
printf (“\n c[%d][%d] = % lf ”, i, j, c[i][j]);
printf (“\n Press any key to exit.”);
getch ( );
}