If we wanted to rewrite the code on page 275 to use while loops instead of do-while loops, what would we have to do differently? Explain. ", n_max);20.status = scanf("%d", &in_val);21.22./*...


If we wanted to rewrite the code on page 275 to use while loops instead of do-while loops, what would we have to do differently? Explain.


275<br>5.8 • The do-while Statement and Flag-Controlled Loops<br>FIGURE 5.14 Validating Input Using do-while Statement<br>1. /*<br>Returns the first integer between n min and n max entered as data.<br>Pre : n_min <= n_max<br>2.<br>3.<br>4.<br>Post: Result is in the range n min through n_max.<br>* /<br>5.<br>6.<br>int<br>7.<br>get<br>_int (int n_min, int n_max)<br>8.<br>{<br>9.<br>int<br>in_val,<br>/* input - number entered by user<br>* /<br>10.<br>/* status value returned by scanf<br>* /<br>status;<br>11.<br>char<br>skip_ch;<br>/* character to skip<br>* /<br>12.<br>int<br>/* error flag for bad input<br>* /<br>error;<br>13.<br>/* Get data from user until in val is in the range.<br>* /<br>14.<br>do {<br>15.<br>/* No errors detected yet. */<br>16.<br>error = 0;<br>17.<br>/* Get a number from the user. */<br>18.<br>printf(
", n_max); 20. status = scanf("%d", &in_val); 21. 22. /* Validate the number. */ 23. if (status != 1) { /* in_val didn't get a number */ 24. error = 1; scanf("%c", &skip_ch); printf("Invalid character >>%c>>. ", skip_ch); printf("Skipping rest of line.\n"); } else if (in_val < n_min="" ||="" in_val=""> n_max) { 25. 26. 27. 28. 29. error = 1; 30. printf("Number %d is not in range.\n", in_val) ; 31. 32. /* Skip rest of data line. */ 33. do 34. scanf ("%c", &skip_ch); while (skip_ch != '\n'); 35. 36. } while (error); 37. 38. return (in_val); 39. 40. } 12. "/>
Extracted text: 275 5.8 • The do-while Statement and Flag-Controlled Loops FIGURE 5.14 Validating Input Using do-while Statement 1. /* Returns the first integer between n min and n max entered as data. Pre : n_min <= n_max="" 2.="" 3.="" 4.="" post:="" result="" is="" in="" the="" range="" n="" min="" through="" n_max.="" *="" 5.="" 6.="" int="" 7.="" get="" _int="" (int="" n_min,="" int="" n_max)="" 8.="" {="" 9.="" int="" in_val,="" *="" input="" -="" number="" entered="" by="" user="" *="" 10.="" *="" status="" value="" returned="" by="" scanf="" *="" status;="" 11.="" char="" skip_ch;="" *="" character="" to="" skip="" *="" 12.="" int="" *="" error="" flag="" for="" bad="" input="" *="" error;="" 13.="" *="" get="" data="" from="" user="" until="" in="" val="" is="" in="" the="" range.="" *="" 14.="" do="" {="" 15.="" *="" no="" errors="" detected="" yet.="" */="" 16.="" error="0;" 17.="" *="" get="" a="" number="" from="" the="" user.="" */="" 18.="" printf("enter="" an="" integer="" in="" the="" range="" from="" %d="" ",="" n_min);="" 19.="" printf("to="" %d="" inclusive=""> ", n_max); 20. status = scanf("%d", &in_val); 21. 22. /* Validate the number. */ 23. if (status != 1) { /* in_val didn't get a number */ 24. error = 1; scanf("%c", &skip_ch); printf("Invalid character >>%c>>. ", skip_ch); printf("Skipping rest of line.\n"); } else if (in_val < n_min="" ||="" in_val=""> n_max) { 25. 26. 27. 28. 29. error = 1; 30. printf("Number %d is not in range.\n", in_val) ; 31. 32. /* Skip rest of data line. */ 33. do 34. scanf ("%c", &skip_ch); while (skip_ch != '\n'); 35. 36. } while (error); 37. 38. return (in_val); 39. 40. } 12.

Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here