Question
Example Output: Enter a list of up to 20 integers or -1 to end the list 3 2 4 8 12 5 6 9 0
Example Output:
Enter a list of up to 20 integers or -1 to end the list 3 2 4 8 12 5 6 9 0 11 10 13 -1 3 2 4 8 12 5 6 9 0 11 10 13 Enter a value and a position to insert: 18 5 3 2 4 8 12 18 5 6 9 0 11 10 13 Enter a value to delete from the array: 18 3 2 4 8 12 5 6 9 0 11 10 13 Enter a value to append: 15 3 2 4 8 12 5 6 9 0 11 10 13 15
The Output should be exactly the same
Please fix the errors. C++
Here's my code:
/* Working with arrays and functions * Author: * Last modified on: * Known bug */ #include using namespace std;
const int CAPACITY=20; // [Already implemented] // displayArray - display the array on a single line separated by blanks. // @param: int array[] is an unordered array of integers // @param: int numberOfElements // [Already implemented] void displayArray(int array[], int numElements);
//ToDo: Declare a function fillArray that fills an int array with values entered // by the user. Stop reading when the user inputs -1 or you reach CAPACITY. // fillArray - reads a list of positive integers ending with -1 or until CAPACITY. // @param: int array[] is an unordered array of integers when leaving this function // @param: int& numberElements is the number of Elements in the array after function // @returns void. void fillArray( int array[], int &numElements);
//ToDo: Delcare a function that removes (i.e., deletes) the element // removeElement - removes the element of the given index from the given array. // @param: int array[] is an unordered array of integers // @param: int& numberElements // @param: int position of element to delete // @returns: true if delete was successful, false otherwise int removeElement( int array[], int &numElements, int position);
//ToDo: Delcare a function that inserts the element in the given position // insertElement - removes the element of the given index from the given array. // @param: int array[] is an unordered array of integers // @param: int& numberElements // @param: int position to insert into // @param: int target to insert. // @returns: true if insert was successful, false otherwise int insertElement( int array[], int &numElements, int position, int target);
//ToDo: Declare a funcxtion that searches for an element in the given array // searchElement - searches for the element in the given array. // @param int array[] is an unordered array of integers // @param int numberOfElements // @param int element // @returns index of element or -1 if not found. int searchElement( int array[], int numElements, int element); int main() { // The NumArray can be partially filled, we use variable NumArrayElems to keep track of how many numbers // have been stored in the array. int NumArray[CAPACITY]; // an int array with a given CAPACITY int NumArrayElems=0; // the array is initially empty, i.e., contains 0 elements int pos; int data; int search=0;
// 1. ToDo: Call your fillArray function to read in a sequence of integer values, // separated by space, and ending with -1. Store the values in the NumArray array // and the number of elements in NumArrayElems. // Display the contents of the array afterwards fillArray(NumArray, NumArrayElems); displayArray(NumArray, NumArrayElems); // 2. ToDo: Read in a value and position from the user. Call your insertElement function // to insert the given value into the given position of the array // Display the contents of the array afterwards cout>data; cin>>pos; insertElement(NumArray, NumArrayElems,pos,data); displayArray(NumArray, NumArrayElems); // 3. ToDo: Read in a value and call your searchElement function. cout>data; pos=searchElement(NumArray,NumArrayElems, data);
// 4. if the value is found, delete it from the array using your deleteElement function // if the value not found, print "Value not found!" // Display the contents of the array afterwards if(pos!=-1) { removeElement(NumArray,NumArrayElems,pos); displayArray(NumArray, NumArrayElems); } else {cout>data; insertElement(NumArray, NumArrayElems,NumArrayElems,data); displayArray(NumArray, NumArrayElems);
return 0; }
//TODO: Implement all functions declared above. //Don't forget to put precondition/postcondition comments under or over the function header.
// displayArray - displays the array // precondition: int array[] is an unordered array of numElems integers. // postcondition: array is displayed on the console on a single line separated by blanks. void displayArray(int array[], int numElems) { for (int i = 0; i
void fillArray( int array[], int &numElements) { int x=0,i=0; cout>x; cout
int removeElement( int array[], int &numElements, int position){ if(position>numElements||position=0){ for(int i=numElements+1;i>=position;i--){ array[i]=array[i-1]; } array[position]=target; } numElements++; //Here numElements should be icremented by 1 so that array size can be increased return 1; }
int searchElement( int array[], int numElements, int element) { int x=-1,i=0; for(i=0;i {if(array[i]==element) { x=i; } } return x; }
PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing, red indicates things in your output that shouldn't be there. The + character refers to newlines, so the green & character refers a newline you are missing in your output and the red a refers to a newline you need to remove from your output. 1 2 3 Enter a list of up to 20 integers or -1 to end the list 8 9 7 6 5 4 3 2 1 0 Enter a value and a position to insert: 8 9 7 6 5 4 3 2 1 4197981 Enter a value to delete from the array: Value not found! Enter a value to append: 8 9 7 6 5 4 3 2 1 04197981 -1 / 4 5 6 PRETTY DIFF This diff is colored to make it clear what parts of the output are wrong. Green indicates things in the correct output that you are missing, red ndicates things in your output that shouldn't be there. The character refers to newlines, so the green & character refers a newline you are missing in your output and the red refers to a newline you need to remove from your output. 1 u +wn Enter a list of up to 20 integers or -1 to end the liste 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Enter a value and a position to insert: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -1519855808 Enter a value to delete from the array: Value not found! Enter a value to append: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -1519855808 32765 6Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started