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

C++ code #include <iostream> #include <string> #include <cstdlib> using namespac

ID: 3868903 • Letter: C

Question

 C++ code  #include <iostream> #include <string> #include <cstdlib> using namespace std;  void reverse(char* front, char* rear); // Precondition: The front and rear are pointing to the front //               and rear of a C-string, respectively // Postcondition: The C-string is reversed  string* addEntry(string* dynamiccArray, int& size, string newEntry); // Precondition: dynamicArray point to a array of strings with give size, //               newEntry is a string // Postcondition: A new dynamic array is created, which is one larger than //                dynamicArray All elements from dynamicArray are copied to //                new array, the new entry is added to new array, the size //                is increased, the dynamicArray is deleted, new dynamic //                array is returned.  string* deleteEntry(string* dynamicArray, int& size, string entryToDelete); // Precondition: dynamicArray point to a array of strings with give size, //               newEntry is a string // Postcondition: The function should search dynamicArray for entryToDelete. //                If not found, the request should be ignored and the //                unmodified dynamicArray returned. If found, create a new //                dynamic array one element smaller than dynamicArray. Copy //                all element except entryToDelete into the new array, delete //                dynamicArray, decrement size, and return the new dynamic //                array  void print(const string* dynamicArray, int size); // Precondition: dynamicArray point to a array of strings with give size, // Postcondition: The elements in dynamic array will be print out. One //                element per line forllowed by its index  int main() {     // write code to test reverse function.     // make sure that you test it on at least two strings     // one string has even length, another string has odd length          // write code to test add entry and delete entry function     // you may watch video notes to get idea for this part                       return 0; }  void reverse(char* front, char* rear) {         // you implement this. Please read Programming Project 4 on page 535 }  string* addEntry(string* dynamicArray, int& size, string newEntry) {         // you implement this. Please read Programming Project 6 on page 536     // you may watch video notes to get the idea }  string* deleteEntry(string* dynamicArray, int& size, string entryToDelete) {         // you implement this. Please read Programming Project 6 on page 536     // you may watch video notes to get the idea }  void print(const string* dynamicArray, int size) {     // you implement this.     // you may watch video notes to get the idea } 

Explanation / Answer

#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define SIZE 5

void reverse(char* front, char* rear);
// Precondition: The front and rear are pointing to the front
//               and rear of a C-string, respectively
// Postcondition: The C-string is reversed

string* addEntry(string* dynamiccArray, int& size, string newEntry);
// Precondition: dynamicArray point to a array of strings with give size,
//               newEntry is a string
// Postcondition: A new dynamic array is created, which is one larger than
//                dynamicArray All elements from dynamicArray are copied to
//                new array, the new entry is added to new array, the size
//                is increased, the dynamicArray is deleted, new dynamic
//                array is returned.

string* deleteEntry(string* dynamicArray, int& size, string entryToDelete);
// Precondition: dynamicArray point to a array of strings with give size,
//               newEntry is a string
// Postcondition: The function should search dynamicArray for entryToDelete.
//                If not found, the request should be ignored and the
//                unmodified dynamicArray returned. If found, create a new
//                dynamic array one element smaller than dynamicArray. Copy
//                all element except entryToDelete into the new array, delete
//                dynamicArray, decrement size, and return the new dynamic
//                array

void print(const string* dynamicArray, int size);
// Precondition: dynamicArray point to a array of strings with give size,
// Postcondition: The elements in dynamic array will be print out. One
//                element per line forllowed by its index

int main()
{

   // write code to test reverse function.
    // make sure that you test it on at least two strings
    // one string has even length, another string has odd length
   char* front; char* rear;
   string evenLenStr="EVEN";
   string oddLenStr="ODD";

   cout<<"Even Length string is "<<evenLenStr<<endl;
   cout<<"Odd Length string is "<<oddLenStr<<endl;
   cout<<"Passing Even Length string in reverse API "<<endl;
   front = &evenLenStr[0];
   rear = &evenLenStr[evenLenStr.size()-1];
   reverse(front, rear);
   cout<<"After Reverse even length string is "<<evenLenStr<<endl;
   cout<<"Passing Odd Lenght string in reverse API "<<endl;
   front = &oddLenStr[0];
   rear = &oddLenStr[oddLenStr.size()-1];
   reverse(front, rear);
   cout<<"After Reverse odd length string is "<<oddLenStr<<endl;

  
    string *strList=new string[SIZE];
    int i=0;
    strList[i++] = "TestName_01";
    strList[i++] = "TestName_02";
    strList[i++] = "TestName_03";
    cout<<"Original List is :"<<endl;

   print(strList,i);
  
    cout<<" After Adding an Entry TestName_04:"<<endl;
    strList=addEntry(strList, i,"TestName_04");
   print(strList,i);

    strList=deleteEntry(strList, i, "TestName_03");
   cout<<" After Adding an Entry TestName_03:"<<endl;
   print(strList,i);
  
    return 0;
}

void reverse(char* front, char* rear)
{
     while (front < rear) {
        char temp;
       temp = *front;
       *front = *rear;
       *rear = temp;
        front++; rear--;
    }
}

string* addEntry(string* dynamicArray, int& size, string newEntry)
{

   string *newArray = new string[size+1];
      for(int i=0; i<size; ++i) {
       newArray[i] = dynamicArray[i];
      }
      delete[] dynamicArray;
      newArray[size] = newEntry;
      ++size;
   return newArray;
}

int findName(string* dynamicArray, int size, string entryToDelete)
{
string *key = find(dynamicArray, dynamicArray+size, entryToDelete);
if(key == dynamicArray+size) {
    return -1;
}
return key - dynamicArray;
}

string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
{
   
int location; // location of the match
location = findName(dynamicArray, size, entryToDelete);
if (location >= 0 ) {
    string *newArray = new string[size-1];
    int i=0;
    for(int i=0, j=0; j<size; ++j) {
      if(j != location) {
   newArray[i] = dynamicArray[j];
   ++i;
      }
    }
    delete[] dynamicArray;
    --size;
    return newArray;
}

else
{
    cout << entryToDelete << " not found! ";
    return dynamicArray;
}
}

void print(const string* dynamicArray, int size)
{
    for(int i=0; i<size; ++i) {
    cout << dynamicArray[i] << endl;
   }
}

OutPUT:

Even Length string is EVEN
Odd Length string is ODD
Passing Even Length string in reverse API
After Reverse even length string is NEVE
Passing Odd Lenght string in reverse API
After Reverse odd length string is DDO
Original List is :
TestName_01
TestName_02
TestName_03

After Adding an Entry TestName_04:
TestName_01
TestName_02
TestName_03
TestName_04

After Adding an Entry TestName_03:
TestName_01
TestName_02
TestName_04

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