Write a program that will calculate the under-10 total of a number of integers.
ID: 3619821 • Letter: W
Question
Write a program that will calculate the under-10 total of a number of integers. The following example will illustratethe process:
We need to get the under-10 total of 93, 42 and 33.
- Add the individual digits of the first integer together (thus 9+3 = 12)
- If the total is greater than 9 (which it is in this case), repeat the process (thus 1+2=3) until the total is less
than 10. This is the first running total.
- Then repeat the process for the second integer (thus 4+2 = 6).
- Now add the under-10 total of the first integer to that of the second integer and repeat the process if the
running total is greater than 9 (thus 3+6=9).
- Get the under-10 of the third integer (thus 3+3=6) and add to the previous running total and repeat the
process to get a total less than 10 (thus 9+6=15; 1+5=6).
- The under-10 total of the three integers 93, 42 and 33 is therefore equal to 6.
Create an input file called input.dat that contains the following set of integers:
39 88 55 21 90 77 24 67 76 87 35 79 45 93
You can assume that there will be at least 2 integers in the file and that all integers in the file will have two
individual digits. Your program needs to read one integer at a time from the file, and then use the operators of C++
(e.g. +, - , /, %, which ever is applicable) to separate the digits and do the calculations. Write the running totals to an
output file called under10.dat. The last integer in the output file will therefore be the under-10 total of all the
integers in the input file.
Hint: If you divide a two-digit integer by 10, the result will be the first digit, e.g. 28 / 10 = 2. If you use the modulus
operator (%) on a two-digit integer and the value 10, the result will be the second digit, e.g. 28 % 10 = 8. You
now have the digits 2 and 8 separated, and can add them together. The sum in this example is 10, which is
more than 9, so we repeat the process. 10 / 10 = 1, giving the first digit, and10 % 10 = 0, giving the second
digit. Now 1 + 0 = 1, which is a valid under-10 total.
For the above input file the output file should be:
3 1 2 5 5 1 7 2 6 3 2 9 9 3
Please help me about the syntax codes for the program
Explanation / Answer
#include <fstream>using namespace std;
int ComputeSum(int num);
int main()
{
ifstream inFile("input.dat");
ofstream outFile("under10.dat");
int input, total = 0;
while(inFile.good())
{
if(total > 0)
outFile << ' ';
inFile >> input;
total += ComputeSum(input);
if(total > 9)
total = ComputeSum(total);
outFile << total;
}
return 0;
}
int ComputeSum(int num)
{
int sum = num / 10 + num % 10;
if(sum > 9)
sum = sum / 10 + sum % 10;
return sum;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.