Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, removeAt, that takes three parameters: an array of integers, t

ID: 3772382 • Letter: W

Question

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. You can use the following code that creates the unsorted random array for you.

#include <iostream> #include <stdlib.h> /* srand, rand */ #include <time.h> /* time */ #define Size 1000 using namespace std; void initArray(int*); int main() { int i; int x[Size]; initArray(x); /* Your solution goes here */ } void initArray(int x[]) { int i,n; const int limit = 10000; n=Size; srand (time(NULL)); for(i=0;i<n;++i) x[i]=rand() % limit + 1; } void mySorting(int x[]) { /* Your solution Goes Here*/ }

Explanation / Answer

#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */

using namespace std;

void initArray(int*,int);

int removeAt(int x[],int s,int n){
x[n]=x[s-1];
return s-1;
}

int main()
{
int i,k,n;
cout<<" Enter the size of the array";
cin>>n;
int x[n];
initArray(x,n);
cout<<" The size of the array is"<<n;
cout<<" The array elements are : ";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
cout<<" Enter the index to be removed";
cin>>k;
if(n<=k)
cout<<" Index is out of range and no deletion occurred";
else
n=removeAt(x,n,k);
cout<<" The new array size is "<<n;
cout<<" The array elements after deletion are ";
for(i=0;i<n;i++)
cout<<x[i]<<" ";
}

void initArray(int x[],int n){
int i;
srand (time(NULL));
for(i=0;i<n;++i)
x[i]=rand() % n + 1;
return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote