Question

1) Write a program that: program starts; declares and initializes to zero an integer variable score;...

1) Write a program that: program starts; declares and initializes to zero an integer variable score; declares and initializes to zero a float variable total; uses a for loop statement with integer x initialized to 1 going 5 iterations hence x<5 incremented by 0; input from keyboard integer score in range of 0 to 99; print on new line input value; adds score to total; switch x equals 3 break default x++; after exiting the loop calculate the average score; print on new line the average 6 wide to two decimal places and number of iterations; just before ending print on new line “Program finished!” then terminate program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the complete code as per the requirements. It is well explained inside the code using comments.

#include <iostream>
#include <iomanip>

using namespace std;

// main function
int main()
{
   // declare score variable and initialize it
   int score = 0;

   // declare and initialize the variable total
   float total = 0;

   // loop for 5 iterations
   int x = 1;
   for (; x < 5; x = x + 0)
   {
       // prompt for score and read it
       cout << "Enter the score [0-99]:" << endl;
       cin >> score;

       // add score to total
       total += score;

       // check for the value of x
       switch (x)
       {
       case 3:
           goto end_loop;   // break loop on x = 3
           break;
       default:
           x++;
       }
   }

end_loop:;   // to break the for loop from inside switch

   // calculate average score
   float average = total / x;

   // print average and number of iterations
   cout << "Average: " << setw(6) << setprecision(2) << fixed << average << ", Iterations: " << x << endl;

   cout << "Program finished!" << endl;

   return 0;
}

Below is the sample output:

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
1) Write a program that: program starts; declares and initializes to zero an integer variable score;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that: program starts; declares and initializes to 10 an integer variable i; uses...

    Write a program that: program starts; declares and initializes to 10 an integer variable i; uses a do loop; print on new line “loop pass # “ and i; if i = 4 break default decrement i by 1 by stating i once; test condition i > 0; just before ending print on new line “Program finished!” then terminate program.

  • Write a complete program that declares an integer variable, reads a value from the keyboard into...

    Write a complete program thatdeclares an integer variable,reads a value from the keyboard into that variable, andwrites to standard output the square of the variable's value.

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Q.5. (a) Write a program/ pseudo-code with following specs. The program contains an integer variable x...

    Q.5. (a) Write a program/ pseudo-code with following specs. The program contains an integer variable x initialized with value 20. After initializing x the program creates 07 processes using fork(). Each parent adds 10 to x and prints x. (15)

  • Write a C++ program - create a 1-d array. Its data type is integer. It has...

    Write a C++ program - create a 1-d array. Its data type is integer. It has 10 elements. - initialize this array by 10 integer numbers. Five elements of the array are positive, and others are negative. - use a while loop to manipulate the array as below. - If the value of an element is positive, the value is doubled. Otherwise, the value is incremented by 3. - print the new value of each element. - use a for...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

  • Write a complete program that: 1) declares two local integers x and y 2) defines a...

    Write a complete program that: 1) declares two local integers x and y 2) defines a global structure containing two pointers (xptr,yptr) and an integer (z) 3) Declares a variable (mst) by the type of previous structure 4) requests the values of x and y from the user using only one scanf statement 5) sets the first pointer in the struct to point to x 6) sets the second pointer in the struct to point to y 7) uses the...

  • In this assignment you are asked to write a python program to maintain the Student enrollment...

    In this assignment you are asked to write a python program to maintain the Student enrollment in to a class and points scored by him in a class. You need to do it by using a List of Tuples What you need to do? 1. You need to repeatedly display this menu to the user 1. Enroll into Class 2. Drop from Class 3. Calculate average of grades for a course 4. View all Students 5. Exit 2. Ask the...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • 1- Write a program to read 10 numbers and find the average of these numbers. Use...

    1- Write a program to read 10 numbers and find the average of these numbers. Use a while loop to read these numbers and keep track of input numbers read. Terminate the while loop with a sentinel value. 2- Now modify the program to find the maximum of these numbers as well. The program should print the number of elements read as input and run until -1 is entered. (-1 is the sentinel that terminates the loop and the program)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT