% operator

Pages: 123
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace 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;
}

Or what do you mean?
If you subtract the second number from the first then you're left with the remainder.

For example, let's take 10 / 7.

10 - 7 = 3, which is the remainder (the full answer would be 1 r 3).

Be sure not to return a negative remainder, though. A check should be put in to make sure there's a positive remainder before returning any value.

EDIT: This isn't going to work for larger numbers. See Gaminic's hints.
Last edited on
Shadow123

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 ...
By definition, if
a = b*c + d

then
d = a - b*c


You have 'a' and 'c'; the rest is trivial.
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;
}
Last edited on
That's division. It's correct for (10,9) and some other completely random examples.
I guess this is what you are looking for.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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.
Last edited on
Perman, I cant get your code to compile, says cprintf not declared in the scope.
This probably isnt right either...

#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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}
Last edited on
I copied your code so check it out. Notice that I moved quotient and reminder so they are global and can be seen from both main() and Reminder().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace 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;
}
I tried it on my compiler and after fixing a couple of spell errors it works fine. Good luck my friend
Final code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream.h>

using namespace 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;
}
You could just get as close to the second number as possible and put the difference in

100 / 33

33 * 1 = 33;
Less than 100, keep going.

33 * 2 = 66;
Less than 100, keep going.

33 * 3 = 99;
Less than 100, keep going.

33 * 4 = 132;
More than 100, use last number.

100 - 99 = 1;

100 / 33 = 3 remainder 1;

Coding that is the hard part. Use a for loop and change the number and result before multiplying the next one.

1
2
3
4
5
6
int total = 0;
int remainder = 0;

for(int c = 0; b * c <= a; c++)  //yes 0, what if it's 0 / 27
  total = b * c;
remainder = a - total;
cokane:

This is a little more complex version. It call the functions in you cout calls. No need for global variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace 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;
}
Last edited on
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.
Correct me if I'm wrong.

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.
Pages: 123