a) What is the benefit of the statement temp = number ; for the program? What will happen if we eliminate it and use the variable number instead of temp in the inner while loop? Hint: Run the program with this modification and check the result! b) What is the purpose of the statement reverse = 0; in the program? What will happen if we didn't put it before the inner while loop? Hint: Run the program without this this statement and check the result!

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section5.3: Interactive While Loops
Problem 5E
icon
Related questions
Question
a) What is the benefit of the statement temp = number; for the program? What will happen if we eliminate
it and use the variable number instead of temp in the inner while loop?
Hint: Run the program with this modification and check the result!
b) What is the purpose of the statement reverse = 0; in the program? What will happen if we didn't put it
before the inner while loop?
Hint: Run the program without this this statement and check the result!
Page 6 of 7
Transcribed Image Text:a) What is the benefit of the statement temp = number; for the program? What will happen if we eliminate it and use the variable number instead of temp in the inner while loop? Hint: Run the program with this modification and check the result! b) What is the purpose of the statement reverse = 0; in the program? What will happen if we didn't put it before the inner while loop? Hint: Run the program without this this statement and check the result! Page 6 of 7
Question #2:
The following program reads integer numbers from a file named mumbers.txt. For each number, the program
computes the reverse in order to check whether the number is a palindrome number or not. It then reports the
results into a file named Report.txt (A palindrome is any mumber whose reverse is also same, eg. 1221. 23432, 7.8 are
palindromes, but 23489 is not).
Suppose the file numbers.txt contains the following numbers:
373
11
2552
5
3561
numbers.txt
Study the program carefully and answer the below questions;
Hint: you might run the program before starting answering the questions!
e) Show the output of the program. Assume that the file mumbers tt contains the data shown above
) As the program contains nested while loop, determine how many times the following statements will
be executed: Assume that the file mumbers.r centains the data shown abee
11 infile fopent "numbers.txt", ":
:
- time(s)
2) printf"d is read from the filen", number):
time(s)
3) digit = temp % 10;
time(s)
4) printfd is a palindrome number! n\n", number):
time(s)
g) How many times does the program check the condition of the outer while loop, i.e., status le EOF ?
Assume that the fik umbers r contaias the data shown abers
timels)
Page 4 of 7
Hinclude cstdio.h>
Rincludecstdib.h>
int main(voidi
int number, reverse -0, digit, temp,
FILE "infile, "outfile;
int status:
l/open the input file and check if an error occurred
infile fopen( "numbers.txt", ://open the input file
Minfile NULLI
printf"Error: File 'numbers.tat" not foundln
exit (1
):
:
lopen the output file and check if error occurred
outfile fopent "Report.tt", w:
ifjoutfile NULL
){
printf"Error: File "Report.txt" can not be createdln":
exit (1):
:
Ihead first number from the file
ITo read all the numbers in the file
status - fscanflinfile, "%d", &number);
while(status la EOF
printf"d is read from the file\n", number):
fprintf(outfile, "Nad", number):
/compute the reverse of the number
temp number
reverse 0.
whileltemp OH
digit temp % 10;
reverse reverse 10+ digit
;
temp temp/10;
printf"The reverse is: %d n", reverse):
forintfjoutfile, "Kad", reverse):
/check whether the number is palindrome
if(number reverse)
printfd is a palindrome numberl n\n", number):
fprintfloutfile, "t Palindromen" ):
elsel
printfd is not a palindrome numberl n", number);
fprintfjoutfile, " Non Palindromen"):
Iread another number from the file
status fscanflinfile, "Nd", &number):
/elose the files
fcloselinfile):
feloseloutfle)
return 0
Transcribed Image Text:Question #2: The following program reads integer numbers from a file named mumbers.txt. For each number, the program computes the reverse in order to check whether the number is a palindrome number or not. It then reports the results into a file named Report.txt (A palindrome is any mumber whose reverse is also same, eg. 1221. 23432, 7.8 are palindromes, but 23489 is not). Suppose the file numbers.txt contains the following numbers: 373 11 2552 5 3561 numbers.txt Study the program carefully and answer the below questions; Hint: you might run the program before starting answering the questions! e) Show the output of the program. Assume that the file mumbers tt contains the data shown above ) As the program contains nested while loop, determine how many times the following statements will be executed: Assume that the file mumbers.r centains the data shown abee 11 infile fopent "numbers.txt", ": : - time(s) 2) printf"d is read from the filen", number): time(s) 3) digit = temp % 10; time(s) 4) printfd is a palindrome number! n\n", number): time(s) g) How many times does the program check the condition of the outer while loop, i.e., status le EOF ? Assume that the fik umbers r contaias the data shown abers timels) Page 4 of 7 Hinclude cstdio.h> Rincludecstdib.h> int main(voidi int number, reverse -0, digit, temp, FILE "infile, "outfile; int status: l/open the input file and check if an error occurred infile fopen( "numbers.txt", ://open the input file Minfile NULLI printf"Error: File 'numbers.tat" not foundln exit (1 ): : lopen the output file and check if error occurred outfile fopent "Report.tt", w: ifjoutfile NULL ){ printf"Error: File "Report.txt" can not be createdln": exit (1): : Ihead first number from the file ITo read all the numbers in the file status - fscanflinfile, "%d", &number); while(status la EOF printf"d is read from the file\n", number): fprintf(outfile, "Nad", number): /compute the reverse of the number temp number reverse 0. whileltemp OH digit temp % 10; reverse reverse 10+ digit ; temp temp/10; printf"The reverse is: %d n", reverse): forintfjoutfile, "Kad", reverse): /check whether the number is palindrome if(number reverse) printfd is a palindrome numberl n\n", number): fprintfloutfile, "t Palindromen" ): elsel printfd is not a palindrome numberl n", number); fprintfjoutfile, " Non Palindromen"): Iread another number from the file status fscanflinfile, "Nd", &number): /elose the files fcloselinfile): feloseloutfle) return 0
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning