Hi everyone, I need help with this assignment my teacher gave me. I'm very lost right now. If someone could help me out and explain how to do this code, I would highly appreciate it! Thanks in advance!
Here is the assignment:
Write a program to read one 3-digit integer number (from 100 to 999) from the console.
If you consider each digit of the number represented by the letters A, B, and C; we can label the number
as ABC.
Your program should:
Make sure the user enters the appropriate type of number
o i.e. an integer
o And fits in the right range
o You must implement this with a wrapper function (hint: it could be helpful for your
wrapper function to return success or failure)
Strip each digit A, B, and C from the number
o Example: User enters 452: A should be 4; B should be 5; C should be 2;
o Hint#1: what’s the integer division of 452/100?
o Hint#2: the modulo operator (%) can also be quite handy!
Form the numbers ABC, BCA, and CAB and store them to variables named accordingly.
o From example above: ABC should be 452; BCA should be 524; CAB should be 254;
Find the remainders of each of the numbers ABC, BCA, and CAB when divided by 11. Store these
remainders to variables named X, Y, and Z respectively.
Find and store the sums X+Y, Y+Z, and Z+X. For discussion sake, we’ll refer to each of these sums
as P, R, and Q respectively.
o If any of these sums (P, R or Q) is an odd number, add 11 to it unless that would make
the result (after adding 11) larger than 20-- in that case, decrease the number by 11.
Basically, the final sum must be an even number but less than 20
o If any of P, R, or Q are already even, leave it be.
Finally, divide each of the final sums (P, R, and Q after they’ve been pushed into the 0-20 range
and made even) in half.
Display the output of P, R, and Q
o What values do you get? Anything special/interesting about these numbers?
Your program should provide a formatted printout of all the computed numbers and sums. Your
program should display a welcome message and prompt the user to enter the required numeric input.
Your program should also prevent the user from entering anything but a 3-digit number, and continue to
ask the user for an appropriate input until they enter one.
OK so lets start out with the first goal of the program
Make sure the user enters the appropriate type of number
o i.e. an integer
o And fits in the right range
o You must implement this with a wrapper function (hint: it could be helpful for your
wrapper function to return success or failure)
Your while loop almost accomplishes this but you must put a ! operator in front of every condition like so while (!(cin >> ABC) || !(ABC >= 100) || !(ABC <= 999))
Now you can move on to the next goal:
Strip each digit A, B, and C from the number
o Example: User enters 452: A should be 4; B should be 5; C should be 2;
o Hint#1: what’s the integer division of 452/100?
o Hint#2: the modulo operator (%) can also be quite handy!
This one gives you some hints that you should use to try and solve it. The first hint helps you get the first digit
1 2
int A = 452/100
cout << A // A = 4
So if the program divides the 3 digit number by 100 you will get the first digit.
For the second and third digits try using the % operator which will return the remainder after dividing e.g. 452 % 10 = 2. The second and third digit can be found with the % operator. Let me know if you can get this working.
Sorry about the late reply... So I have been working on this assignment for a day now and I'm almost done. Thank you McNo for the help! However, I still need some help with the last few parts! Here is my code as of right now.
int X = ABC % 11;
int Y = BCA % 11;
int Z = CAB % 11;
cout << "When each number is divided by 11, the remainders are: \nABC(X)= " << X << "\nBCA(Y)= " << Y
<< "\nCAB(Z)= " << Z << endl;
int P = X + Y;
int Q = Y + Z;
int R = Z + X;
cout << "When the remainders are added, the numbers are: \nP(X+Y)= " << P << "\nQ(Y+Z)= " << Q << "\nR(Z+X)= " <<
R << endl;
int D = P % 2;
int E = Q % 2;
int F = R % 2;
if (D == 1) {
cout << "Since P is odd, we add 11 to it: \nP = " << P + 11 << endl;
}
else if (D == 0) {
cout << "Since P is even, we leave it be! \nP = " << P << endl;
}
if (E == 1) {
cout << "Since Q is odd, we add 11 to it: \nQ = " << Q + 11 << endl;
}
else if (E == 0) {
cout << "Since Q is even, we leave it be! \nQ = " << Q << endl;
}
if (F == 1) {
cout << "Since R is odd, we add 11 to it: \nR = " << R + 11 << endl;
}
else if (F == 0) {
cout << "Since R is even, we leave it be! \nR = " << R << endl;
}