Array Trouble

closed account (365X92yv)
EDIT: How about this...... if I have an array that has a garbage value on the odd increments, and I want to get rid of all the odds and have the even values one after another, what would I do?


Example:

OrigArray= {1,0,2,0,3,0,4,0,5,0,6};

NewArray = {1,2,3,4,5,6};
Last edited on
closed account (365X92yv)
I don't get why an easy as hell question like this can't be answered but some of these other dumb questions get immediate responses. Seriously.
Write a for loop that searches through every value of the array, and checks to see what var % 2 is equal to. If it equals one, it's odd, so you would want to set that value to NULL. If it equals zero it's even, so the var would stay the same.

Please have some patience. A lot of questions are asked on this site, and sometimes it takes some time to get yours answered.
Last edited on
closed account (365X92yv)
If its set to NULL, it would still effect my array. It would be {1, NULL, 2, NULL}. Wouldn't that have the same effect as {1, 0, 2, 0, 3, 0}? My point is I'm trying to cut an array in half by skipping every odd value. So if my original array is 32512, then I would have an end result array that would have all the even values in the first 16256 spots of the array. What I'm saying is if you go in and move every variable up one, eventually the number of zeroes grouped together would be so massive, it'd most likely need recursion or something to get it to work.
How about something like this...
1
2
3
4
5
6
7
8
9
int j = 0;
	for(int i = 0; i < array1.size(); ++i)
	{
		if(array1[i] != NULL)
		{
			array2[j] = array1[i];
			++j;
		}
	}

Last edited on
why would you want to clean up the array? its not like it has a huge amount of RAM allocation and the information is put into it somewhere. also array1.size();.size() works for strings and vectors, not arrays.
Use a vector instead:
1
2
3
4
5
6
std::vector <int> myvector;
// ...
for (std::vector<int>::iterator it = myvector.begin(); it < myvector.end(); ++it) {
        if ((it - myvector.begin()) % 2 != 0)
                myvector.erase(it);
}
ui uiho wrote:
.size() works for strings and vectors, not arrays.

Sigh, I am aware of that. The purpose was to give the op an idea to solve the issue at hand.
If you use pseudocode, make it clear. You WILL get people frustrated thinking that's actual code otherwise.
Topic archived. No new replies allowed.