Question

1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately...

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:

I’ll 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 <iostream>
using std::cout;
using std::cin;
using std::endl;

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

   cout << "\n Test the stream, the clear() and ignore()\n";
   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;
}

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

#include <iostream>
#include<list>
#include<string>
using namespace std;

int main() {
   //initialize sum and counter to zero
   int sum(0), count(0);
   string number;
   bool done = false;
   list<int> scores;
  
   while(done!=true)
   {
       cin >> number;
       if (stoi(number) >= 0)
       {
           scores.push_back(stoi(number));
           sum += stoi(number);

       }
       else if (stoi(number) < 0 && stoi(number) != -9999)
       {
           //cin.ignore();
           cin.clear();
           continue;
       }
       else
       {
           done = true;
           continue;
       }
       //cin.ignore();
       cin.clear();
       ++count;

   }
   cout << "Non negetive integers read were {";
   list<int>::iterator it = scores.begin();
   while (it != scores.end())
   {
       cout << (*it) << " ";
       it++;
   }
   cout << "}\n";
   cout << "Results: "<<count<<" non-negative integers entered, average " << (float)sum / count << endl;
}

/*Output
30 45 -9 100.0 0 8 -1000.8 -9999
Non negetive integers read were {30 45 100 0 8 }
Results: 5 non-negative integers entered, average 36.6
*/

Add a comment
Know the answer?
Add Answer to:
1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately...
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
  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • Write a program that reads a list of integers ending with a negative, and that then...

    Write a program that reads a list of integers ending with a negative, and that then outputs that list in reverse. After the negative that ends the list, a character indicates what character should separate the output integers. If the input is: 3 5 2 - 1 * the output is: 2*5*3 LAB ACTIVITY 7.16.1: LAB: Reverse a list, with separating characters 2 / 10 main.cpp Load default template... 6 7 8 int numbers [100],i=0, num, size; char character; cin>>num;...

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • Question text Find four problems with this program: ------------------------------------------- using namespace std; // this program inputs...

    Question text Find four problems with this program: ------------------------------------------- using namespace std; // this program inputs three integers, determines if negative, positive, or zero int main() { cout << "Enter an integer" << endl; cin >> input;    if (input < 0); cout << "The number is negative."  endl; else if (input > 0) cout << "The number is positive" << endl; else cout << "The number is zero" << endl;    return 0; } programm c++ please and thank you

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • The program has errors. Identify and make the necessary changes to make the program work. Remember...

    The program has errors. Identify and make the necessary changes to make the program work. Remember to follow the rules of functions and arrays. Here is the expected output Total = 20 Average = 15 Area = 150 Area = 70 Enter a value: 10 Value 1 entered = 10 Enter an integer: 20 Value 2 entered = 20 Enter an integer: 30 Value 3 entered = 30 .................................................................. #include <iostream> using namespace std; void total(int, int, int); double average(int...

  • Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

    Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here {    float avg; // variable declaration    int num,sum=0,count=0; /* 'count' is the int accumulator for number of...

  • C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl;...

    C++ with comments please! // numberverifier.cpp #include <iostream> #include <exception> using std::cout; using std::cin; using std::endl; #include <cmath> #include <cstring> class nonNumber : public exception { public: /* write definition for the constructor */ -- Message is “An invalid input was entered” }; int castInput( char *s ) { char *temp = s; int result = 0, negative = 1; // check for minus sign if ( temp[ 0 ] == '-' ) negative = -1; for ( int i...

  • C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list...

    C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Ex: If the input is: 4 hello zoo sleep drizzle z then the output is: 200 drizzle To...

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
ADVERTISEMENT