C++ C++ Problem 1: The program will ask the user to enter a sequence of scores b
ID: 3855078 • Letter: C
Question
C++ C++
Problem 1: The program will ask the user to enter a sequence of scores between 0 and 100, inclusive. The program then will print out the stats about these scores, including: the number of scores, the maximum score, the minimum score and the average score.
Requirements for Solution to Problem 1: The students should implement the following two functions to solve this problem. void get_scores(vector &v); // get integer scores from keyboard and store in v. // we assume that the user will input scores in range // of 0 to 100, inclusive. User enter a negative // number to stop the input and end the function void print_stats(vector &v); // print out the stats of data that are stored in v // includes: max, min, total number of data, // and the average
Problem 2: The program will ask the user to enter a sentence. The program will then display a message to indicate if this sentence is a palindrome. The following sentence is a palindrome: A nut for a jar of tuna. The white space and non English letters are not counted. The case difference is ignored.
Requirements for Solution to Problem 2: The students should implement the following function to solve this problem bool is_palindrome(string sentence); // return true if the sentence is a palindrome; // false otherwise
Note: Highlight ones are the inputs Enter scores, enter -1 to stop: 87 98 65 78 93 -1 There are total 6 scores The highest score is: 98 The lowest score is: 65 The average score is: 84.8333 Enter a sentence: A nut for a jar of tuna "A nut for a jar of tuna" is a palindrome Do you want to try another sentence? Y/N: y Enter a sentence: Hello world "Hello world" is not a palindrome Do you want to try another sentence? Y/N: n Program ended with exit code: Note: 1. If there are no scores entered, the output message shall just say that there is no score. (I didn't include this case in sample run) are ignored it. 2. To determine if a sentence is palindrome or not, it is NOT case sensitive. Also all spacesExplanation / Answer
NOTE: this is first part of your program. due to lack of time i am not able to complete second part and will share the code by EOD.
Problem.1 Code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// function declaration
void get_scores(vector<int> &);
void print_stats(vector<int> &);
int main()
{
vector<int> v;
// initializing a default vector array as we dont know the current size of elements user going to pass
v.resize(100);
get_scores(v);
print_stats(v);
return 0;
}
void get_scores(vector<int> & v)
{
int input = 0;
int i = 0;
cout << "Enter score, enter -1 to stop:" << endl;
// reading until user enters -1
while(1){
cin >> input;
if (input == -1) break;
// storing in vector array
v[i++] = input;
}
// reallocating the vector array as per number of elements user provided
v.resize(i);
}
void print_stats(vector<int> & v)
{
int high, low;
double avg, sum = 0;
high = low = v[0];
for(unsigned int i = 0; i < v.size(); i++) {
// identifying max score
if(v[i] > high)
high = v[i];
// identifying lowest score
if(v[i] < low)
low = v[i];
// summing the scores
sum += v[i];
}
avg = sum / v.size();
cout << " There are total " << v.size() << " scores";
cout << " The highest score is: " << high;
cout << " The lowest score is: " << low;
cout << " The average score is: " << avg;
}
Execution and output:
Enter score, enter -1 to stop:
87
98
65
78
88
93
-1
There are total 6 scores
The highest score is: 98
The lowest score is: 65
The average score is: 84.8333
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.