Iterating through an array, removing an element, then shifting the array

I am trying to iterate through this array, and remove the number 2. Once I remove the number 2, I want to shift 3 and 4 to the left, changing the array size as well. The reason I am asking this is imagine an example problem: "Ask a user to type in a first and last name into a string, then remove the spaces in between the first an last name by iterating through the string." I've looked all over the web for answers and been playing with this for about 4 hours now. Thought I would ask, Thanks.

My output code:
12234

Expected output code:
134


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{

	int myArray[4] = { 1, 2, 3, 4 };



	for (int i = 0; i < (myArray[i]); i++)
	{
	
	cout << myArray[i];

	if (myArray[i] == 2)
	{
		myArray[4] = myArray[i - 1];
		cout << myArray[i];
	}
	
	}

	system("pause");

	return 0;
}
THere is a function is standard library which does exactly what you want. It is a little unwieldy to use with raw arrays as opposed to proper containers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <algorithm>
#include <iostream>
 
int main() 
{
	int arraylen = 4;
	int myArray[4] = { 1, 2, 3, 4 };
	for (int i =0; i < arraylen; ++i)
		std::cout << myArray[i] << ' ';
	std::cout << '\n';
 
	//http://en.cppreference.com/w/cpp/algorithm/remove
	arraylen = std::remove(myArray, myArray + arraylen, 2) - myArray;
 
	for (int i =0; i < arraylen; ++i)
		std::cout << myArray[i] << ' ';
	std::cout << '\n';
}
1 2 3 4 
1 3 4 
http://ideone.com/eh3foU
Last edited on
Greetings,

When you get more into it, you'll learn about vectors, and this won't be an issue, but learning arrays is important!

I just wanted to make some notes about your code though:

This line:
for (int i = 0; i < (myArray[i]); i++)
Will yield some results that you don't want. You're checking to see if the variable i is less than whats at the array at position i. What I assume you're looking to do is run through the array. In that case you want to iterate through the length of the array. What you would want to use is the size function of arrays.

for (int i = 0; i < myArray.size(); i++)

The second thing is here:
1
2
3
4
5
6
	
        if (myArray[i] == 2)
	{
		myArray[4] = myArray[i - 1];
		cout << myArray[i];
	}


The if statement is good, you're looking for the value '2'. However, you're accessing the array at a hard coded position, which can be dangerous if you don't know what you're doing. You're array's size and number of elements are actually different numbers (off by one). For example, you have an array of size 4 but it starts counting at 0 so element 0 is 1, element 1 is 2, element 2 is 3, and element 3 is 4. So what is at element 4 (the position you're trying to access)?

For simple programs you should try and trace out whats going on, line by line. You'll find out whats wrong.

So, to trace this out:
i is 0 so it outputs 1
it fails the if statement and goes back to the top of the loop
i is 1 so it outputs 2
it passes the if statement
it then assigns 1 to position 4 in the array (but you don't have access to it)
then it outputs 2 (i is still 1!)
then it goes back to the top of the loop
i is 2 so it outputs 3
it fails the if statement and goes back to the top of the loop
i is 3 so it outputs 4
it fails the if statement and goes back to the top of the loop
then i is 4 but you don't have access to myArray[4] (its usually garbage)
However, if you were allowed, you assigned 1 to myArray[4] so it fails the while condition of the for loop, and drops out completely.
the program for me segfaulted at this point (Which it should have)




Last edited on
OP's problem is a common introductory homework.

The trick is to understand that you need two indices into the array:

  - a source index, where you copy from
  - a destination index, were you copy to

+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+
  ^src
  ^dest

+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+
      ^src
      ^dest

+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+
          ^src
      ^dest

+---+---+---+---+---+---+
| 1 | 3 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+
              ^src
          ^dest

+---+---+---+---+---+---+
| 1 | 3 | 4 | 4 | 5 | 6 |
+---+---+---+---+---+---+
                  ^src
              ^dest

If you find a value at src that you don't want to keep, just move to the next.

The final count of elements used in the array can be obtained from the dest index when you are done.

Hope this helps.
Topic archived. No new replies allowed.