1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
/* this function deletes a number from
an array. The user is to enter the number to be deleted
and a void function, remove, deletes it
*/
void remove(int list[], int listLength, int removeItem); //function prototype
int main()
{
int num[]={1,2,3,4,5,6,7,8,9,0}; //array num of size 10
int removeNum; //variable to hold the number to be deleted from the array
int length=10; //length of the array
cout << "Enter an item you want removed from the list." << endl;
cin >> removeNum;
remove(num, length, removeNum); //function call to remove the number
cout << "After removing " << removeNum << " from the list, the list is " << endl;
for(int index=0;index<length;index++) //output the array after number is deleted
cout << num[index] << " ";
cout << endl;
return 0;
}
void remove(int list[], int& listLength, int removeItem) //function to delete the number
{
for(int i=0;i<listLength;i++)
if(list[i]==removeItem) //checks to see whether the number to be deleted is found in the array
{
listLength=listLength-1; //decrements the lenght of the array by 1 if the number is found
for(int index=i;index<listLength;index++) //loop to update the contents of the array after the number is deleted
{
list[index]=list[index+1];
}
break;
}
}
|