Problem 1: The program will ask the user to enter a sequence of scores between 0
ID: 3776018 • Letter: P
Question
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 nuber of scores, the maximum score, the minimum score and the average score.
Requirements: Implemtn the follwoing functions to solve the problem.
void get_score(vector<int> &v);
// get integer scores from keyboard and store in v.
// we assume that the user will input scores in range of 0 10 100, inclusive. User enter a negative number to stop the input and end the function
void print_stats(vector<int> &v);
//print out the stats of the data that are stored in v
//includes: max, min, total number of data and average
Second step: The program will ask the user to enter a sentence. The program will then display a message to indicate if this sentence is a palindarome. The following sentence is a plaindrome. A nut for a jar of tuan> The white space and non English letters are not confused. The case difference is ignored
requirements: It should implement the following function to solve this problem
bool is_palindrome(string sentence);
//return true if the sentence is a palindrome;
//false otherwise
Run the demo to see how the probram should run.
Explanation / Answer
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
bool is_palindrome(string sentence);
void get_scores(vector<int> &v);
void print_stats(vector<int> &v);
string reverse(const string& s);
string make_lower(const string& s);
void swap(char& v1, char& v2);
string remove_punct(const string& s, const string& punct);
int main()
{
cout << "Enter scores between 0 and 100. Enter a negative to stop." << endl;
vector<int> u;
get_scores(u);
print_stats(u);
cout << "Enter a sentence to tell if it is a palindrome: " << endl;
string str;
getline(cin, str);
if ( is_palindrome(str) )
cout << """ << str + "" is a palindrome!!" << endl;
else
cout << """ << str + "" is NOT a palindrome!!" << endl;
return 0;
}
void swap(char& v1, char& v2)
{
char temp = v1;
v1 = v2;
v2 = temp;
}
string reverse (const string& s)
{
int start = 0;
int end = s.length();
string temp(s);
while (start < end)
{
end--;
swap(temp[start], temp[end]);
start++;
}
return temp;
}
string make_lower(const string& s)
{
string temp(s);
for (int i = 0; i < s.length(); i++)
temp[i] = tolower(s[i]);
return temp;
}
string remove_punct(const string& s, const string& punct)
{
string no_punct;
int s_length = s.length();
int punct_length = punct.length();
for( int i = 0; i < s_length; i++)
{
string a_char = s.substr(i,1);
int location = punct.find(a_char, 0);
if(location < 0 || location >= punct_length)
no_punct = no_punct + a_char;
}
return no_punct;
}
bool is_palindrome(const string& s)
{
string punct ( ".;:.?!'" ");
string str(s);
str = make_lower(str);
string lower_str = remove_punct(str, punct);
return (lower_str == reverse(lower_str));
}
void get_scores(vector<int> &v)
{
int input;
while (input > 0)
{
cin >> input;
v.push_back(input);
}
}
void print_stats(vector<int> &v)
{
int max = 0, min = 101, total = 0;
double average;
cout << "Scores are: ";
for(unsigned int i = 0; i < v.size(); i++)
{
cout << v[i] << ", ";
if ( v[i] > max )
{
max = v[i];
}
if (v[i] > 0)
{
if (v[i] < min)
{
min = v[i];
}
total = total + v[i];
}
}
cout << "Stats entered: " << v[] << endl;
average = total / (v.size() - 1);
cout << " " << endl;
cout << "Stats entered: " << << endl;
cout << "Max = " << max << endl;
cout << "Min = " << min << endl;
cout << "Total = " << total << endl;
cout << "Average = " << average << endl;
}