Question

In this project, the students will finish three functions that are described in Programming Project 4...

In this project, the students will finish three functions that are described in Programming Project 4 (on page 536) and Programming Project 6 (on page 358). The student will also implement the main function and a print function to test these three functions. Please notice, there is a video notes for the solution of Project 6. However, your main function shall have more test than what in video notes.

The student may start with the attached code. Please rename the file as YourNameProg1.cpp. Turn in the file via the "Browse My Computer" link below (You need to click on Proj 1 link above first).

___

(Project 4) Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length.

___

(Project 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 behavior 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 *dynamicArray, int &size, string newEntry);

This function should create a new dynamic array one element larger than dynamicArray, 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(string *dynamicArray, int &size, string entryToDelete);

This 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 elements except entryToDelete into the new array, delete dynamicArray, decrement size, and return the new dynamic array. Test your functions by adding and deleting several names to the array while outputting the contents of the array. You will have to assign the array returned by addEntry or deleteEntry back to the dynamic array variable in your main function.

___

/* This program requires the student to write 3 functions described in
 * Program project 4 (Page 535) and Program Project 6 (Page 536).
 * The student also need to add a print function to print out an array.
 * The student may watch video notes on MyProgrammingLab to get the idea
 * on how to write the main function and three of these four functions
 *
 * Author: Your Name
 * Version: The date
 */

#include 
#include 
#include 
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
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//pls rate my solution positively,thanks! pls just don't give -ve rating if are not able to compile the program. I compiled and executed and pasted the output.

#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()
{
   char str1[] = "Hello", str2[] = "World";
   // write code to test reverse function.
   reverse(&str1[0], &str1[4]);
   reverse(&str2[0], &str2[4]);
   //print str1 now
   cout << "Reverse of string "<<str1<<" is "<<str1 << endl;
   cout << "Reverse of string " << str2 << " is " << str2 << endl;
   // make sure that you test it on at least two strings
   // one string has even length, another string has odd length
   string arr[3] = { "This","is","To" };
   int size = 3;
   string *retStr=addEntry(arr, size, "Test");
   print(retStr, size);
   retStr = addEntry(retStr, size, "Programs");
   print(retStr, size);
   // write code to test add entry and delete entry function
   // you may watch video notes to get idea for this part
   retStr = deleteEntry(retStr, size, "tests");
   cout << "Dynamic array after deleting test: "<<endl;
   print(retStr, size);
   retStr = deleteEntry(retStr, size, "Test");
   cout << "Dynamic array after deleting test: " << endl;
   print(retStr, size);
   return 0;
}

void reverse(char* front, char* rear)
{
   // you implement this. Please read Programming Project 4 on page 535
   char tmp;
   while (front < rear) {
       tmp = *front;
       *front++ = *rear;
       *rear-- = tmp;
   }
}

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 *newStrArray = new string[size + 1];  
   for (int i = 0; i < size; i++)
   {
       newStrArray[i] = dynamicArray[i];
   }
   newStrArray[size++] = newEntry;
   return newStrArray;
}

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

   //first check if entryToDelete found in dynamic array
   int found = 0;
   for (int i = 0; i < size; i++)
   {
       if (entryToDelete == dynamicArray[i])
           found = 1;

   }
   if (found == 0) //return dynamicArray unchanged
   {
       cout << entryToDelete << " not found in dynamicArray" << endl;
       return dynamicArray;
   }

   string *newStrArray = new string[size - 1];
          
   for (int i = 0,j=0; i < size; i++)
   {
      
       if (entryToDelete != dynamicArray[i])
       {
           newStrArray[j++] = dynamicArray[i];
          
       }
      
   }
   --size;
   return newStrArray;
}

void print(const string* dynamicArray, int size)
{
   // you implement this.
   // you may watch video notes to get the idea
   cout << "Dynamic array : " << endl;
   for (int i = 0; i < size; i++)
   {
       cout << dynamicArray[i] << endl;
   }
}

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

/*Output
Reverse of string olleH is olleH
Reverse of string dlroW is dlroW
Dynamic array :
This
is
To
Test
Dynamic array :
This
is
To
Test
Programs
tests not found in dynamicArray
Dynamic array after deleting test:
Dynamic array :
This
is
To
Test
Programs
Dynamic array after deleting test:
Dynamic array :
This
is
To
Programs

*/

Add a comment
Know the answer?
Add Answer to:
In this project, the students will finish three functions that are described in Programming Project 4...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ 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 similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

  • In C Programming Language In this lab you will implement 4 string functions, two using array...

    In C Programming Language In this lab you will implement 4 string functions, two using array notation and two using pointers. The functions must have the signatures given below. You may not use any C library string functions. The functions are 1. int my strlen (char s ) - This function returns the number of characters in a string. You should use array notation for this function. 2. int my strcpy (char s [], char t I)- This function overwrites...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT