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

Write a C++ program Question: About Dynamic Memory Allocation Consider the follo

ID: 3684822 • Letter: W

Question

Write a C++ program

Question: About Dynamic Memory Allocation

Consider the following code piece and complete the parts a thru' d:

string * namesPtr;

int size;

cout >> "How many names do you like to store: ";

cin > size;

a. Write C++ code to dynamically create an array of 10 strings's and make namesPtr contain the base address of the dynamic array.

b. Write C++ code that inputs data (say names of your family members) into the dynamic array in part a above from the keyboard.

c. Write C++ code that output data from the dynamic array to in part b above to the screen.

d. Write a C++ code to de-allocate the memory space allocated for the dynamic array created in part a above.

Explanation / Answer

#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
  
   string * namesPtr;

   int size;

   cout<< "How many names do you like to store: ";

   cin >>size;
  
   //a
   namesPtr = new string[10];
  
   //b
   for(int i=0; i<size; i++){
       cout<<"Enter "<<(i+1) <<" name: ";
       cin>>namesPtr[i];
   }
  
   //c
   for(int i=0; i<size; i++){
       cout<<namesPtr[i]<<endl;
   }
  
   //d
   delete [] namesPtr;
  
   return 0;
}

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