# dividing two matrices in c

Page 1 of 1

## 4 Replies - 5467 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=b3510547afd0f9d02aeeb1c512d5ef23&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 slaveforthewave

• New D.I.C Head

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: 4887
• Posts: 15,264
• 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

• New D.I.C Head

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;
}

```

Was This Post Helpful? 0

### #4 jimblumberg

Reputation: 4887
• Posts: 15,264
• 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

• New D.I.C Head

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?
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}