From: amelia jane on 17 May 2010 20:04 Hello folks, I have the following pseudo code: #define MY_DEF 10 void my_func(void) { int i, loopcount = 0, offset = 6; for (i = 0; i< ((MY_DEF-offset)/2); i++) { loopcount++; } printf("loopcount=%d\n",loopcount); } The above code, instead of printing loopcount=2 prints loopcount=1; If I make the following modification to the code: void my_func(void) { int i, loopnumber = 0; loopcount = 0, offset = 6; loopnumber = (MY_DEF-offset)/2; for (i = 0; i< loopnumber; i++) { loopcount++; } printf("loopcount=%d\n",loopcount); } This code prints out loopcount=2. I am a bit confused on why this is the case. Any thoughts on this are welcome.
From: Patricia Shanahan on 17 May 2010 20:15 amelia jane wrote: > Hello folks, > > I have the following pseudo code: The term "pseudo code" suggests that what you posted is not *exactly* what you ran. This type of problem depends on details, so it might be better to select a programming language, and post a complete program in that language that shows the effect you want to understand. Patricia
From: Dann Corbit on 17 May 2010 20:57 In article <b1c7d97c-afba-4741-813e-7cd9819a6b31 @v12g2000prb.googlegroups.com>, ameliajane42(a)gmail.com says... > > Hello folks, > > I have the following pseudo code: > > #define MY_DEF 10 > > void my_func(void) > { > > int i, loopcount = 0, offset = 6; > for (i = 0; i< ((MY_DEF-offset)/2); i++) > { > loopcount++; > } > > printf("loopcount=%d\n",loopcount); > } > > The above code, instead of printing loopcount=2 prints loopcount=1; > > If I make the following modification to the code: > > void my_func(void) > { > > int i, loopnumber = 0; loopcount = 0, offset = 6; > > loopnumber = (MY_DEF-offset)/2; > for (i = 0; i< loopnumber; i++) > { > loopcount++; > } > > printf("loopcount=%d\n",loopcount); > } > > This code prints out loopcount=2. > > I am a bit confused on why this is the case. Any thoughts on this are > welcome. #include <stdio.h> #include <stdlib.h> #define MY_DEF 10 void my_func0(void) { int i, loopcount = 0, offset = 6; for (i = 0; i < ((MY_DEF - offset) / 2); i++) { loopcount++; } printf("loopcount=%d\n", loopcount); } void my_func1(void) { int i, loopnumber = 0; int loopcount = 0, offset = 6; loopnumber = (MY_DEF - offset) / 2; for (i = 0; i < loopnumber; i++) { loopcount++; } printf("loopcount=%d\n", loopcount); } int main(void) { my_func0(); my_func1(); return 0; } /* After removal of obvious syntax errors, I get this: loopcount=2 loopcount=2 */
From: pete on 17 May 2010 23:22 amelia jane wrote: > > Hello folks, > > I have the following pseudo code: > > #define MY_DEF 10 > void my_func(void) > { > > int i, loopnumber = 0; loopcount = 0, offset = 6; > > loopnumber = (MY_DEF-offset)/2; > for (i = 0; i< loopnumber; i++) > { > loopcount++; > } > > printf("loopcount=%d\n",loopcount); > } > > This code prints out loopcount=2. > > I am a bit confused on why this is the case. Any thoughts on this are > welcome. loopnumber is assigned a value of two. The loop executes twice. loopcount starts at zero and increments twice. -- pete
From: amelia jane on 19 May 2010 01:51
On May 17, 8:22 pm, pete <pfil...(a)mindspring.com> wrote: > amelia jane wrote: > > > Hello folks, > > > I have the following pseudo code: > > > #define MY_DEF 10 > > void my_func(void) > > { > > > int i, loopnumber = 0; loopcount = 0, offset = 6; > > > loopnumber = (MY_DEF-offset)/2; > > for (i = 0; i< loopnumber; i++) > > { > > loopcount++; > > } > > > printf("loopcount=%d\n",loopcount); > > } > > > This code prints out loopcount=2. > > > I am a bit confused on why this is the case. Any thoughts on this are > > welcome. > > loopnumber is assigned a value of two. > The loop executes twice. > loopcount starts at zero and increments twice. > > -- > pete Thanks for the responses so far. For the benefit of everyone, here is the original piece of code in C: #define MY_DEF 10 void my_func(void) { int i; int loopcount; int offset = 6; loopcount = 0; for (i = 0; i< ((MY_DEF-offset)/2); i++) { loopcount++; } printf("loopcount=%d\n",loopcount); } main() { my_func(); } I still don't get why loopcount equals 1; |