Thread: this is fun but Im new and confused

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    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. #4
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    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. #5
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25

    Question 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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #7
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #9
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #11
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    i see what you mean
    so then how about this
    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. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know why you are so morally opposed to
    Code:
    for (first = 0 ...
    since that's where you have to start.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    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. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #15
    Registered User
    Join Date
    Oct 2008
    Location
    new york
    Posts
    25
    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.

Popular pages Recent additions subscribe to a feed