0

This is the question

/* code segment */
for (k=0; k<n; k++) 
{
  for (i=0; i<n; i++) 
   {
     r = a[i][k];
     for (j=0; j<n; j++) 
     {
       c[i][j] += r + b[k][j];
     }/* end for…j)
   }/* end for…i)
}/* end for…k)
/* code segment ends */

I have modified the code like this

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int main()
{

    int **A,**B,**C;
    int i,j,r,k;
    int n=2;

    A=(int**)malloc(n*sizeof(int*));

    for(i=0;i<n;i++)
    {
        A[i]=(int*)malloc(n*sizeof(int));
    }
B=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
{
B[i]=(int*)malloc(n*sizeof(int));
}
C=(int**)malloc(n*sizeof(int*));
for(i=0;i<n;i++)
{
C[i]=(int*)malloc(n*sizeof(int));
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
A[i][k]=rand()%10;
}
}

for(i=0;i<n;i++)
{
for(k=0;k<n;k++)
printf("%d ",A[i][k]);
printf("\n");
}

for(k=0;k<n;k++)
{
for(j=0;j<n;j++)
{
B[k][j]=rand()%10;
}
}
for(k=0;k<n;k++)
{
for(j=0;j<n;j++)
printf("%d ",B[k][j]);
printf("\n");
}
for (k=0; k<n; k++) {
for (i=0; i<n; i++) {
r = A[i][k];
for (j=0; j<n; j++) 
{
C[i][j] += r + B[k][j];
}
}
}
printf("The required matrix is");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",C[i][j]);
printf("\n");
}

It runs but not producing the desired output.

2
  • Please edit your question and properly format your code. It's easy to do, select the code and click the {} icon in the format bar. And perhaps explain "the desired output" in more detail. Mar 28, 2017 at 11:42
  • You don't initialise the content of C, so the C[i][j] initially have arbitrary values (maybe initialise them to 0?). I have no idea what the desired output is, but if you're doing matrix multiplication, that would require some * instead of + in the line where you += to C[i][j].
    – Blauelf
    Mar 28, 2017 at 11:53

0

You must log in to answer this question.

Browse other questions tagged .