Another Simple Array Question

This is a discussion on Another Simple Array Question within the C Programming forums, part of the General Programming Boards category; Why does this program not work? Where does it go wrong. I am trying to print out the array. Code: ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    Another Simple Array Question

    Why does this program not work? Where does it go wrong. I am trying to print out the array.

    Code:
        int a[5], i;
    
        for (i = 0; i < 5; ++i)
            printf("%d", a[i]);

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    But it does work, it just probably doesn't do what you want it to.

    It's obvious you're asking homework questions.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Sorry, it's a number but it doesn't print the array which is what I'm trying to do.

    I'm not a school kid but I am using a text book but it's not homework. Unfortunately I don't have a teacher so I have to rely on boards like these. So please don't make it more difficult than it already is by discouraging people from helping me with misguided comments. I understand the point you are trying to make but I need this.

    My text book has the answer but I really need to know why it doesn't work, not simply that the output is garbage (which is the answer in my text.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well look at the code. What do you think a[0] is?

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    start with post incrementing i, also i dunno if you meant to but what about the brackets around the for loop.

    what output are you getting compared to what you want?

    and what are you giving each element of the array?
    Last edited by guyfromfl; 03-29-2008 at 12:05 AM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    start with post incrementing i,
    why so?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    wouldn't that start with i[1] not i[0]

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Quote Originally Posted by guyfromfl View Post
    wouldn't that start with i[1] not i[0]
    no it wouldn't

    the 3rd part of the for statement is alculated after the loop iteration, there is absolutely no difference between pre or post increment (in C). In C++ pre-increment is even more suitable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    quite interesting... im using my java experience and combining it with c's and will def. check that out. because i know in jscript that would be a different result.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Quote Originally Posted by tabstop View Post
    Well look at the code. What do you think a[0] is?
    I though it would be 0 but I can see that isn't the case.

    start with post incrementing i, also i dunno if you meant to but what about the brackets around the for loop.

    what output are you getting compared to what you want?

    and what are you giving each element of the array?
    Will post incrementing have a different effect on this loop and do I really need the brackets in this case?

    The output is a huge number which I take to be an overflow error. I thought it would produce a list of five numbers 0 - 4 which are produced by the for loop.

    Basically this is a question in the text and it says the answer is: "Garbage", but I can't understand what element I am missing that turns this program into garbage.

  11. #11
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Code:
        int a[5], i;
    
    /* i hope you initialized the array before this lol haha :D */
        for (i = 0; i < 5; ++i)
            printf("%d ", a[i]); /* a space was not being printed. fixed :D */

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Code:
    int a[5] ={0};
    this declares an array AND initializes all its members with zeros
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by guyfromfl View Post
    quite interesting... im using my java experience and combining it with c's and will def. check that out. because i know in jscript that would be a different result.
    I never used it that much, but I'm pretty sure it's the same in jscript.

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    20
    ++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?

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    guyfromfl: No!
    The i++ part is executed after the condition check, and after a loop if the condition is true. lol haha lol

Page 1 of 2 12 LastLast
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, 12:59 PM
  2. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  3. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 09:43 PM
  4. simple array question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21