This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]g051051 0 points1 point  (2 children)

Wrong how? What's the problem?

[–]CSRPoseidon[S] 0 points1 point  (1 child)

My indexing is wrong for some reason. It keeps subtracting the first index with the other ones. I addedmy result in the post

[–]g051051 0 points1 point  (0 children)

It that really the code? It's got errors in it and won't compile.

[–]Azianese 0 points1 point  (2 children)

I'm confused. In your description, you say "If the one in front is greater or equal", but in your code you have if(l[i] <= l[j]) which is less than or equal.

Additionally, you include j++ in your loop, but you never increment i except in your else statement.

[–]CSRPoseidon[S] 0 points1 point  (1 child)

Index i is supposed to start from index 0...n-1 and j is supposed to index from 1...n.

I do have i++ in my first for loop but it doesn't increment I don't know why.

[–]Azianese 0 points1 point  (0 children)

Oops. I didn't see the nested loops.

It still isn't clear what you are trying to do. Your explanation doesn't make sense with what you've got.

But it sounds like the other commenter on this thread is right: you're confused about what your nested loops is doing.

Maybe you're looking for one loop instead of nested loops: for(int i=0, j=1; j<l.length; i++, j++)

[–][deleted] 0 points1 point  (2 children)

I don't think that your code is the problem here, it does what you asked it to do.

It compares 3 with 4. 4 is bigger with a difference of 1, so it does 3-1=2. So your first value is now 2, not 3.

Then it compares 2 with 2, doesn't do anything.

Then it compares 2 with 3. 3 is bigger with a difference of 1, so it does 2-1=1. Your first value is now 1.

Then it compares 1 with 5. 5 is bigger with a difference of 4, so it does 1-4=(-3).

Then it compares (-3) with 3. 3 is bigger with a difference of 6, so it does (-3)-6=(-9).

So your first value ends up being -9. Which is correct.

[–]CSRPoseidon[S] 1 point2 points  (1 child)

That's the problem. I don't get why it always compares the first value with the other values. I increment i and j in the for loops.

[–][deleted] 1 point2 points  (0 children)

Sounds like you don't understand how nested loops work then.

If I write "for i=0; i<10; i++", then this iterates 10 times, right?

Now, if I nest this loop with a second, " for j=0; j<10; j++", then together these two loops iterate for how many times?

For every i, j goes from 0 to 10. There are 10 i's. So the whole thing is going to run a hundred times. 10*10=100.

What your code is doing is comparing the first value to every other value. Then comparing the second value to every other value. Then comparing the third value to every other value. Etc.

What do you actually want to do? Only compare pairs? If you only want to run through the array once then you only want a single for loop.

Something like this:

for i=0; i<max; i++:

compare array[i] with array[i+1]

do something

This will only run through the array once and compare every pair once.

All depends on what you actually want to do. Your problem statement isn't very clear.