C++ Code. Write several functions to manipulate vectors of doubles or strings: 1
ID: 3692528 • Letter: C
Question
C++ Code.
Write several functions to manipulate vectors of doubles or strings:
1. A void function ReadVector that has a single vector parameter vector & inVect. The function should prompt the user to enter the number of double elements to be read in, to read in those values, and to stores them in inVect.
2. A void function PrintVect that as a vector parameter const vector& dataVect and an integer value maxLine that prints out all of the values of dataVect with maxLine values per line.
3. A void function ReadSentence that has an vector parameter vector & Sentence that prints out a prompt telling the user to enter a sentence terminated by a '.' reads in those values, and stores them in Sentence. Note that the character '.' should be removed from the string it appears in.
4. A void functions reverseWords with a vector parameter const vector & wordVect and another vector parameter vector & backwards that will return the values of wordVect in reverse order.
Explanation / Answer
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
void ReadVector(vector<double> &inVal)
{
int size;
cout << "Enter the number of double elements to be read in: ";
cin >> size;
double element;
cout << "Enter elements in vector ";
for (int i = 0; i < size; ++i)
{
cin >> element;
inVal.push_back(element);
}
cout << endl;
}
void PrintVect(const vector<double> &dataVect, int maxLine)
{
int t = 0;
for (int i = 0; i < dataVect.size(); ++i)
{
if(t == maxLine)
{
t = 0;
cout << endl;
}
cout << dataVect[i] << " ";
t++;
}
cout << endl;
}
void ReadSentence(vector<string> &Sentence)
{
string s;
cout << "enter a sentence terminated by a '.' ";
getline(cin,s);
for (int i = 0; i < s.size()-1; ++i)
{
Sentence.push_back(s[i]);
}
}
void reverseWords(const vector<string> &wordVect, vector<string> &backwards)
{
for (int i = 0; i < wordVect.size(); ++i)
{
backwards[i] = wordVect[i];
}
reverse(backwards.begin(),backwards.end());
}
int main()
{
vector<double> inVal;
ReadVector(inVal);
int maxLine = 2;
PrintVect(inVal,maxLine);
vector<string> Sentence;
ReadSentence(Sentence);
vector<string> backwards;
reverseWords(Sentence,backwards);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.