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]);
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]);
But it does work, it just probably doesn't do what you want it to.
It's obvious you're asking homework questions.
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.
Well look at the code. What do you think a[0] is?
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.
why so?start with post incrementing i,
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
wouldn't that start with i[1] not i[0]
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
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 though it would be 0 but I can see that isn't the case.
Will post incrementing have a different effect on this loop and do I really need the brackets in this 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?
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.
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 */
this declares an array AND initializes all its members with zerosCode:int a[5] ={0};
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
++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?
guyfromfl: No!
The i++ part is executed after the condition check, and after a loop if the condition is true. lol haha lol