Help with Array

Apr 30, 2013 at 5:12am
Super quick question about arrays and I am sure it has a very easy answer lol.

Can I add to an array that is defined by an int. I will give an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (ix = 0; ix < count; ++ix)
{
        numDifference = abs(highTemp[ix] - highTemp[ix + 1]);

	if (numDifference > maxValue)
	{
		largestDiff = numDifference;

		dayOne = day[ix];

		dayTwo = day[ix + 1];
	}
}


the [ix + 1] is that valid?

thanks in advance
Apr 30, 2013 at 5:25am
It is valid, but it won't do what you think because of the next iteration of the for loop. This has the effect of overwriting your info.

Maybe you need 2 arrays - which is probably the more sensible thing to do.
Apr 30, 2013 at 5:35am
okay so let me make sure I understand what you are saying to do.

I want to make sure that the second array call ( highTemp[ix + 1] ) doesn't overwrite my first array.. so I should make a copy of my highTemp to another temporary array then replace the highTemp[ix + 1] with my temporary array?
Apr 30, 2013 at 6:24am
ix+1 is valid only if the arrays have such element. If an array has size count (or less), then on last iteration ix=count-1 and therefore ix+1 is not valid.
Apr 30, 2013 at 6:36am
that makes a ton of sense thanks for clearing that up.. how would I be able to do something like that without getting the final error of ix+1 not being valid?

Apr 30, 2013 at 7:17am
Iterate one element less.

Btw.maxValue and largestDiff .. intentional?
Apr 30, 2013 at 7:22am
nope that is an error haha thank you for pointing that out
Topic archived. No new replies allowed.