ThIS IS JAVA Write a method, remove, that takes three parameters: an array of in
ID: 3829997 • Letter: T
Question
ThIS IS JAVA
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. ( After deleting an element, the number of elements in the array is reduced by 1.) Assume that the array is unsorted.
This time use an insertion sort as given in this chapter to sort the array and then remove the item with the array sorted. Create a method for the insertion sort.
Explanation / Answer
#include <iostream>
using namespace std;
void remove(int[],int&,int);
void print(int[],int);
int main()
{int a[]={5,8,3,0,12,34,45,1,120};
int n=9,m;
cout<<"Before ";
print (a,n);
cout<<"Enter value to remove ";
cin>>m;
remove(a,n,m);
cout<<"After ";
print (a,n);
system("pause");
return 0;
}
void remove(int a[],int& n,int m)
{int i,j;
for(i=0;i<n;i++)
if(a[i]==m)
if(i==n-1)
{n--;
return;
}
else
{for(j=i;j<n-1;j++)
a[j]=a[j+1];
n--;
return;
}
cout<<m<<" not found in the array ";
}
void print(int a[],int n)
{int i;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.