4 Replies - 971 Views - Last Post: 26 October 2013 - 12:08 PM Rate Topic: -----

#1 slaveforthewave  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-November 08

dividing two matrices in c

Posted 26 October 2013 - 11:13 AM

so i wrote a code to populate two matrices with random numbers. then i am supposed to multiply, dividem add and subtract the matrices. i have it all printed out and everything looks good... except my divide function isn't working to give me a fraction of a number. matrix[a] and matrix[b] are int and matrix[c] is a double.

here is the code i have to do the math and print

void divMatrix(int a[][3],int b[][3],double c[][3])
     {int i,j;
     for(i=0; i<3; i++)
     for(j=0; j<3; j++)
     c[i][j] = (a[i][j])/(b[i][j]);
     return;
     }



and
               divMatrix(a,b,c);
               printf("\ndivided matrix:\n");               
               for(i=0;i<3;i++)
                   {for(j=0;j<3;j++)
                        printf("%7.1f ",c[i][j]);
                        printf("\n");
                   }



Is This A Good Question/Topic? 0
  • +

Replies To: dividing two matrices in c

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: dividing two matrices in c

Posted 26 October 2013 - 11:20 AM

Look at the following snippet:
	     c[i][j] = (a[i][j])/(b[i][j]);

Do you realize that you're doing integer math in this snippet? After the integer calculation is preformed you then implicitly cast the result to your floating point type.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 slaveforthewave  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-November 08

Re: dividing two matrices in c

Posted 26 October 2013 - 11:53 AM

i'm about 2 months into to an entry level c class. we have not covered what it is you just said. i googled it and got this. which works, not really sure how or why but it works haha. thanks.

void divMatrix(int a[][3],int b[][3],double c[][3])
     {int i,j;
     for(i=0; i<3; i++)
     for(j=0; j<3; j++)
     c[i][j] = (double) (a[i][j])/(b[i][j]);
     return;
     }


Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4099
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: dividing two matrices in c

Posted 26 October 2013 - 11:58 AM

It works because when you cast one side of your calculation to the double it forces floating point calculation. Remember with integer calculations you can't have fractions 1/2 will yield zero.

Also you should be insuring that the divisor in your calculation (b[][]) is never zero.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 slaveforthewave  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 29-November 08

Re: dividing two matrices in c

Posted 26 October 2013 - 12:08 PM

random numbers are defined as:
a[i][j]=rand()%49+1;



that should take care of zeros.

now, if i wanted find the numbers that were odd and print them for each part... would i just create a loop to divide by two and use the modulus operator to see if it =/0 then print the digits that falls into? would that be the most efficient way?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1