Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Question Given a vector: vector names: //names gets filled with: \"GeoRGE\",

ID: 3860213 • Letter: C

Question

C++ Question

Given a vector: vector names: //names gets filled with: "GeoRGE", "Ng", "AlEkS", "jeNni", //"Stu", "anne", "SHEREEN" and a function: void name_case(string & n): Write the code necessary to make all the vector's elements name-case'. (Do NOT write the name_case function! It is GIVEN to you!) (Thought-provoker: Does it really matter what name case' means?) Write a function which, when called as 'average (vec, 5, 11)', will return the average of the elements of vec between positions 5 and 11 {inclusive). (You can pick an appropriate base-type for the vector argument...) (Thought-provoker: What happens if the caller asks for the average of an empty/non-existent range?) (Thought-provoker 2: Is this single function as flexible as possible for the caller? Could you easily make it more flexible? How/why not?)

Explanation / Answer

The answers for the questions are given below. Please rate the answer if it helped. Thank you.

question 13

vector<string> names = {"GeoRGE", "Ng", "AlEkS"};
for(vector<string>::iterator it = names.begin(); it != names.end(); ++it)
name_case(*it);

question 14

//assuming start and end are indices of hte elements range.
//The function is flexible to correct the start and end values if they are out of range
double average(vector<double> numbers, int start, int end)
{
if(start > end) //if starting index is greater than ending index
return 0;
  
//check and correct the start and end indices when they are out of range, making hte function flexible
if(start < 0 || start >= numbers.size())
start = 0;
  
  
if(end < 0 || end >= numbers.size())
end = numbers.size() - 1;
  
double avg = 0;
  
for(vector<double>::iterator it = numbers.begin() + start; it <= numbers.begin() + end; ++it)
avg += *it;
  
int n = end - start + 1;
avg /= n;
return avg;
}

=================================

To demonstrate the above codes work correctly and as intended, the following code is given ......

#include <iostream>
#include <vector>
#include <cctype>
#include <cstring>
using namespace std;

void name_case(string &n);
void display(vector<string> names);
double average(vector<double> numbers, int start, int end);
int main()
{
  
vector<string> names = {"GeoRGE", "Ng", "AlEkS"};

cout << "names before calling name_case " << endl;
display(names);

for(vector<string>::iterator it = names.begin(); it != names.end(); ++it)
name_case(*it);
  
cout << "names after calling name_case " << endl;
display(names);
  
vector<double> num = { 1 , 2, 3, 4 , 5, 6};
cout << "average from index 1 to 3 = " << average(num, 1, 3) << endl;
cout << "average from index -1 to 10 (auto correction) = " << average(num, -1, 10) << endl;
}

void name_case(string &n)
{
for(int i = 1; i < n.length(); i++)
n[i] = tolower(n[i]);
n[0] = toupper(n[0]);
}

void display(vector<string> names)
{
for(int i = 0; i < names.size(); i++)
cout << names[i] << " " ;
  
cout << endl;
}

//assuming start and end are indices of hte elements range.
//The function is flexible to correct the start and end values if they are out of range
double average(vector<double> numbers, int start, int end)
{
if(start > end) //if starting index is greater than ending index
return 0;
  
//check and correct the start and end indices when they are out of range, making hte function flexible
if(start < 0 || start >= numbers.size())
start = 0;
  
  
if(end < 0 || end >= numbers.size())
end = numbers.size() - 1;
  
double avg = 0;
  
for(vector<double>::iterator it = numbers.begin() + start; it <= numbers.begin() + end; ++it)
avg += *it;
  
int n = end - start + 1;
avg /= n;
return avg;
}

output

names before calling name_case
GeoRGE Ng AlEkS
names after calling name_case
George Ng Aleks
average from index 1 to 3 = 3
average from index -1 to 10 (auto correction) = 3.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote