# dividing two matrices in c

Page 1 of 1

## 4 Replies - 3598 Views - Last Post: 26 October 2013 - 12:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=332487&amp;s=88c8b5372e533bd60d51fdcd5f03f7d3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 slaveforthewave

Reputation: 1
• Posts: 37
• 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

Reputation: 4652
• Posts: 14,584
• 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

### #3 slaveforthewave

Reputation: 1
• Posts: 37
• 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;
}

```

### #4 jimblumberg

Reputation: 4652
• Posts: 14,584
• 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

### #5 slaveforthewave

Reputation: 1
• Posts: 37
• 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?