4 Replies - 767 Views - Last Post: 12 February 2009 - 10:50 PM Rate Topic: -----

#1 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

Removing Items from an Array

Posted 12 February 2009 - 10:05 AM

I have a small program that I built (code is listed below) that allows the user to input 10 numbers into an array and then search for the location of a number within that array. However, I am trying to make the program completely remove the number they search for (example: if the user enters 1 - 10 and wants to remove the number 8, the array would no longer hold the number 8). I've been trying to figure it out since yesterday but can't seem to find a way of going about it. Any help is greatly appreciated.

#include <iostream>

using namespace std;

// Declare ARRAY_SIZE as a constant variable
const int ARRAY_SIZE = 10;

int remove(const int array[], int arrayLength, int searchItem);

int main()
{
	int intArray[ARRAY_SIZE];
	int number;
	int index;
	
	cout << "Enter " << ARRAY_SIZE
		 << " integers." << endl;
	
	// As long as index is less than ARRAY_SIZE(10)
	// allow the user to input the values into intArray	 
	for(index = 0; index < ARRAY_SIZE; index++)
			  cin >> intArray[index];
			  
	cout << endl;
	
	// Ask user which number they wish to search for
	cout << "Enter the number to be searched: ";
	cin >> number;
	cout << endl;
	
	// Call function remove and set the value to index
	index = remove(intArray, ARRAY_SIZE, number);
	
	// If index is a valid number in the array
	if(index != -1)
			 cout << number << " is found at position "
				  << index
				  << endl;
	else
			 cout << number << " is not in the list." << endl;
			 
	cout << endl;
		
	// Display the variables in intArray		 
	for(index = 0; index < ARRAY_SIZE; index++)
			  cout << intArray[index] << " ";
	cout << endl;
	
	system("Pause");
}

int remove(const int array[], int arrayLength, int searchItem)
{
	 int loc;
	 
	 for(loc = 0; loc < arrayLength; loc++)
			 if(array[loc] == searchItem)
				 return loc;
	 
	 return -1;	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Removing Items from an Array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Removing Items from an Array

Posted 12 February 2009 - 10:23 AM

You can't truly "remove" an item from a standard array. You could make the space blank, shift everything left, etc... but the 'slot' is still there.

Can you use a vector for this assignment?
Was This Post Helpful? 0
  • +
  • -

#3 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

Re: Removing Items from an Array

Posted 12 February 2009 - 10:30 AM

Can't use vectors.

But I basically need to do what you said. Just remove the first occurance of what ever integer the user enters.
Was This Post Helpful? 0
  • +
  • -

#4 C_Dingo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-December 08

Re: Removing Items from an Array

Posted 12 February 2009 - 12:30 PM

Well, I just figured it out after messing with the code for a bit. I found an old question that was similar to mine from searching on Google and they suggested just setting the value found in the array to Null. At first I was trying to set up an if statement for setting it to Null, but finally figured out that all I had to do was simple set it to Null. I have included the piece of code that I used for this incase someone else ever has this problem (Note: I didn't shift the values in the array down since it wasn't required).

// Call function remove and set the value to index
	index = remove(intArray, ARRAY_SIZE, number);
	
// Set the searched index equal to Null
			 intArray[index] = '\0';


Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Removing Items from an Array

Posted 12 February 2009 - 10:50 PM

Quote

...
// Set the searched index equal to Null
intArray[index] = '\0';



All you are really doing in the above ...

is setting the int value, at that index in the array of integers, to the integer value 0 ...

Since the Lvalue is an integer ... the char Rvalue is cast to the int 0
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1