I need to write a program that has two functions main and remainder. The limitation is that I cant use the % operator to implement function remainder I have to write the code from the scratch so that the function remainder produces the same results as %.
I wonder if a compound operator could be used %= ?
The 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.
#include <iostream>
usingnamespace std;
int main(void) {
int a = 7;
int b = 3;
int full = 0;
int remainder = 0;
full = static_cast<int>(a/b);
remainder = a % b;
cout << "Full: " << full << ", Remainder: " << remainder << endl;
return 0;
}
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 %.
Sorry, I think I stuck on my decent knowledge of the english language :(
I don't understand what's the problem is. Perhaps anyone other could answer you ...
I have no idea if the code I posted is right, but I emailed it to my teacher to see if he likes it or not.
#include <iostream>
using namespace std;
int showRemainder (int a, int b){
int answer = a/b;
return answer;
}
int main () {
cout << showRemainder(10, 9);
return 0;
}
#include <conio.h>
int iValue;
int iReminder;
void Modulus(long a, long b);
// Modulus number
void Modulus(long a, long b)
{
iValue = a / b; // 2
iReminder = a % b; // 37
}
// Real number
void Reminder(long a, long b)
{
int iTemp;
iValue = a / b; // 2
a*= 100; // 13700
iTemp = a / b; // 274
iReminder = (iTemp - (iValue * 100)); // 74
}
int main()
{
Modulus(137, 50);
cprintf("Value: %d Reminder: %d\r\n", iValue, iReminder);
Reminder(137, 50);
cprintf("Value: %d Reminder: %d\r\n", iValue, iReminder);
getch();
}
Depending on how many decimals you need for the reminder is set by the mul/div factor. In above example it's 100 which gives you 2 decimal after the decimal point. If you don't need deal with big numbers you can use int's in the function call. Anyhow... Int's in most compilers are defined as 32 bits anyway. Normally you have to declare it as a short int to get 16 bits.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int quotient;
int remainder;
cout << "Enter a divisor! \n";
cin >> a;
cout << "Enter a dividend! \n";
cin >> b;
quotient = a/b;
remainder = a%b;
cout << "The quotient of those numbers is " << quotient << endl;
cout << "The remainder of those numbers is " << remainder << endl;
return 0;
}
Definitely not. The whole purpose of your exercise is to provide a function that returns the remainder of two numbers without using the modulus operator.
Give you a pretty easy way of working out the remainder earlier in the post.
Hope this helps. If I seem reluctant to give code that's because I am. This looks a lot like a homework question and you'll not benefit at all by lumping someone else's code into an IDE and hoping it compiles.
cokane:
Did you remember to include conio.h? Another way is you can use cout instead. I use cprinf because I'm so use to it and it do all the conversions for you. Goes back to the old 'C' days.
Last you can just import the function called Reminder() or make your own. It works fine. It will give you the real reminder and not modulus. 137 / 50 = 2.74. Call the function Reminder(137, 50)
will in the global variables give you: iValue = 2 and iReminder = 74. Like I said before depending what your multiply factor is will give you how many decimals. 10 = 1, 100 = 2 and so on.
PS It compiled fine on my compiler Borland C++ ver 5.03.
I did a minor correction to the code. You don't need iTemp.
int iValue;
int iReminder;
void Reminder(long a, long b)
void Reminder(long a, long b)
{
iValue = a / b; // 2
a*= 100; // 13700
iReminder = a / b; // 274
iReminder = (iTemp - (iValue * 100)); // 74
}
int main()
{
// Here you make your own code for the input to function Reminder()
Reminder(a, b); // a and b are your vales
// Here you make your own code to print out iValue and iReminder
// That should do it
return 0;
}
#include <iostream>
usingnamespace std;
int quotient;
int remainder;
void Reminder(long a, long b);
void Reminder(long a, long b)
{
iValue = a / b; // 2
a*= 100; // 13700
reminder = a / b; // 274
reminder = (reminder - (quotient * 100)); // 74
}
int main()
{
int a;
int b;
cout << "Enter a divisor! \n";
cin >> a;
cout << "Enter a dividend! \n";
cin >> b;
Reminder(a, b);
cout << "The quotient of those numbers is " << quotient << endl;
cout << "The remainder of those numbers is " << remainder << endl;
return 0;
}
#include <iostream.h>
usingnamespace std;
int quotient;
int remainder;
void Reminder(long a, long b);
void Reminder(long a, long b)
{
quotient = a / b; // 2
a*= 100; // 13700
remainder = a / b; // 274
remainder = (remainder - (quotient * 100)); // 74
}
int main()
{
int a;
int b;
cout << "Enter a divisor! \n";
cin >> a;
cout << "Enter a dividend! \n";
cin >> b;
Reminder(a, b);
cout << "The quotient of those numbers is " << quotient << endl;
cout << "The remainder of those numbers is " << remainder << endl;
cin >> a;
return 0;
}
#include <iostream>
usingnamespace std;
int Quotient(long a, long b);
int Remainder(long a, long b);
// function quotient
int Quotient(long a, long b)
{
return = a / b;
}
// Function remainder
int Remainder(long a, long b)
{
int quotient;
int remainder;
quotient = a / b;
a*= 100;
remainder = a / b;
return (remainder - (quotient * 100));
}
int main()
{
int a;
int b;
cout << "Enter a divisor! \n";
cin >> a;
cout << "Enter a dividend! \n";
cin >> b;
Remainder(a, b);
cout << "The quotient of those numbers is " << Quotient(a, b) << endl;
cout << "The remainder of those numbers is " << Remainder(a, b) << endl;
cin >> a;
return 0;
}
If you only need two functions (main and remainder), then I would suggest using my code in a void function. Put c and remainder in an address of variable. If you do this, then it will be easy.
Just a little remainder. The remainder returned are wit 2 digits so if you get 20 back as remainder then it's = X.20. If you get 4 back = X.04. Hopes that help the understanding of it.
Where do you use all this in the real world you may ask. It's used in small embedded microcontroller systems where there is not enough space and time to do floating point calculations. Doing like above you can make pretty good precision calculation on small code at high speed. Me myself have been working with microcontroller for the last 30 years and learned all the tricks. In a PC world this would never been used.
137 / 50 = 2.74 is what you say makes your process work. 2 remainder 74 is your answer.
If I'm not wrong, then you need to take another look at that. 2 * 50 + 74 = 174. If that's true, and that's exactly how your code is supposed to work, then I'm missing something.