# Thread: this is fun but Im new and confused

1. ## this is fun but Im new and confused

so here is something simple I can not get to work. What going on?
basically I am given an array temps
double temps
n = the number of elements in the array
i want to write a loop to create the average of the figures in the array temps.

I declared two more variables k and temp
here is what I have so far
Code:
for( k=0;k<=n; k++) {
total++;

aveTemp = temps[total] / n;
}

2. Total starts out as some random number (unless you set it to 0 and didn't tell us). You then add 1 to it, n+1 times, so now total is (random number)+n+1. You then try to use that as an array index into temps, which is just so not going to work, and then you divide by n.

3. okay so lets say i declare total to 0 when I initialize.
Basically I just want to use total as a temp holder to do the reverse.
the way i see it is. I have the for loop that is reversing the data stored in n and stores that in total.
do i need 2 loops then?
all i want to do is get the sum of the N values and then divide by the number of n values right?

so the for loop i have gives me the sum of the n values right? and then i should just divide by N no?
What am I missing

4. Hey Tabstop I got it. One I declared total to 0 it all clicked. Thanks for pointing that out.
here is what i used
Code:
total = 0;

for (k = 0; k<n; k++)
total += temps[k];

avgTemp = total / n;

5. ## stumped again

so now I'm stumped again with another problem. IN this case if to reverse the array.
Code:
for (k=n-1; k>=n;k--) {
temp = a[k];
a[n] = temp;
}
This is what i have thus far. So I have an array a and n is the number of elements in array a
so
a[n]

so what i tried to do here was reverse the order of the elements in n and assign them to temp. i feel like another step is needed but im not sure what it is? Any help much appreciated thanks

6. Swapping two elements (which is what you need to do) requires three = signs.

Also, your conditions on the for-loop are all over the place.

7. really what wrog with the for loop? I thought that was the part that was correct? I reeally dont see a problem. it just reverse the elements of N?

8. Well, it will never happen, is what's wrong with it. You set k to be n-1, but the loop will only continue while k is bigger than n.

9. i dont know
this is what i am doing now .still not getting it.
Code:
for (k=n-1; k>=n;--k){
temp = k;
k = n;
a[n] = k;
}
I want the loop seems to be written for what i want. It should start a the end of the elements of N and work its way down to just above N. When it become less then N it should stop

10. Let's walk through this for a minute, and let's suppose n is 10.
1. Set k = n-1 --> k = 9
2. While k >= 10
some stuff
3. Decrease k by 1
How many times do you think this is going to loop?

Once we get this straight, we'll look at what's going on inside the for loop.

11. i see what you mean
Code:
for(k=a[n] -1; k>=n; k--)
in this case now k is = to the last in the array rather then just a variale n
right?

12. I don't know why you are so morally opposed to
Code:
for (first = 0 ...
since that's where you have to start.

13. I guess i dont get it. From my thinking I have to start at the last of the elements of n and work in reverse order. So starting at 0 would be starting at the first element of N not the last.
Its the concept of the array that is confusing me

14. If you think you can reverse an array (in place, not copying to a different array) without working both ends against the middle, you're crazy. When you put the last entry in the 0 slot, you're also going to have put the 0 entry in the last slot.

15. Actually I dont think anything. I am learning this as I do it so I have no idea what you can and can not do.