Purpose: The purpose of this project is to help the students to reinforce the kn
ID: 3858758 • Letter: P
Question
Purpose: The purpose of this project is to help the students to reinforce the knowledge from Chapter 8 of the textbook. Objectives: 1. Review the top-down design 2. Understand and apply string methods, such as length, operator [], etc 3. Understand and apply functions in cctype library 4. Understand and apply vector methods, such as push_back, at, operator [], etc Problem Description: The project will solve two problems. 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
Explanation / Answer
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void get_scores(vector<int> &v){
int input;
cout << "Enter scores (to stop enter a negative score) ";
do{
cin >> input;
if (input >= 0)
v.push_back(input);
} while (input >= 0 );
}
void print_stats(vector<int> &v){
int max;
int min;
int sum;
double avg;
max = v.at(0);
min = v.at(0);
sum = 0;
for (int i = 0; i<v.size(); i++){
if (max < v.at(i))
max = v.at(i);
if (min > v.at(i))
min = v.at(i);
sum = sum + v.at(i);
}
cout << "Count: " << v.size() << endl;
cout << "Maximum: " << max << endl;
cout << "Minimum: " << min << endl;
cout << "Average: " << sum/v.size() << endl;
}
void check_palindrome(){
string line;
string data1, data2;
data1 = "";
data2 = "";
getline(cin,line);
cout << "Enter the sentence ";
getline(cin,line);
for(int i=0; i<line.size(); i++){
if (isalpha(line.at(i))){
data1 = data1 + (char)tolower(line.at(i));
}
}
for(int i=line.size()-1; i>=0; i--){
if (isalpha(line.at(i)))
data2 = data2 + (char)tolower(line.at(i));
}
if (data1 == data2){
cout << "It is a palindrome ";
}
else {
cout << "It is not a palindrome ";
}
}
int main(){
vector<int> v;
get_scores(v);
print_stats(v);
check_palindrome();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.