1,105,534 Community Members

C++ Division Remainder Help

Member Avatar
Ghost
Posting Whiz
354 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

When using C++, how do you find the REMAINDER of two numbers and store it in an integer.

I think the answer is:

X % Y = Int Z; :idea:

Please help.

Thanks :lol: ,
C++

Member Avatar
Chainsaw
Posting Pro in Training
434 posts since Jun 2004
Reputation Points: 12 [?]
Q&As Helped to Solve: 13 [?]
Skill Endorsements: 1 [?]
 
0
 

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

Member Avatar
Ghost
Posting Whiz
354 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
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,&quotient,&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++

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
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,&quotient,&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.

Member Avatar
Ghost
Posting Whiz
354 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

You're correct.

Thank you SO much,
C++

wangchung
Newbie Poster
1 post since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

*remainder = denominator % numerator;

should be

*remainder = numerator % denominator;

http://www.cprogramming.com/tutorial/modulus.html

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article