C9PJ6 Please code in C++ Problems continues into next image: Additional Notes(fr
ID: 3828844 • Letter: C
Question
C9PJ6 Please code in C++
Problems continues into next image:
Additional Notes(from my class):
-No global variables may be used in any of our assignments. If I see a global variable being declared, a zero points will be assigned.
Clarification:
-While variables may not be global, all functions and constants should be declared global.
-all assignments must contain functions, even if it is possible to do without one.
-If the assignment calls for just a function, you must code a driver to test the function. The driver and the function both must be submitted.
(Chapter 9 PJ 6)
Thanks
:)
6. One problem with dynamic arrays is that once the array is created using the new operator, the size cannot be changed. For example, you might want to add or delete entries from the array as you can with a vector. This project asks you to create functions that use dynamic arrays to emulate the behav ior of a vector. First, write a program that creates a dynamic array of five strings. Store five names of your choice into the dynamic array. Next, complete the following two functions: string addEntry(string *dynami CArray int &size; string new Entry); This function should create a new dynamic array one element larger than dynami CArray, copy all elements from dynamicArray into the new array, add the new entry onto the end of the new array, increment size, delete dynamicArray, and return the new dynamic array. string deleteEntry Cstring dynamicArray, int &size; string entry ToDelete); This function should search dynamicArray for entryToDelete. If not found, the request should be ignored and the unmodified dynamicArrayExplanation / Answer
#include<iostream>
#include<string>
using namespace std;
string ne="Viv";
string* addEntry(string* dynamicArray, int &size,string newEntry);
string* deleteEntry(string* dynamicArray, int &size,string entryToDelete);
void initialize(string* Array);
int main()
{
int N=5;
string* A;
A=new string[N];
initialize(A);
string* New;
New=addEntry(A,N,ne);
deleteEntry(A,N,ne);
delete[] A;
}
void initialize(string *Array)
{
for(int i=0;i<5;i++)
{
cin>>Array[i];
}
}
string* addEntry(string* dynamicArray, int &size,string newEntry)
{
string *New;
New=new string[size+1];
int i;
for(i=0;i<size;i++)
{
New[i]=dynamicArray[i];
}
New[i]=newEntry;
return New;
}
string* deleteEntry(string* dynamicArray, int &size,string entryToDelete)
{
string *New;
int flag=0;
for(int i=0;i<size;i++)
{
if(dynamicArray[i]==entryToDelete)
{
flag=1;
break;
}
}
if(flag==0)
return dynamicArray;
else
{
int k=0;
New=new string[size-1];
for(int i=0;i<size;i++)
{
if(dynamicArray[i]!=entryToDelete)
{
New[k++]=dynamicArray[i];
}
}
}
return New;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.