# Thread: Another Simple Array Question

1. ## 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. But it does work, it just probably doesn't do what you want it to.

It's obvious you're asking homework questions.

3. 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. Well look at the code. What do you think a[0] is?

5. 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?

why so?

7. wouldn't that start with i[1] not i[0]

8. Originally Posted by guyfromfl
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

9. 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. Originally Posted by tabstop
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. 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. Code:
`int a[5] ={0};`
this declares an array AND initializes all its members with zeros

13. Originally Posted by guyfromfl
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. ++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. guyfromfl: No!
The i++ part is executed after the condition check, and after a loop if the condition is true. lol haha lol

Popular pages Recent additions