#4 Some of the code is given already We just need to expand it Work on the voidP
ID: 3744575 • Letter: #
Question
#4 Some of the code is given already We just need to expand it Work on the voidProgram part APTER 8/ Strings and Vectors Repeat Practice Program 1 except use the class string to extract not the cstring functions 2. 3. Write a program that inputs a first and last name, separated by a sp into a string variable. Use the string functions to output last initial. Embed your code into a do-while loop At the end of the loo ask the user if he or she would like to repeat the program. Input the choice into a char using cin. If the character is 'y then repeat the program otherwise exit. Beware of the pitfall with newlines when cin is mixed with getline. user's 4. Write a function named firstLast2 that takes as input a vector of integers. The function should return true if the vector starts or ends with the digit 2. Otherwise it should return false. Test your function with vectors of dif- ferent length and with the digit 2 at the beginning of the vector, end of the vector, middle of the vector, and missing from the vector. 5. Write a function named swapFront Back that takes as input a vector of inte- gers. The function should swap the first element in the vector with the last element in the vector. The function should check if the vector is empty to prevent errors. Test your function with vectors of different length and with varying front and back numbers. Do Practice Program 7.4 except change the program to use vectors of strings instead of arrays of strings. 6. 7. Write a program that inputs two string variables, first and 1ast, each of which the user should enter with his or her name. First, convert both strings to all lowercase. Your program should then create a new string that contains the full name in Pig Latin with the first letter capitalized for the first and last name. The rules to convert a word into Pig Latin are as follows: If the first letter is a consonant, move it to the end and add "ay"' to the end. If the first letter is a vowel, add "way" to the end For example, if the user inputs "Erin" for the first name and "Jones" the last name, then the program should create a new string v Erinway Onesjay" and print it. with the t ROGRAMMING PROJECTS rogramming Projects require more problem-solving than Practice Pro grams and Visit www.myprogramminglab.com sually be s . an olved many different waysExplanation / Answer
//Hey. Here is your solution.
//Please do upvote if it helps you.
//Thank you.
#include <iostream>
#include <vector>
using namespace std;
bool firstLast2(vector<int> tempVector);
void main()
{
// Intialising vector
// int arrInt[] = { 16,2,77,29,34,5,9,2 };
int arrInt[100];
int n;
cout << "Enter number of elements in a vector - ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << " Enter Positive Elemnt 1 - ";
cin >> arrInt[i];
}
vector<int> vector1(arrInt, arrInt + sizeof(arrInt) / sizeof(int));
// Displaying vector elements
cout << "Displaying all vector elements ";
int count = 0;
for (std::vector<int>::iterator itr = vector1.begin(); itr != vector1.end(); ++itr)
{
if (count < n)
std::cout << ' ' << *itr;
else
break;
count++;
}
if (firstLast2(vector1))
cout << " Vector starts or ends with 2";
else
cout << " Vector does not starts or ends with 2";
int x;
cin >> x;
}
bool firstLast2(vector<int> tempVector)
{
int first, last;
vector<int>::iterator itr = tempVector.begin();
first = *itr;
cout << " First Element of Vector = " << *itr;
for (int i = 1; i < tempVector.size(); i++)
{
if (*itr > 0)
itr++;
}
last = *(itr-1);
cout << " Last Element of Vector = " << *(itr-1);
if (first==2 || last==2)
return true;
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.