Hey everyone. I have a question about trying to delete successive elements from an array. For example im trying to delete every other element. Lets say my size is 10, so my elements are 0,1,2,...9. Heres what I want my output to look like:

0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
1 3 5 6 7 8 9
1 3 5 7 8 9
1 3 5 7 9
3 5 7 9
3 7 9
3 9
3

I know how to delete the 1st element but no luck otherwise.
here is my code:

int main()
{
	const int max = 1000;
	int num = 0, kill = 0;

	std::cin >> num;

	int x[max] = {0};

	for (int i = 0; i < num; i++)
	{
		x[i] += i;
		std::cout << x[i] << ' ';
	}
	std::cout << '\n';

	for (int i = 0; i < num; i++)
	{
		if (x[i] == kill)
		{
			while (i < num)
			{
				x[i] = x[i + 1];
				i++;
			}
			num--;
		}
		kill += 2;
	}

	for (int i = 0; i < num; i++)
	{
		std::cout << x[i] << ' ';
	}

	return 0;
}

Recommended Answers

All 9 Replies

I would make a second array then copy the elements you want to keep into it. When done delete all the elements of the original array, then copy the contents of the temp array into the original array. Doing that I believe you will only need two simple loops, one to do the first job and another to do the last job.

I would make a second array then copy the elements you want to keep into it. When done delete all the elements of the original array, then copy the contents of the temp array into the original array. Doing that I believe you will only need two simple loops, one to do the first job and another to do the last job.

do you think that would work if length is based on user input? the max amount of elements I can have are 1000. there are a few other test cases i can show: like if 11 is entered:

0 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 3 4 5 6 7 8 9 10
...
1 3 5 7 9
1 5 7 9
1 5 9
5 9
5

I think you are presenting something a little different than what I had in mind.

The first time around the first number is deleted.
The second time around the second number is deleted.
The third time around the third number is deleted.

That keeps going at the end of the list of numbers, at that time the process starts all over again.

0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 // first number was deleted
1 3 5 6 7 8 9 // second number from above list was deleted
1 3 6 7 8 9 // third number from above list was deleted
1 3 6 7 9 // fourth number from above list was deleted
// Since there is no 5th number to be deleted we start back at the first number
3 6 7 9 // first number from above list was deleted
3 7 9 // second number from above list was deleted
3 7 // third number from above list was deleted
7 // first number from above list was deleted

I think you are presenting something a little different than what I had in mind.

The first time around the first number is deleted.
The second time around the second number is deleted.
The third time around the third number is deleted.

That keeps going at the end of the list of numbers, at that time the process starts all over again.

Thats about what I want. I want to kill off every other number, then when it reaches the end, start over. so in the case where i enter 10, first kill off 0, then 2, then 4, then 6, then 8. Since it is at the end of the line, kill off 1, then 5, then 9, then 7 then 3.

any idea on how to do that? thanks for your help so far.

There are several ways to write that program. Probably one of the simplest is to use vectors and iterators.

Step 1: Create a vector of ints that will hold all the numbers from 0 to ???

Step 2: create a function that displays all the numbers in the vector. Pass the vector to this function by reference.


Step 3: create a vector<int>::iterator that will be used to iterate through the list

Step 4: in a loop call vector's erase() method to erase the value currently pointed to by the iterator, then increment the iterator, Continue this loop until the vector >= to the vector's end() method. On each iterator of the loop call the function you wrote in step #2 above.

There are several ways to write that program. Probably one of the simplest is to use vectors and iterators.

Step 1: Create a vector of ints that will hold all the numbers from 0 to ???

Step 2: create a function that displays all the numbers in the vector. Pass the vector to this function by reference.


Step 3: create a vector<int>::iterator that will be used to iterate through the list

Step 4: in a loop call vector's erase() method to erase the value currently pointed to by the iterator, then increment the iterator, Continue this loop until the vector >= to the vector's end() method. On each iterator of the loop call the function you wrote in step #2 above.

Do u know anyway 2 do it without vectors? I am in an intro c++ class and the most advanced things I know is functions & arrays.

Yes I know. Its done very similar with normal arrays. Instead of using an erase() method I would just mark the array element as deleted, for example by replacing the number with 0, then when displaying the array ignore all elements with 0.

should I use a double for loop? Or use separate for loops?

ok, here is what I got so far. it works great until i get all odds, then it stops working right.

#include <iostream>

int main()
{
	int rebels = 0; 
	std::cin >> rebels; 

	const int max_rebels = rebels;
	int x[1000] = {0}; 
	int num_rebels = rebels, index = 0; 

	for (int i = 0; i < num_rebels; i++)
	{
		x[i] = i;  
	}

	while (num_rebels > 0)
	{
		for (int i = 0; i < num_rebels; i++)
		{
			std::cout << x[i] << ' '; 
		}
		std::cout << '\n';
		
		if (index < num_rebels)
		{
			for (int i = index; i < num_rebels; i++)
			{
				x[i] = x[i + 1];
			}
		}

		index = (max_rebels + 1) % num_rebels; 

		if (index % num_rebels == num_rebels - 1 || index % num_rebels == 0)
		{
			if (max_rebels % 2 == 0)
			{
				index = 0;
			}
			else 
			{
				index = 1;
			}
		} 

		num_rebels--; 
	}

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.