Write a function, removeAt, that takes three parameters: an array of integers, t
ID: 3772074 • 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
c++ code with an example:
#include <bits/stdc++.h>
using namespace std;
void removeAt(int x[],int * length,int index){
if(index<0 || index>= *length){
cout<<"Index out of bounds."<<endl;
}
else if(*length==0){
cout<<"Array is empty"<<endl;
}
else{
for(int i=index;i<(*length)-1;i++){
x[i]=x[i+1];
}
*length= (*length)-1;
}
}
void printarr(int x[],int * length){
for(int i=0;i<*length;i++){
cout<<x[i]<<endl;
}
}
int main(){
int x[]={1,2,3,4,5,6,7,8,9};
int l=9;
int * length=&l;
removeAt(x,length,3);
printarr(x,length);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.