Write a function to delete array elements with the following
Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer array, size is the number of elements stored m the array (passed by reference), and val_to_delete is a value that the user wishes to delete from the array. Your function should search the array for val_to_delete. If it finds the value, then it should delete it by moving all elements to the right of the value over to the left by one position. Then, it should decrement the value of size by one and return the Boolean value true. For example, if aTT= [5, 8, 9, 3, 4, 7, . ...] and the user selects val_to_delete == 9, then the values 3, 4, and 7 should each be moved one array index to the left to form the array [5, 8, 3, 4, 7....]. The value of size should be decremented from 6 to 5 and the function should return true because the value was found. If val_to_delete exists more than once in the array, then only the first occurrence of that value should be deleted. If val_to_delete does not exist in the array, then leave arr and size unchanged and have your function return false. Write a main function that sets up an array and invokes the function deleteElement. void generateRandomSortedArray(int arr[], int size, int max_val) that fills the array arr with sorted random integers between 1 and max_val (inclusive). Note that the numbers do not have to be unique and can repeat. For example, if size = = 4 and max_val = = 10, then some possible values that can be written into arr might be: 2, 5, 8, 9 1, 3, 6, 10 3, 4, 4, 7 Write a main function that generates and prints out the contents of 10 arrays, each of length 6, containing sorted random numbers selected between 1 and 100. #include #include unsigned int seed = time(0); srand(seed) This code only needs to be called once in your main function. Then, you can generate a random integer between 1 and max_val using the following code in your generateRandomSortedArray function: int randomInt = (rand() % max_val) + 1; ![Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer a Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer a](/WebImages/19/write-a-function-to-delete-array-elements-with-the-following-1038648-1761539230-0.webp)
Solution
#include <iostream>
#include <string>
using namespace std;
bool deleteElement(int arr[], int& size, int val_to_delete){
for (int i = 0; i < size; ++i)
{
if(arr[i] == val_to_delete){
// move all elements to the left
for (int j = i; j < size; ++j)
{
arr[j] = arr[j+1];
}
size -=1;
return true;
}
}
return false;
}
void print_arr(int arr[], int size){
for (int i = 0; i < size; ++i)
{
cout << arr[i] << \" \";
}
cout << endl;
}
int main(int argc, char const *argv[])
{
int arr[] = {5,8,9,3,4,7};
int size = 6;
print_arr(arr, size);
deleteElement(arr, size, 9);
print_arr(arr, size);
return 0;
}
![Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer a Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer a](/WebImages/19/write-a-function-to-delete-array-elements-with-the-following-1038648-1761539230-0.webp)
![Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer a Write a function to delete array elements with the following header: bool deleteElement (int ar [], int& size, int val_to_delete) where arr is an integer a](/WebImages/19/write-a-function-to-delete-array-elements-with-the-following-1038648-1761539230-1.webp)