I submitted an assignment but I was told it was wrong. I have till the end of the day to get it right but I'm working and my deadline is creeping up. So I'm hoping you guy can help me out.

Assignment

Write a program with two funtions: main and remainder. The remainder function will work exactly the same as the % operator in C++, that is:

cout << remainder(10, 3) ;

will print 1, the same as the result of 10%3.

The limitation is that you cannot use the % operator to implement fucntion remainder You will have to write the code from the scratch so that your function remainder produces the same results as %.

Your function main should just ask the user to input two numbers, and then it will call remainder to calculate and print the result of the remainder of the first number divided by the second.

Incase any body wanted to laugh at my answer this was what I submitted

#include  <cmath>
#include <iostream>
using namespace std;
int main ()
{double num1 , num2;
cout << "Enter number 1: ";
cin >> num1;
cout << "Enter number 2: ";
cin >> num2 ;
cout <<"Your remainder is: " << remainder(num1, num2);
    system("PAUSE");
    return EXIT_SUCCESS;
    return 0 ;
}

where's your remainder function?

In order to call remainder(num1,num2)

You need to have your function

which should be

#include <iostream>
using namespace std;

int remainder(int x, int y)
{ 
int result;
// your code to get remainder
return result;
}

int main()
{
// code that you just posted

return 0;
}

Edited 5 Years Ago by ninjatalon: n/a

Exactly what he said you have no remainder function. What do I need I don't have my text book with me and I'm a little lost.

int remainder(int x, int y){
int i;
for(i = 1; y*i < x; i++);
return (x - y*(i-1));
{
int remainder(int x, int y){
int i;
for(i = 1; y*i < x; i++);
return (x - y*(i-1));
{

Can I take this and slap it into my code with appropriate adjustments? I really hate to have people do my assignment for me and it's in my best interest to do this but the online course and my school leaves something to be desired,

The modulo operator simple finds what the remainder is after dividing by a number. So with integer division 20 / 3 = 6 but 6 * 3 = 18 so there has to be a remainder of 2. you get the remainder by subtracting the divisor times the answer from the number.

//  find the whole part
20 / 3 = 6

// multiply to find out how short we are
6 * 3 = 18

// subtract to get the remainder
20 - 18 = 2

This should help you create a solid function.

Making progress but my math is off if I enter 10 and 3 I should get 1 but i'm getting 2???

#include <iostream>
using namespace std;
 
int remainder(int x, int y)
{ 
int r;
r = x - ( x / y * y ) ;
return (r);
}
 
int main()
    
{int x , y, z;
int remainder = z;
cout << "Enter number 1: ";
cin >> x;
cout << "Enter number 2: ";
cin >> y ;
cout <<"Your remainder is: " << z;

 
 system("PAUSE");
 return EXIT_SUCCESS;
return 0;
}

You need to call your function in main(). Line 14 just creates a local integer value called remainder, which bears no relation to your function. Since z is not initialized, it comes with whatever junk is in there at the time.

Your function call will look like remainder(x,y) , take a crack at figuring out where to put it, and post back.

This seems correct I got rid of the Z (14) line and added your remainder(x,y) to the cout statment at the end....I think I'm good right?

#include <iostream>
using namespace std;
 
int remainder(int x, int y)
{ 
int r;
r = x - ( x / y * y ) ;
return (r);
}
 
int main()
 
{int x , y, z;
remainder (x,y);
cout << "Enter number 1: ";
cin >> x;
cout << "Enter number 2: ";
cin >> y ;
cout <<"Your remainder is: " << remainder (x,y);
 
 
 system("PAUSE");
 return EXIT_SUCCESS;
return 0;

Hello Lokril,
try this....

#include<iostream.h>
#include<conio.h>
int remainder(int x,int y)
{int d,e;
 d=x/y;
 e=x-(y*d);
 return(e);
}
int main()
{.
 .
 .// code that you just posted
}

Edited 5 Years Ago by Arbus: n/a

This seems correct I got rid of the Z (14) line and added your remainder(x,y) to the cout statment at the end....I think I'm good right?

#include <iostream>
using namespace std;
 
int remainder(int x, int y)
{ 
int r;
r = x - ( x / y * y ) ;
return (r);
}
 
int main()
 
{int x , y, z;
remainder (x,y);
cout << "Enter number 1: ";
cin >> x;
cout << "Enter number 2: ";
cin >> y ;
cout <<"Your remainder is: " << remainder (x,y);
 
 
 system("PAUSE");
 return EXIT_SUCCESS;
return 0;

Yes, except line 14 isn't doing anything and is therefore not necessary.

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