Question

(C++) Write a function, remove, that takes three parameters: an array of integers, the number of...

(C++) Write a function, remove, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, removeItem). The function should find and delete the first occurrence of removeItem in the array.

(Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

Also, write a program to test the function. Your program should prompt the user to enter 10 digits for the array. Display the starting array to the user and prompt them to select an integer to remove. After the selected integer has been removed, the updated list should be displayed to the user. If the value does not exist or the array is empty, output the following message:

 x is not in the list 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

thanks for the question, here is the complete program in C++.

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

#include<iostream>

using namespace std;

// function takes in the array, the number of elements and the number to be removed

// function returns true if the number was successfully deleted

// function returns false if the number was not found

bool remove_(int* &array_p, int size, int removeItem){

               

                bool numberExist = false;

                for(int i=0; i<size; i++){

                                if(*(array_p+i)==removeItem){

                                                numberExist=true;

                                                break;

                                }

                }

                if(!numberExist)return false; // when the number was not found return false

               

// create a new dynamic array of size 1 less than the original array

                int* updated_pointer = new int[size-1];

                bool removed = false;

                int index=0;

// copy the numbers from the original array to the new array

                for(int i=0; i<size; i++){

                                if((*(array_p+i)==removeItem) &&!removed){

                                                removed=true;

                                }

                                else{

                                                *(updated_pointer+index) = *(array_p+i);           

                                                index+=1;

                                }

                }

                delete[] array_p; // delete the original array

                array_p = updated_pointer; // assign the new array address to the original array variable

                return true; // return true as the number was deleted

               

}

int main(){

               

               

                int* array_pointer = new int[10];

               

                for(int i=0; i<10; i++){

                                cout<<"Enter number "<<i+1<<": ";

                                cin>>*(array_pointer+i);

                }

               

                int removeItem;

                cout<<"Enter the number you like to delete: ";

                cin>>removeItem;

               

                cout<<"\nInitial Array: \n";

                for(int i=0; i<10; i++){

                                cout<<*(array_pointer+i)<<" ";

                }

                cout<<endl<<endl;

               

                if(remove_(array_pointer,10,removeItem)){

                               

                                cout<<"After removal: \n";

                                for(int i=0; i<9; i++){

                                cout<<*(array_pointer+i)<<" ";

                                }

                                cout<<endl;      

                }else{

                                cout<<removeItem<<" is not in the list\n";

                }

               

                delete[] array_pointer;

               

}

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

Add a comment
Know the answer?
Add Answer to:
(C++) Write a function, remove, that takes three parameters: an array of integers, the number of...
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
  • Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array...

    Program must be wriiten in c++ Write a function, removeAt, that takes three parameters: an array of integers, the number of elements in the array, and an integer (say, index). The function should delete the array element indicated by index. If index is out of range or the array is empty, output an appropriate message. (Note that after deleting the element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.

  • (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of...

    (C++) Write a function, insertAt, that takes four parameters: an array of integers, the number of elements in the array, an integer (say, insertItem), and an integer (say, index). The function should insert insertItem in the array provided at the position specified by index. If index is out of range, output the following: Position of the item to be inserted is out of range or if the index is negative: Position of the item to be inserted must be nonnegative...

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array...

    In c++ 1. Write a function named findTarget that takes three parameters - numbers: an array of integers size: an integer representing the size of array target: a number The function returns true if the target is inside the array and false otherwise 2. Write a function min Valve that takes two parameters: myArray an array of doubles - size: an integer representing the size of array The function returns the smallest value in the array 3 Wrile a funcion...

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • Write a program that asks the user to enter 1000 integers to be stored in an...

    Write a program that asks the user to enter 1000 integers to be stored in an array called "numbers". Since the same integer might occur (exist) in the array multiple times, your program needs to fill a second array, called "Isolate" that contains all the integers from the first array but NOT REPAPTED (every integer will exist only once). Finally, your program will print the integers of the second array sorted by occurrence (occurrence is: the number of times the...

  • Write a function named findTarget that takes three parameters: - numbers, an array of integers -...

    Write a function named findTarget that takes three parameters: - numbers, an array of integers - size, an integer representing the size of array - target, a number The function returns true if the target is inside array and false otherwise

  • Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and...

    Write a C++ function, lastLargestIndex that takes as parameters an int array and its size and returns the index of the "last occurrence" of the largest element in the array. Include another function to print the array. Also, write a program to test your function. [HINTS) Create an array of size 15 in the main function and initialize it with the values shown in the below sample output. Pass the array and its size to function lastLargestindex; function lastLargestindex returns...

  • Write a function to have a user enter some number of integers into an array. The...

    Write a function to have a user enter some number of integers into an array. The integer values must be between -100 and +100 inclusive (+100 and -100 should be accepted as valid inputs). The integer array and the size of the array are passed into the function through parameters. Do not worry about includes. This is only a function, so there is no main routine. The function should fill the array with valid inputs. For invalid input values, inform...

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
ADVERTISEMENT