programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# array for loop question.

Ranch Hand
Posts: 518
4
Hi guys kind of confused on this its hurting my brain just a little and questioning what I know lol,

so here is my program a for loop which assigns a value in intervals of 5(5,10,15,etc) to the indexes of my array.

This seems to work for the first iteration and index 0(arr1[0]) becomes 5 but from the on afterward they only seem to iterate by one so index[1] is 6,[2] is 7 and so on,how would I assign the values in intervals of 5 I was pretty sure that would work it logically looks right to me :s

Liutauras Vilda
Sheriff
Posts: 4918
334
Adam Chalkley wrote:I was pretty sure that would work it logically looks right to me

Why is it not working as you expected then? On a positive side - it does exactly what you are telling to do.

What do you want to do:
1. arr1[0] = 5, arr1[4] = 10, arr1[9] = 15, arr1[14] = 20, arr1[19] = 25 ?
or
2. arr1[0] = 5, arr1[1] = 10, arr1[2] = 15, ... ?

Liutauras Vilda
Sheriff
Posts: 4918
334
At the moment this code would print: 0 1 2 3 .. 19
How would you modify this code to print 0 5 10 15 ?

Ranch Hand
Posts: 518
4
Liutauras Vilda wrote:
Adam Chalkley wrote:I was pretty sure that would work it logically looks right to me

Why is it not working as you expected then? On a positive side - it does exactly what you are telling to do.

What do you want to do:
1. arr1[0] = 5, arr1[4] = 10, arr1[9] = 15, arr1[14] = 20, arr1[19] = 25 ?
or
2. arr1[0] = 5, arr1[1] = 10, arr1[2] = 15, ... ?

number 2 I'm trying to have arr1[0] = 5, arr1[1] = 10, arr1[2] = 15

Ranch Hand
Posts: 518
4
Liutauras Vilda wrote:
Adam Chalkley wrote:I was pretty sure that would work it logically looks right to me

Why is it not working as you expected then? On a positive side - it does exactly what you are telling to do.

What do you want to do:
1. arr1[0] = 5, arr1[4] = 10, arr1[9] = 15, arr1[14] = 20, arr1[19] = 25 ?
or
2. arr1[0] = 5, arr1[1] = 10, arr1[2] = 15, ... ?

number 2 I'm trying to have arr1[0] = 5, arr1[1] = 10, arr1[2] = 15

int[] arr1 = new int[20];

for(int i = 0; i < arr1.length; i++){

arr1[i] = i;
System.out.println(arr1[i] + 5);
}

I tried this even this way I thought arr1[0] = 5 which it does but then when It iterates I would have thought arr1[1] would = 10 as I'm adding 5 each time not 1.

Liutauras Vilda
Sheriff
Posts: 4918
334
Adam Chalkley wrote:I'm trying to have arr1[0] = 5, arr1[1] = 10, arr1[2] = 15

Very difficult to help without telling the answer - i'll try (read comment).
There are more ways to achieve that - this is the first thing came up to my mind.

Greg Zobel
Greenhorn
Posts: 23
1
Liutauras Vilda wrote:
Adam Chalkley wrote:I'm trying to have arr1[0] = 5, arr1[1] = 10, arr1[2] = 15

Very difficult to help without telling the answer - i'll try (read comment).
There are more ways to achieve that - this is the first thing came up to my mind.

I think you've gotten as close to the line as you possibly can without crossing it here. One thing I wonder though Adam, you do understand that if you have a new int array[20], the numbers for the indices will always be 0 to 19 no matter what number you plug into the container they point to? You can do this without using "i" in your calculation.

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
This line in your original code:

in your for loop, you increment i by 1 each time. so, i goes from 0 to 1 to 2 to 3...

So, i + 5 would go from 5 to 6 to 7 to 8...which is what you are seeing.

Let's go back to your algebra days. Do you remember when you had an expression like

3x^2 + 6x +3

and you were told to simplify it? What would you do first? (hint: it has something to do with a 3).

Now apply the same logic to the values you want in the end...5 10 15...etc.

Ranch Hand
Posts: 518
4
fred rosenberger wrote: . . . Let's go back to your algebra days. . . . Now apply the same logic to the values you want in the end...5 10 15...etc.

Thanks to all guys I'm going to figure this out now

Guru Hiremath
Greenhorn
Posts: 1
Hi guys i am new to coderanch.Is it permittable here to post the complete solution program?

Campbell Ritchie
Marshal
Posts: 56546
172

We don't usually permit complete solutions because they would stop the discussion developing and they may short‑circuit the poster's thought process so they don't come to the answer themselves. Remember that an answer one works out for oneself will be much easier to remember than what one has simply been told. There are some websites e.g. stackoverflow where people go specifically for complete solutions, but our philosophy here is different.
There are a few instances where I do give the complete solution: who is going to think of
unless they have been told about it?

I this case, however, though others might have different opinions, I think the solution would be only one line and AC has had much time to think about it, so I shall make an exception. Please go ahead and post the solution.

And welcome to the Ranch and thank you for showing the consideration to ask before posting