Thread: need help understanding basic arrays

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    40

    need help understanding basic arrays

    Hey everyone..

    So i'm adding arrays to the already long-enough list of C concepts that i don't understand. I looked at the tutorial on this site and i've googled for info on arrays; nothing i have read has helped me understand any better so i was hoping someone here could maybe explain what's happening in layman's terms.

    To be clear, this is not homework, this is just an example practice question that i don't quite understand..

    What is the output of the following program?
    Code:
    Int main() {
    Int array[5] = {3, 6, 2, 7, 1}
    Int I;
     
    For (i=0; i<4; i++)
                   Array[i] = array[i] + aray[i+1]
    For (i=0; i<5; i++)
                   Printf(“%d”, array [i]);
    Return 0;
    }
    now i put this into codeblocks and the output was '98981.' However, i don't understand how. I guess my real question is this: What's going on within the for loops?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by jpatrick View Post
    now i put this into codeblocks and the output was '98981.' However, i don't understand how. I guess my real question is this: What's going on within the for loops?
    The last loop just prints the array. Given:
    Code:
    int array[5] = {3, 6, 2, 7, 1};
    the last array adds the the value of i and i + 1 so the result you're seeing is 3+6 = 9, 6+2 = 8, 2+7 = 9, 7+1 = 8, 1 is the last value and is unaffected since the first loop only loops while i < 4. Don't know how to explain that differently, hope that made sense.
    Last edited by Subsonics; 03-15-2012 at 02:31 PM.

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You have three different spellings of array in here:

    Code:
    For (i=0; i<4; i++)
                   Array[i] = array[i] + aray[i+1]
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    i do understand better now; I mean, i see how you got the output, at least. i guess i was confusing myself after looking at i=0 so my thought process was something like this: since i=0 then 0+0+1=0...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    i know bigH. i was rushing sorry lol. but rest assured i fixed it in codeblocks.. i copied and pasted this from a powerpoint.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    for more clarification.. here's another example that i can't figure out with the same logic because it is a different type of problem..

    Code:
    int main() {
                   int a[] = {8, 1, 6, 3, 5, 7, 2, 4, 10, 9};
                   int i;
     
                                  for (i=0; i<10; i++)
                                  a[i] = a[a[i]-1];
     
                   for (i=0; i<10; i++)
                                  printf("%d ", a[i]);
     
                                  return 0;
    }
    i'm not entirely sure what
    Code:
    a[i] = a[a[i]-1];
    is asking.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Well, let's break it up into parts. First look at a[i]. When i=0, a[i]=8. So a[0]-1 = 7.

    Now we can put that into the entire expression

    Code:
    a[0] = a[a[0]-1]
    a[0] = a[7]
    a[0] = 4
    because 4 is the the value stored at a[7].
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    i see. so then the next value would be a=1, 1-1=0, and 8 is at the 0 value but it has been replaced by 4? so the second value is 4? is that how it works?

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Bingo.
    Code:
    while(!asleep) {
       sheep++;
    }

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    40
    well thanks for the help. i'm pretty sure i understand it now. i'm assuming things might get a little complicated with all the numbers changing so i'll just have to keep track of everything on paper.

    i appreciate it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help understanding pointers and arrays
    By cyclone123 in forum C Programming
    Replies: 11
    Last Post: 02-03-2011, 02:11 PM
  2. Need help understanding basic list code
    By hiven in forum C Programming
    Replies: 26
    Last Post: 05-12-2009, 08:16 PM
  3. help understanding arrays of strings!
    By smoking81 in forum C Programming
    Replies: 18
    Last Post: 02-23-2008, 04:24 AM
  4. Need Help understanding arrays and pointers
    By flicka in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 09:45 PM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM