In C++ code, write a single program with a menu function that will solve the bel
ID: 3914086 • Letter: I
Question
In C++ code, write a single program with a menu function that will solve the below two problems and match the program run screen below. The functions must have comments explaining what each part is accomplishing.
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
If there are no scores entered, the output message shall just say that there is no score.
The following two functions should be used:
void get_scores(vector<int> &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<int> &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. Make
sure your program will take a sentence as input not just a string with no space in it.
The following function should be used:
bool is_palindrome(string sentence);
// return true if the sentence is a palindrome;
// false otherwise
Program run screen:
1. Score Stats
2. Palindrome Test
3. Quit
Please enter your choice: 1
Enter scores, 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
Do you want to try another set of scores? Y/N: y
Enter scores, enter -1 to stop:
-1
There is no scores.
Do you want to try another set of scores? Y/N: n
1. Score Stats
2. Palindrome Test
3. Quit
Please enter your choice: 2
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
1. Score Stats
2. Palindrome Test
3. Quit
Please enter your choice: 3
Program ended with exit code: 0
Explanation / Answer
#include<iostream>
#include<vector>
using namespace std;
//method to read scores and store them in vector
void get_scores(vector<int> &v)
{
int n;
cout<<"Enter scores, enter -1 to stop:";
while(1)
{
cin>>n;//reading input
if(n==-1)//then break
break;
v.push_back(n);//adding to vector at the back
}
}
//to print the stats of data in the vector..
void print_stats(vector<int> &v)
{
int len = v.size();//finding length of the vectors..means number of elements in it
int max,min,sum=0;
if(len>0)
{
int i=0,k;
max=min=v.at(0);
for(i=0;i<len;i++)//finding max.min and sum
{
k=v.at(i);
sum=sum+k;//finding sum
//updating max value
if(k>max)max=k;
//updating min value
if(k<min)min=k;
}
//finding average
double av =sum*1.0/len;
//printing stats
cout<<"There are total "<<len<<" scores ";
cout<<"The highest score is: "<<max<<endl;
cout<<"The lowest score is: "<<min<<endl;
cout<<"The average score is: "<<av<<endl;
}
else//if vector is empty
{
cout<<"There is no scores. ";
}
}
///method checks whether the given string is palindrome or not ...
bool is_palindrome(string sentence)
{
string s=sentence;
int len=0;
//finding length of the sentence
while(s[len]!='')len++;
//matching string from both sides...to check palindrome or not..
//example.... ->maam<-... matching both sides...if both are alphabets...
int i=0,j=len-1;
while(i<=j)
{
// cout<<i<<" "<<j<<endl;
char c=s[i];
char d =s[j];
if(c<65 || c>123 || (c>91 && c<97))//means it is not an alphabet
{
j++;
}
else if(d<65 || d>123 || (d>91 && d<97))//means it is not an alphabet
{
i--;
}
else//if both are alphabets...
{
//cout<<c<<" "<<d<<endl;
if(c<92)
{
c=c+32;//making it to lower case
}
if(d<92)d=d+32;//making it to lower case
if(c!=d)
break;
}
i++;
j--;
}
//cout<<i<<" "<<j<<endl;
if(i<=j)return false;
return true;
}
int main()
{//testing method
int c=0;
while(c!=3)
{
cout<<"1. Score Stats 2. Palindrome Test 3. Quit " ;
cout<<"Please enter your choice:";
cin>>c;
if(c==1)
{
char d='y';
while(1)
{
vector<int> v;
get_scores(v);
print_stats(v);
cout<<"Do you want to try another set of scores? Y/N:";
cin>>d;
if(d=='n'||d=='N')break;
}
}
else if(c==2)
{
char d='y';
while(1)
{
string s;
cout<<"Enter a sentence:";
getline(cin,s);//reading string..
getline(cin,s);//reading string.
if(!is_palindrome(s))
cout<<"""<<s<<"""<<" is not a Palindrome ";
else cout<<"""<<s<<"""<<" is a Palindrome ";
cout<<"Do you want to try another set of scores? Y/N:";
cin>>d;
if(d=='n'||d=='N')break;
}
}
}
return 0;
}
output:
1. Score Stats
2. Palindrome Test
3. Quit
Please enter your choice:1
Enter scores, 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
Do you want to try another set of scores? Y/N:y
Enter scores, enter -1 to stop:-1
There is no scores.
Do you want to try another set of scores? Y/N:n
1. Score Stats
2. Palindrome Test
3. Quit
Please enter your choice:2
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 set of scores? Y/N:y
Enter a sentence:hello world
"hello world" is not a Palindrome
Do you want to try another set of scores? Y/N:n
1. Score Stats
2. Palindrome Test
3. Quit
Please enter your choice:3
Process exited normally.
Press any key to continue . . .