Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Z.UOCX CSc 10 A- labJ-2.docx (128 KB) | A* Alternative formats Page 1 > of 7 O ZOOM In the previous in-lab activity, you learned

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
Z.UOCX CSc 10 A- labJ-2.docx (128 KB) | A* Alternative formats Page 1 > of 7 O ZOOM In the previous in-lab activity, you learned about several uses of pre-test WHILE loops. One of those was a variation of the While Loop known as a "counter-controlled" loop. It turns out that this is such a common situation in programming that most programming languages (and Flowgorithm) have "shorthand" for such loops. The most important thing to understand is that this is just shorthand for the counter-controlled loop you learned in the previous exercise. It works exactly the same way -- it just saves you some typing. Part 1 - Valid Values In a previous exercise, we checked to see if an input value represented a valid exam score. An algorithm we used was: Input score If score 100 Then Display "Not valid" Else Display "Valid Score" End If In today's first exercise, we'll consider the same problem, but instead of just displaying whether the score is valid or not, we'll force the user to enter a valid score. But first, we'll do it wrong! Here's the algorithm we'll try. Input Score If score 100 Then Display "Sorry that was invalid. Try again!" Input Score End If Display score & " is valid"Page of 7 O 2 1. Create a new flowchart and implement that exact algorithm. Be sure to echo the value of the input in the final output operation. It sort of looks reasonable. If they enter an invalid score, we'll tell them they made an error and what the error was (as part of the prompt for the next input), and then we'll make them re-do the input. Let's try it and see. 2. Execute the flowchart. For your first input, enter 90 (a valid score). Well, that should have worked. It should have printed "90 is valid". 3. Now execute again. This time, enter an invalid score (101). Well, that looks good, too -- it said we had entered an invalid score and asked us to do it again. So obey the rules -- enter a valid score (90). It worked again. Printed "90 is valid", as it should have for 90. Hmm. Maybe I'm wrong. Maybe it really is a good solution to the problem. But let's try it just one more time. 4. Execute, and this time, enter the invalid value (101). When prompted to re-enter, enter another invalid value, -1. Uh oh! It says that -1 is a valid score. Back to the drawing board! What we need is a way to make them re-enter if they enter an invalid score, but then to continue to make them re-enter, time after time, until they finally enter a valid score. We need a loop Part 2 - Corrected Data Validation Algorithm Look carefully. The loop says that as long as the value of Score is invalid, we'll keep on looping (and within the body of the loop, re-entering the value for Score). Input score While score 100 Display "Sorry that was invalid. Try again!" Input score End While Display Score & "is valid" 1. Start a new flowchart. 2. Start with the input. 3. Below the input, add a "While Loop". 4. Open the While Loop to enter the condition. Use exactly the same condition you used in the previous version. 5. On the True path, put the new input operation, being sure to prompt the user with a message telling him/her that they made an error. 6. In the False path (i.e., the path that will be taken when the loop condition is not true), put the final output operation. 7. Execute. Try all of the possibilities we tried before. 8. Execute and enter a valid score the very first time. 9. Then execute again, entering an invalid score first, followed by a valid one.Page 3 of 7 3 10. Then execute again, entering an invalid score, followed by another invalid score. That's the case that didn't work right with the If version. But it works just fine now. We're not getting out of this loop until we finally enter a value that makes the While condition false, that is, until we enter a valid score. 11. Try several more invalid scores. 12. Finally, enter a valid score. The loop exits (if you did it right) and the correct message is displayed. Part 3 - Another Example of Data Validation Look at the following flowchart. Make sure you see how the approach used in this flowchart is exactly the same as the approach you just learned in the preceding example in this handout. Main Integer answer Output "Are you happy? (1 - yes, 2 - roj" Input answer Fake answer 2 Output "Please try again. Are you happy? (1 - yea, 2 - no) Input answer False True answer = 1 Output "I'm sorry to hear that" Output *Glad to hear it" End Is this correct? If not, what is wrong? Part 4 - Input Validation One of the problems with the last example is that the user can enter an incorrect score. Bowling scores, naturally, can't be less than 0, but will never exceed 300. We can create nice like below:Page 300 Display "Enter a valid bowling score" Input Score End While Display Score & "is valid" Look carefully. The loop says that as long as the value of Score is invalid, we'll keep on looping (and within the body of the loop, re-entering the value for Score). 1. Start a new flowchart. (You can try to re-use the existing one, but it might be more trouble than it is worth.) 2. Start by adding an input. 3. Below the input, select the "While Loop". 4. Open the While Loop to enter the condition. Use exactly the same condition you used in the previous version. We'll use mostly pre-test loops. Pre-test means that the condition will be evaluated before we enter the loop -- look at the pseudocode -- if the condition is true, we enter the loop, displaying the error message and getting another input. But if the condition is false, even on the very first time we reach the loop (in other words, if the user's first entry is valid), we just skip the entire loop! 1. In the True path, put the new input operation, being sure to prompt the user with a message telling him/her that they made an error (keep it to one line). 2. In the False path (i.e., the path that will be taken when the loop condition is not true), put the final output operation. 3. Execute. Try all of the possibilities we tried before. 4. Execute and enter a valid score the very first time. 5. Then execute again, entering an invalid score first, followed by a valid one. 6. Then execute again, entering an invalid score, followed by another invalid score. That's the case that didn't work right with the IF version. But it works just fine now. We're not getting out of this loop until we finally enter a value that makes the WHILE condition false, that is, until we enter a valid score. 7. Try several more invalid scores. 8. Finally, enter a valid score. The loop exits (if you did it right) and the correct message is displayed. Part 5 - Post Test Validation Here is a post-test loop used to solve the same problem. DO INPUT Score WHILE Score 300 DISPLAY "Valid"(128 KB) | A* Alternative formats Page 5 > of 7 5 Notice that this approach looks a bit simpler - there's only one Input operation. The loop body contains just that one statement - just as in the pre-test version. But there is no need to have an additional Input operation before the loop, as there was in the pre-test version. When this gets executed, it will always enter the loop the first time - the Input statement in the loop body gets executed. Then it reaches the condition - if the condition is true (the Score is out of range), we return to the top of the loop (the "DO") and execute the loop again. When (eventually) the user enters a valid score, the condition is false, and we leave the loop, going on to the DISPLAY statement outside the loop. 1. Create a new Flowgorithm flowchart for this algorithm. To create the post-test loop, select WHILE loop icon, just as you did for pre-test loops. But when you open it up to enter the condition, choose the radio button that says "Post-Test". Put the Input operation within the loop, i.e., on the True path. For the input prompt, say "Enter a bowling score." The False path is the exit to the loop - just as in the pre-test approach. Put the DISPLAY on the False path. 2. Execute the flowchart. You'll see that the first thing that happens is that you are asked to enter a score. Enter a valid bowling score. Execution continues to the condition, and since the condition is false, we leave the loop. 3. Do the same thing again, but this time, enter two or three invalid scores before entering a valid score. You should get a clear picture of just how this loop operates. Well, it certainly looks like post-test loops are fine for this kind of problem, and they are a bit easier to write, since there is one less step (as compared to the pre-test loop version). But I don't think this works well at all! 4. Execute the flowchart and enter a couple of invalid scores. Look carefully at the message you get each time - it just asks you to "Enter a bowling score. The earlier (pre-test) version would have told you that you had made a mistake when you entered an invalid score. Try to figure out a way to provide an error message. But, of course, you don't want to tell them they made a mistake for the first entry - because they haven't made a mistake yet. Here's a modification to the algorithm which will solve this problem. Do Input Score If Score 300 THEN Display an error message End If While Score 300 Display "Valid" 5. Add the logic of that algorithm to your flowchart. (That is, add an IF with an Output of an error message on the true path, nothing on the false path.) Of course, the IF must follow the Input operation. 6. Execute the flowchart, and enter a valid score the first time. That should work nicely - you won't see an error message, and you get the ending message saying the score is valid. . Execute again - this time, enter several invalid scores before entering a valid one. That works, too. You get the error message for every invalid score.This now works just like the pre-test version we had belore - an error message for every invalid input Problem solved! 8. But do you still think the post-test approach is simpler than the pro-test one? (it you answered "yes' to that question, you'll probably be one of those programmers who uses post-test loops most at the time. I'm not. I almost never use one.) Part 6 - Putting It All Together How we have used input validation to make sure the bowling score ls correct. and we can make a nice sentinel- based loop to compute an average. Wow. we rock! Now, let's combine both concepts together to make the best bowling score program ever! Your new program should use the sentinel loops from the first part of the activity to enter scores until the user enters -1. At the same time. make sure that the bowling spores are valid using input validation. This might take a little time to work on and get ltjust right. See if you can gure it out without asking for help and show your true computer skills

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Leading Strategic Change In An Era Of Healthcare Transformation

Authors: Jim Austin ,Judith Bentkover ,Laurence Chait

1st Edition

3319808826, 978-3319808826

Students also viewed these General Management questions

Question

What are the benefits of using positive self-talk? (p. 151)

Answered: 1 week ago

Question

implmenet echo client server using C

Answered: 1 week ago