Thread: Another Simple Array Question

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by guyfromfl View Post
    ++i means you start i as i + what i was, i++ means you start i as what it was then start adding...correct?

    ++i would mean you start the loop as 1 if i=0?
    Not in the slightest. You have to remember what for means:
    Code:
    for(statement1; statement2; statement3)
    {
       statement4;
       statement5;
    }
    means
    Code:
    statement1;
    while (statement2) {
        statement4;
        statement5;
        statement3;
    }
    Pre-incrementing is powerful, but it can't get past two whole other statements. ETA: I mean, the pre-incrementing in statement3 still can't happen all the way before statements 4 and 5.

  2. #17
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    In one of my texts it says the basic code for looping through an array is

    Code:
    int i;
    const int NUM = 10;
    int my_array[NUM]:
    for (i = 0; i < NUM; i++) {
        do something with my_array[i];
    }
    which I thought could be shortened to

    Code:
    int i, my_array[10]:
    for (i = 0; i < 10; i++) {
        do something with my_array[i];
    }
    which is very similar to the code I posted but I can see that even using the "basic code" it doesn't work.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by deadhippo View Post
    In one of my texts it says the basic code for looping through an array is

    Code:
    int i;
    const int NUM = 10;
    int my_array[NUM]:
    for (i = 0; i < NUM; i++) {
        do something with my_array[i];
    }
    which I thought could be shortened to

    Code:
    int i, my_array[10]:
    for (i = 0; i < 10; i++) {
        do something with my_array[i];
    }
    which is very similar to the code I posted but I can see that even using the "basic code" it doesn't work.
    You don't see very well. That code works perfectly, assuming you do something specific with "do something". The problem with your printing is that you don't have anything in your array to print. Arrays are not initialized for you.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    because "do something with my_array" does not suggests to print the garbage values - it suggests to initialize your array first (if you want some reasonable results of course)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    ::ho hum:: i asked where the assigns were earlier. (the 'giving' part)
    but back to a good point...

    and yes your while explanation is quite eye opening.

    im gonna screw with that for a while now. thanks for the heads up

  6. #21
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Quote Originally Posted by tabstop View Post
    You don't see very well. Arrays are not initialized for you.
    Thank you very much. You are right. I didn't realize that the printf wasn't actually doing anything but printing the result of something.

    Here is what I came up with from that piece of advice:

    Code:
        int a[5], i;
    
        for (i = 0; i < 5; ++i){
            a[i] = i + 1;
            printf("%d\t", a[i]);
        }
    I initialized it with i + 1 to give an array of 1 to 5. Added the tab to make it purty.
    Thanks guys. You got me over another hurdle.

  7. #22
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    so wouldnt that mean he just assigned i[1] the first var?

    please tell me how i am wrong with this.

    and as a monday night quarterback my third recommendation was to split the outputs up ("\t or \n")

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by guyfromfl View Post
    so wouldnt that mean he just assigned i[1] the first var?
    That doesn't even mean anything.

    a[0] = 1
    a[1] = 2
    a[2] = 3
    a[3] = 4
    a[4] = 5
    That's what happens. Remember that assignments take the value on the right and put them in the variable on the left.

  9. #24
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    yes i am wrong. and i see whats going on now, i was at the bar too long i guess...i am right about refering arrays but missed the WHOLE part about how the loop made its own numbers as it went.

    im going to bed to sleep the poison out.

    sorry...
    mark

  10. #25
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's got nothing to do with the loop, that's really there to trick you.

    The point is, you're printing the values of uninitialized variables. And in C, when you access an unitialized variable you'll get whatever value was in that memory location last. Unlike Java wish would set it to 0 for you. For example,

    What would
    Code:
    int a;
    print("&#37;d\n", a);
    Show? It's really essiential to understand how memory works to understand why this doesn't work.

  11. #26
    Registered User
    Join Date
    Mar 2008
    Posts
    6

    You need to initialize the array...

    Someone already posted an easier example of this, but when you declare an array you are simply reserving memory. Anything can be in that memory. You need to initialize each of the elements in that array.

    tzuch

    Code:
    int array[5], i;
    
    for(i=0;i<5;i++)
    {
    array[i]=0; //or any other input, usually from the user
    }
    Hope this helps.

  12. #27
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Quote Originally Posted by tzuch View Post
    Hope this helps.
    Thanks for the input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  3. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 10:43 PM
  4. simple array question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 04:46 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM