2 Replies - 1664 Views - Last Post: 22 August 2013 - 12:27 AM Rate Topic: -----

#1 Kartikcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-August 12

An expected output in matrix_multiplication

Posted 18 March 2013 - 04:38 AM

This is my matrix multiplication program...
I attached the program and the output in this mail...
In output c[0][1] value is coming .... I dont know why...?

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: An expected output in matrix_multiplication

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,437
  • Joined: 25-December 09

Re: An expected output in matrix_multiplication

Posted 18 March 2013 - 07:44 AM

#include<stdio.h>
int main()
{
    int i,j,x,y,z,p,q,m,n;
    float a[3][3],b[3][3],c[3][3];
    printf("\n Enter the row and column ");
    scanf("%d %d",&x,&y);
    for(i=0;i<=x;i++)
    {
        for(j=0;j<=y;j++)
        {
             printf("\n Enter value for a[%d][%d]",i,j);
             scanf("%f",&a[i][j]);
        }
    }

    printf("\n Enter the row and column ");
    scanf("%d %d",&p,&q);
    for(i=0;i<=p;i++)
    {
        for(j=0;j<=q;j++)
        {
             printf("\n Enter value for b[%d][%d]",i,j);
             scanf("%f",&b[i][j]);
        }
    }
    if(y==p)
    {
         m=x;n=q;
    }
    else
    {
        printf("Cannot perform div... wrong dimension");
    }

    for(i=0;i<=m;i++)
    {
        for(j=0;j<=n;j++)
        {
            for(z=0;z<=m;z++)
            {
                    c[i][j]=c[i][j]+(a[i][z]*b[z][j]);
            }
        }
    }
    printf("The value of m is %d and n is %d",m,n);
    for(i=0;i<=m;i++)
    {
        for(j=0;j<=n;j++)
        {
            printf("\n c[%d][%d]= %f",i,j,c[i][j]);
        }
            printf("\n");
    }
    return 0;
}



Program output.

Quote

Enter the row and column 1
1

Enter value for a[0][0]4

Enter value for a[0][1]4

Enter value for a[1][0]4

Enter value for a[1][1]4

Enter the row and column 1
1

Enter value for b[0][0]2

Enter value for b[0][1]2

Enter value for b[1][0]2

Enter value for b[1][1]2
The value of m is 1 and n is 1
c[0][0]= 16.000000
c[0][1]= 7900026394273209600000000000000000.000000

c[1][0]= 16.000000
c[1][1]= 16.000000

Process returned 0 (0x0) execution time : 111.078 s
Press any key to continue.


I have a couple of suggestions. First you need to validate the user input. What happens if the user enters a value larger than the size of your arrays?

Next use meaningful variable names. Trying to follow the logic with the use of all those single variable names is tedious.

Next I suggest you run this program thru your debugger. Set a breakpoint and single step thru the program watching the variables as you step.

I really suspect that you are going to find your loops are the problem. Anytime I see a loop control section using the operator<= rings bells in my mind. In C/C++ arrays start at zero and end at size - 1 so using operator<= will quite often result in accessing the arrays out of bounds.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Kartikcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-August 12

Re: An expected output in matrix_multiplication

Posted 22 August 2013 - 12:27 AM

Yes..
The problem is with the garbage value at c matrix.. After initializing c matrix to zero i got the correct output..
Thank you ... !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1