Trace Table for an Arrary

Feb 6, 2014 at 2:57pm
int marr[6]={23,67,12,9,33,7};
int track =0;
int ans = 2;

while(track<7)
{
marr[track] = marr[track] + 3;
if(marr[track]<20)
ans = marr[track] / ans;
}
Last edited on Feb 6, 2014 at 5:49pm
Feb 6, 2014 at 3:33pm
So far I realize that when it enters the while loop, track will be 0, marr[track] will be marr[3]


no.

that line will set the first element in your array from 23 to 26.

you never change the value of track so you'll have an infinite loop, or possibly a div by zero exception due your last line in main()
Last edited on Feb 6, 2014 at 3:35pm
Feb 6, 2014 at 3:53pm
i didn't even realize that. for some reason I thought track would be 3 (3+0).Thank you for the clarification.

so when I reach the If statement, will it be If 26<20? which would be false, therefore ans=0/2.Is this a correct explanation mutexe

Thanks
Feb 6, 2014 at 4:51pm
not really no.
will it be:
if(26<20)
Yes.

will that be
false
Yes.

therefore ans=0/2

No.

Because the condition fails, it won't run into your if condition, and ans does NOT get re-assigned and therefore ans will remain 2 (which is what you initialized it with).

also: each iteration of your loop will add 3 to the first element of the array, so it'll go:
29, 32, 35, 38


FOREVER.
*but* you'll hit the maximum value for an integer, get wrap around, and then get a division by zero exception.

You should step through this in a debugger rather than theorizing about the process.

Last edited on Feb 6, 2014 at 4:57pm
Feb 6, 2014 at 5:16pm
Thank you very much for this beautiful explanation. I really appreciate it
Feb 6, 2014 at 5:47pm
Thank you very much for this beautiful explanation. I really appreciate it


If you really appreciate it, you won't modify your posts so that others happening upon this thread won't have the context to decipher what went on here. Your last post in combination with the OP edit is essentially saying "In thanks for your help, I'm making this thread useless for anyone else."
Feb 6, 2014 at 5:52pm
So true. I must apologize cire.
Topic archived. No new replies allowed.