Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets

1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets use -9999) is entered. The program should then correctly report the number of non-negative scores entered and the arithmetic mean (average) of those scores

2. Demonstrate your programs behavior in response to errors on input (that is, show that it faithfully rejects improper input such as alphabetic characters, decimal points, and commas). Here are some specific examples demonstrating the requirements of your program.

3. Pseudo-code:

Initialize a Sum and Counter to zero

Initialize a loop Done variable to false, or zero

Provide a brief console output to describe what the program will do

While not Done

Attempt to read the next Input, checking for an integer read failure

If this attempt to read an integer failed then

Clear the input stream status, then ignore the current character on the stream

Repeat the attempt to read an integer until successful

If the Input integer is the sentinel value

Set the loop Done variable to true, or nonzero

Else if the Input integer is non-negative then

Add one to the Counter

Add the Input to the Sum

Repeat until Done

Compute and display the number of inputs and the average (report 0 average for 0 inputs)

Example Output:

Ill compute the average of your inputs:

30 45 -9 one junk 100.0 more -1000.8 -9999

Results: 5 non-negative integers entered, average 36.6

// Comment: the non-negative integers read were (30, 45, 100, 0, 8)

Please work off of the code I have provided below.

#include using std::cout; using std::cin; using std::endl;

int main() { int a(123456789); const int NUM(4);

cout << " Test the stream, the clear() and ignore() "; cout << "Enter " << NUM << " integers: "; for (int i = 1; i <= NUM; i++) { while (!(cin >> a)) // While we have an error reading... { cin.clear(); // Remove the error state in stream cin.ignore(); // Read but ignore the next character } cout << "\t integer(" << i << ") was: " << a << endl; } system("pause"); return 0; }

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

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

How do parent-infant attachment bonds formpg12

Answered: 1 week ago