Question

In: Computer Science

Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...

Sentinel While Loop Lab

Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following:

  • Prompts the user to enter a grade or a -1 to quit.
  • IF the user entered a -1 THEN
    • Display a message that the User is done entering grades
  • ELSE
    • Count each grade as it is entered.
    • Compute a running total of the grades entered.
  • END IF
  • After the user enters the sentinel of -1, calculate the average of the grades entered.
  • When computing the average, make sure that there is no division by zero. Check to make sure the counter is not equal to zero.
  • Make sure the sentinel is not counted as a grade.
  • Print out a message stating the average of the grades entered.

Include the following:

  1. Use a sentinel-controlled WHILE loop that uses a -1 as the sentinel to exit the loop.
    • Before counting the grades, initialize the counter to zero before the while loop begins.
  2. While within the body of the ELSE:
    • Within the WHILE loop:
      • Prompt the User to enter a grade or a -1 to quit.
      • IF the input is not a -1 THEN
        • Count the grade.
        • Compute a running total of these grades as they are entered.
      • END IF
    • After the WHILE Loop is done executing, compute the average of all the grades entered. Remember to check to make sure you don't perform a division by zero.

NOTE 1:

  • DO NOT use the "return 0" code, end, break, or exit to force an exit from within your IF/ELSE structure.  Declare all variables within the data declaration section.
    • Let the program progress to the end of the main function.
    • The end of the main function is the only place that the “return 0” should be placed.
    • A SWITCH statement is the only place that the break command should be used.
  • DO NOT use the "continue" statement.

NOTE 2:

1. Declare all variables within the data declaration section of each class and method.  (-.1)
2   Do not get input on the same line as a variable declaration.  
(-.1)

3. Do not place an equation for computation on the same line as declaring a variable.  (-.1)
4. Do not place an equation for computation on the same line as an output statement.  (-.1)

Solutions

Expert Solution

Answer: Hey!! dear student kindly find your solution below.Let me know if any issue. Thanks.

Copy to code: This code asks for grades from user and continue asks until sentinel value is entered.After entering sentinel value,user display a message and average of the grades.

You didn't mention any language so C++ used here if any change you want just comment me in the comment box.

#include<iostream>
using namespace std;
int main()
{
   int counter = 0;//declaration of varaibles
   double grade,sum = 0,avg;
  
   while(grade!=-1)//whle loop until sentinel value is entered
   {
       cout<<"Enter grades: ";//prompts for grades
       cin>>grade;
      
       if(grade>0)//if grade not less than 0
       {
           counter++; //increment of counter
           sum = sum+grade;//sum of grades
       }
       else
       if(grade==-1)//if sentinel vlaue is entered
       {
           cout<<"User is done entering grades";//display message
       }
   }
   if(counter!=0)//check counter if not equal to 0
   {
       avg = sum/counter;
   }
   cout<<"\nAverage of grades: "<<avg;
   return 0;  
}

Snapshot of code and output:

Java Code:

import java.util.*;
import java.util.Scanner;
class Main
{
public static void main(String[]args)
{
int counter = 0;//declaration of varaibles
double grade = 0,sum = 0,avg= 0.0;
Scanner sc = new Scanner(System.in);
  
while(grade!=-1)//while loop until sentinel value is entered
{
System.out.println("Enter grades: ");//prompts for grades
grade = sc.nextDouble();
  
if(grade>0)//if grade not less than 0
{
counter++; //increment of counter
sum = sum+grade;//sum of grades
}
else
if(grade==-1)//if sentinel vlaue is entered
{
System.out.println("User is done entering grades");//display message
}
}
if(counter!=0)//check counter if not equal to 0
{
avg = sum/counter;
}
System.out.println("Average of grades: "+avg);
}
}


Related Solutions

Summary In this lab, you use a counter-controlled while loop in a Java program provided for...
Summary In this lab, you use a counter-controlled while loop in a Java program provided for you. When completed, the program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. The data file contains the necessary variable declarations and some output statements. Instructions Ensure the file named Multiply.java is open. Write a counter-controlled while loop that uses the loop control variable to take on the values 0 through 10. Remember to initialize...
Summary In this lab, you write a while loop that uses a sentinel value to control...
Summary In this lab, you write a while loop that uses a sentinel value to control a loop in a C++ program that has been provided. You also write the statements that make up the body of the loop. The source code file already contains the necessary variable declarations and output statements. Each theater patron enters a value from 0 to 4 indicating the number of stars the patron awards to the Guide’s featured movie of the week. The program...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
Explain the similarities and differences of counter-controlled iteration and sentinel-controlled iteration. Also, give an example of...
Explain the similarities and differences of counter-controlled iteration and sentinel-controlled iteration. Also, give an example of how each would be used in a computer program.
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT