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]);
Printable View
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?
why so?Quote:
start with post incrementing i,
wouldn't that start with i[1] not i[0]
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?Quote:
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};
++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
Not in the slightest. You have to remember what for means:
meansCode:for(statement1; statement2; statement3)
{
statement4;
statement5;
}
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.Code:statement1;
while (statement2) {
statement4;
statement5;
statement3;
}
In one of my texts it says the basic code for looping through an array is
which I thought could be shortened toCode:int i;
const int NUM = 10;
int my_array[NUM]:
for (i = 0; i < NUM; 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.Code:int i, my_array[10]:
for (i = 0; i < 10; i++) {
do something with my_array[i];
}
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)
::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
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:
I initialized it with i + 1 to give an array of 1 to 5. Added the tab to make it purty.Code:int a[5], i;
for (i = 0; i < 5; ++i){
a[i] = i + 1;
printf("%d\t", a[i]);
}
Thanks guys. You got me over another hurdle.
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")
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
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
Show? It's really essiential to understand how memory works to understand why this doesn't work.Code:int a;
print("%d\n", a);
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
Hope this helps.Code:
int array[5], i;
for(i=0;i<5;i++)
{
array[i]=0; //or any other input, usually from the user
}