Question:
c++ how to remove numbers from an array?
mike
2010-11-19 19:12:35 UTC
I have an array of numbers, but I want to write a function to remove one of the numbers.
I tried this:

bool NumberCollection::removeNumber(int num)
{
bool check;
if (indexOf(num) == -1 && count < SIZE){
numberArray[count] = num;
count --;
check = true;
}
else {
check = false;
}
return check;
}

but it didnt work. What am I doing wrong with it?
Five answers:
?
2010-11-19 19:31:36 UTC
arrays are not like vectors or other normal C++ containers. The size of an array is fixed the moment it is created, and numbers cannot be removed, only changed.



It is unclear what is your function trying to achieve, without comments, but I would *guess* that SIZE is the actual size of the array and count is the number of useful elements in the array, in which case you're planning to swap the number out to the end of the array and reduce count, which is a legitimate approach. Here's how it would work in a complete program:



#include

#include

#include

#include

#include

template

class NumberCollection {

     int numberArray[SIZE];

     int count;

public:

     template

     NumberCollection( int(&a)[N] ) : count(N) {

         if (N>SIZE) throw std::range_error("NumberCollection");

         std::copy(a, a+N, numberArray);

     }

     friend std::ostream& operator<<(std::ostream& os, NumberCollection& nc)

     {

         std::copy(nc.numberArray, nc.numberArray+nc.count, std::ostream_iterator(os, " "));

         return os;

     }

     // *** this is your function. I simplified it a bit

     // *** also I taught it to remove multiple instances of num

     // *** if you just want to remove one, use std::find and std::swap

     bool removeNumber(int num)

     {

         int* p = std::remove(numberArray, numberArray+count, num);

         if(p == numberArray+count) return false;

         count = p - numberArray;

         return true;

     }

};

int main()

{

     int a[] = {1, 2, 2, 3};

     NumberCollection<10> nc(a);

     std::cout << " initial NumberCollection: " << nc << '\n';

     bool two_removed = nc.removeNumber(2);

     std::cout << " number 2 removed? " << std::boolalpha << two_removed << " nc = " << nc << '\n';

     bool seven_removed = nc.removeNumber(7);

     std::cout << " number 7 removed? " << seven_removed << " nc = " << nc << '\n';

}



test run:



~ $ ./test

initial NumberCollection: 1 2 2 3

number 2 removed? true nc = 1 3

number 7 removed? false nc = 1 3
husoski
2010-11-19 19:46:00 UTC
Looks like you're making a list of ints, with an int[] array as the hidden implementation.



It also looks like you modified an addNumber() method, and didn't remove/change enough of the add logic. Why else would you store num into the array you're trying to delete it from?



Deleting a number that is NOT at the end of the list leaves a gap, and you have to either fill that gap by moving other numbers within the array. If the numbers have to stay in order, that means moving all of the numbers with larger index locations down one position. If the numbers don't have to stay in order, then you can simply move the last number on top of the deleted one.



You don't want to check for indexOf(num) == -1. That's true only when the number isn't in the list to begin with (leftover add logic?). Save the value:



int index = indexOf(num);



Then verify that index >= 0 (and < count, if you're paranoid about the validity of indexOf()) before doing any actual deleting.



If it's an unordered collection, then you can use

--count;

numberArray[index] = numberArray[count];



to do the delete. You can skip the assignment if index==count (deleting the last entry) but leaving it in does no harm.



If the remaining numbers need to stay in the order in which they were added, then you need to write a loop to copy the next int at index+1 to index, the int at index+2 to index+1, and so on until the index at count gets copied to count-1. (Assuming you decrement count beforehand.)
gravitt
2016-12-17 12:24:33 UTC
properly, one th865ccb4ab0e063e5caa3387c1a8741ng you're able to desire to do 865ccb4ab0e063e5caa3387c1a8741s use the set strategies-set. verify the array for a style each t865ccb4ab0e063e5caa3387c1a8741me you attempt to pass right into a style. 865ccb4ab0e063e5caa3387c1a8741f the type 865ccb4ab0e063e5caa3387c1a8741s there, then do not placed the type 865ccb4ab0e063e5caa3387c1a8741nto the array, and deliver a message to the person. S865ccb4ab0e063e5caa3387c1a8741nce you have a small array, you will not go through too lots check865ccb4ab0e063e5caa3387c1a8741ng the array for the type. a manner talked approximately as conta865ccb4ab0e063e5caa3387c1a8741ns w865ccb4ab0e063e5caa3387c1a8741ll help you do th865ccb4ab0e063e5caa3387c1a8741s. bool conta865ccb4ab0e063e5caa3387c1a8741ns (865ccb4ab0e063e5caa3387c1a8741nt num) { for(865ccb4ab0e063e5caa3387c1a8741nt i=0; i < length; i++) 865ccb4ab0e063e5caa3387c1a8741f(865ccb4ab0e063e5caa3387c1a8741ntegers[i]==num) return authentic; return fake; } Then have the person enter ten numbers. each t865ccb4ab0e063e5caa3387c1a8741me they enter a style, conta865ccb4ab0e063e5caa3387c1a8741ns() 865ccb4ab0e063e5caa3387c1a8741s talked approximately as f865ccb4ab0e063e5caa3387c1a8741rst. 865ccb4ab0e063e5caa3387c1a8741f the type 865ccb4ab0e063e5caa3387c1a8741s 865ccb4ab0e063e5caa3387c1a8741n the array, tell the person "you have already entered x. Please enter a d865ccb4ab0e063e5caa3387c1a8741fferent style." That way, you forestall dupl865ccb4ab0e063e5caa3387c1a8741cates from the beg865ccb4ab0e063e5caa3387c1a8741nn865ccb4ab0e063e5caa3387c1a8741ng, and you do not might desire to bypass around chang865ccb4ab0e063e5caa3387c1a8741ng the array s865ccb4ab0e063e5caa3387c1a8741ze or anyth865ccb4ab0e063e5caa3387c1a8741ng.
The Phlebob
2010-11-19 19:26:51 UTC
Where are you removing the value? If the number isn't in the array, you copy it into the last element (overwriting whatever's there), then decrementing the count (dubious in its own right).



And what happens if indexOf(num) isn't -1, that is (I think) if num happens to exist?



Hope that helps.
anonymous
2010-11-19 19:22:09 UTC
U cannot phisically delete a term from an array. U can only delete its value, and then moving all terms after the "deleted" one, a position to the begining. Something like this



....

int SIZE = 100;

int number[SIZE];

....

void DeleteNumber(int Index)

{

for(int i=Index;i
number[i] = number[i+1]; // moving the terms of array

SIZE--; // updating new size

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...