Not Yet Answered # C++ Division Remainder Help

Chainsaw 12 Discussion Starter Ghost Dave Sinkula 2,398 wangchung

0

like you said, "int z = x % y" gives the remainder of x / y int z. Just turn your expression around.

0

The compiler said your code was incorrect :twisted: , so I thought i would give you the orriginal question.

Here it is:

Write the definition of a function divide that takes four argumentsª and returns no valueª. The first two argumentsª are of typeª intª. The last two argumentsª argumentsª are pointers to intª and are set by the function to the quotient and remainder of dividing the first argumentª by the second argumentª. The function does not return a valueª.

The function can be used as follows:

intª numerator=42, denominator=5, quotient, remainder; divide(numerator,denominator,"ient,&remainder); /* quotient is now 8 */ /* remainder is now 2 */

I think the answer is:

void divide(int numerator,int denominator,int* quotient, int* remainder){

*(quotient=&numerator/&denominator);

*(remainder=&denominator%&numerator);

}

But, when I run i through the compiler, these three messages appear:

In function `void divide(int, int, int *, int *)':,

invalid operands `int *' and `int *' to binary `operator /',

invalid operands `int *' and `int *' to binary `operator %'

Please help

C++

0

I think the answer is:

void divide(int numerator,int denominator,int* quotient, int* remainder){

*(quotient=&numerator/&denominator);

*(remainder=&denominator%&numerator);

}

Divide *values*, not *memory locations*:

```
#include <stdio.h>
void divide(int numerator, int denominator, int* quotient, int* remainder)
{
*quotient = numerator / denominator;
*remainder = denominator % numerator;
}
int main(void)
{
int numerator=42, denominator=5, quotient, remainder;
divide(numerator,denominator,"ient,&remainder);
printf("numerator = %d\n", numerator);
printf("denominator = %d\n", denominator);
printf("quotient = %d\n", quotient);
printf("remainder = %d\n", remainder);
return 0;
}
/* my output
numerator = 42
denominator = 5
quotient = 8
remainder = 5
*/
```

Whitespace makes code more easily readable.

0

While this post is very old, there is a mistake:

*remainder = denominator % numerator;

should be

*remainder = numerator % denominator;

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

`enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};`

And i construct my array using a voidptr ...

I'm new to python, for my project I need to send mail from python.. I'm getting the following error can someone please help me how to get out of it..

```
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for ...
```

i am trying to create a library management system which is supposed to be used to store/keep infp for the library and the library members( book title, author, name and picture for borrower and other stuffs). The app has 6 buttons, delete, save, add,previsous,next, and upload( for upload ...