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

C++ (don\'t copy, ask own question or change) a. Write a code snippet which asks

ID: 3822976 • Letter: C

Question

C++

(don't copy, ask own question or change)

a. Write a code snippet which asks the user to enter city names until "DONE" is entered and stores the cities entered in a string vector.
Then print the cities that were entered but in reverse order.

b. Given a code snippet like int val = 5; please write a code snippet that increments the value without assigning a value to the variable val directly (e.g. no val++; or val=6;). Then print the new value of the variable val without using the val variable.

c. Write a function named increment which takes a pointer to int and increments the value of the variable pointed at. The function returns nothing.
Provide client code that calls the function.

d. Write a code snippet that asks the user to enter how many scores will be input and allocates an array with the requested capacity.
Then ask the user again and allocate a new array using the same variable used for the previous array. What should you do to avoid leaking the memory of the previous array?

e. Write a function named combine that takes two int arrays and their lengths and returns a new array that contains all the elements in arr1 followed by the elements in arr2. Provide client code that calls the function and prints all the elements.

Explanation / Answer

a.

#include <iostream>
#include <vector>


using namespace std;

int main()
{
    vector<string> cityNames; // declare vector of city names
    string cn ="NOT DONE";
  
   while (cn != "DONE")
    {
     
       cout << " Please enter city name, <DONE> to quit: ";
       getline(cin,cn);
       if (cn == "DONE")
         break;
       else
          cityNames.push_back(cn);//if city name is not DONE push it into vector
    }
  
    int i;
    cout<<" City Names in reverse order : ";
    for (i = cityNames.size()-1;i>=0; i--) // display in reverse order
     

       cout << cityNames[i]<<" ";


    return 0;
}

Output:

Please enter city name, <DONE> to quit:New York

Please enter city name, <DONE> to quit:California

Please enter city name, <DONE> to quit:Paris

Please enter city name, <DONE> to quit:Toranto

Please enter city name, <DONE> to quit:Santa Clara

Please enter city name, <DONE> to quit:DONE

City Names in reverse order :

Santa Clara

Toranto

Paris

California

New York

b.

#include <iostream>

using namespace std;

int main()
{
    int val = 5;
    int *ptrVal = &val;
  
    cout<<"Incremented value of val = "<<*ptrVal+1;

    return 0;
}

Output:

Incremented value of val = 6

c.

#include <iostream>

using namespace std;

void increment(int *ptrVal)
{
     (*ptrVal)++;
  
}
int main()
{
    int val = 5;
  
    int *ptr = &val;
    cout<<"Incremented value of val = ";
    increment(&val);
    cout<<val;

    return 0;
}

output:

Incremented value of val = 6

d.

#include <iostream>
#include <cstdlib>

using namespace std;


int main()
{
     int i,n;
  
    cout<<"Enter the number of scores to input";
    cin>>n;
    int *p = (int*) malloc ( sizeof(int) * n );
  
    cout<<" Scores: ";
    for(i=0;i<n;i++)
    {
    *(p+i) = i;
    cout<<*(p+i);
    }

    free (p); //to avoid memory leaks
  
    cout<<" Enter the number of scores to input";
    cin>>n;
  
    p = (int*) malloc ( sizeof(int) * n );
     cout<<" Scores: ";
   for(i=0;i<n;i++)
    {
    *(p+i) = i+4;
    cout<<*(p+i);
    }

    free (p);
  

    return 0;
}

output:

e.

#include <iostream>
#include <cstdlib>

using namespace std;
void combine(int arr1[10],int arr2[10],int arr3[20])
{
    int i;
    for(i=0;i<10;i++)
    {
        arr3[i] = arr1[i]; //copy elements of first array
      
    }
    for(i=10;i<20;i++)
    {
        arr3[i] = arr2[i-10];//copy elements of second array
      
    }
  
}

int main()
{
  
   int arr1[10],arr2[10],arr3[20];
   int i;

   for(i=0;i<10;i++)
   cin>>arr1[i];

   for(i=0;i<10;i++)
   cin>>arr2[i];

   combine(arr1,arr2,arr3);

   for(i=0;i<20;i++)
   cout<<arr3[i]<<" ";

    return 0;
}

output: