I need to write this in C++: Write a function called Delete that takes in three
ID: 3566992 • Letter: I
Question
I need to write this in C++:
Write a function called Delete that takes in three parameters: an integer array, the size of the array, and the index of the item to delete. This function should delete the value at the given index. The remaining items in the array will need to shift over to fill the "empty" slot. The last item in the array (now vacant) should be set to 0. If the given index is out of bounds for the array, abort the function without deleting anything. This function does not return a value.
i've tried a couple things but i can't figure out to to properly write the definition for this function. Any help would be greatly appreciated.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int del(int l[],int n,int j){
if (j >= n || j < 0){
return 0;
}
int i = 0;
while (i < j)
i++;
for (; i < n-1; i++){
int t = l[i];
l[i] = l[i+1];
l[i+1] = 0;
}
return 1;
}
int main(){
int l[10] = {1,2,3,4,5,6,7,8,9,10};
cout << "Array Before Deletion " << endl;
for (int i= 0; i < 10; i++)
cout << l[i] << " ";
cout << endl;
int res = del(l,10,4);
if (res == 1){
cout << "Successfully Deleted ";
cout << "Array After Deletion "<< endl;
for (int i= 0; i < 10; i++)
cout << l[i] << " ";
cout << endl;
}
else
cout << "ERROR : INDEX OUT OF BOUND ";
}