Question: C++ question: Warning, it's a long one. Please only answer if you have time to follow directions carefully. If possible, explain some of the steps.
C++ question: Warning, it's a long one. Please only answer if you have time to follow directions carefully.
If possible, explain some of the steps. Thanks!
For this project, use pointer notation rather than array notation whenever possible. For example, use *(p + i) rather than p[i].
In main, create a loop that asks for a user choice and prints the following menu:
cout << "Please choose from the following menu choices:" << endl; cout << "1) Append a new element to the end of the list." << endl; cout << "2) Insert a new element at the beginning of the list." << endl; cout << "3) Insert an element into the list at a given index." << endl; cout << "4) Remove an element from the list at a given index." << endl; cout << "5) Sort the list." << endl; cout << "6) Print the contents of the list." << endl; cout << "7) Exit." << endl;
Implement each option in the menu. Use the following function prototypes for your design:
void printMenu(); void printList(int *p, int count); int* expand(int *p, int count, int &size); void insertZero(int *p, int n, int count); bool insert(int *p, int n, int start, int count);
bool remove(int *p, int index, int count); void sort(int *p, int size);
For your insert function (option 3 in the menu), make sure the index given is not greater than your count of integers currently in the list. If the index is out of bounds, return false. If the insertion succeeds, return true.
For your remove function, make sure the index is valid. If the index is not valid, return false. If the removal succeeds, return true.
For your sort function, you can use any sorting algorithm you like. Be sure to use pointer notation rather than array notation for your algorithm.
You may combine insertZero and insert if you wish, since you could just pass an index of 0 into insert to accomplish the same task.
Use the example with the char array from class for hints. This code is attached below. Remember to expand the array when the size is equal to the count.
Don't forget to free up your dynamic pointers when the program completes to avoid memory leaks.
Make sure you use "new" to dynamically allocate your arrays. If you allocate them on the stack by declaring them normally the addresses will not be valid when the functions return.
Note that expand() returns the pointer to the newly expanded array. This means that expand should be called from main so the pointer to the array in main can be updated.
And here's an attachment of mystring.cpp: (I'm not sure why we need it, professor attached it to the homework question)
#include
using namespace std;
void printMenu(); void printString(char *, int); char *expand(char*, int, int&); void insertZero(char *, char, int);
int main() { int size = 1; ///size starts at 1, will dynamically change char *p = new char[size]; ///on the heap int count = 0; ///stores the number of chars typed into the array ///in other words, count holds the number of chars they have typed ///size holds the number of chars the array could potentially hold cout << "Welcome to the character storage program!" << endl; cout << "I will dynamically hold an array of the characters you type!" << endl;
char c; ///store the character they type to enter into the array int choice; ///menu choice do { printMenu(); cin >> choice; ///get menu choice after printing menu switch(choice) { case 1: /// insert a char at the beginning of the list cout << "Enter a char to insert: "; cin >> c; if(size == count) { p = expand(p, count, size); ///get a new bigger array and copy over existing elements } insertZero(p, c, count); ///insert new element at index 0 count++; break; case 2: printString(p, count); break; default: if(choice != 3) { cout << "Invalid menu choice" << endl; }
}
}while(choice != 3); ///exit loop on 3
return 0; }
void printMenu() { cout << "Select a menu choice: " << endl; cout << "1) Insert a new element at the beginning of the list." << endl; cout << "2) Print the contents of the list." << endl; cout << "3) Exit." << endl; }
void insertZero(char *p, char c, int count) { ///really concise code below ///alternatively you could make a copy of the whole array ///and loop through it. ///the goal is to copy c to element 0 and shift everything else ///one index to the right for(int i = count; i >= 1; i--) { *(p + i) = *(p + i - 1); } *p = c; ///insert c at element 0 after shifting everything to the right }
char *expand(char *p, int count, int &size) ///call this from main { size *= 2; ///double the size to hold more stuff cout << "Expanding the array. New size: " << size << endl; char *temp = new char[size]; ///new bigger array, unitialized ///copy old stuff into new array for(int i = 0; i < count; i++) { *(temp + i) = *(p + i); } delete []p; ///free up the old array return temp; ///return the new bigger array with copied elements }
void printString(char *p, int count) { for(int i = 0; i < count; i++) { cout << *(p + i); ///*(p + i) is same as p[i] } cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
