NO

Author Topic: Postfix increment operator problem  (Read 2954 times)

EAJ1569

  • Guest
Postfix increment operator problem
« on: July 29, 2012, 06:19:40 PM »
Cant get the postfix increment operator to work correctly for the cpop ?
After compiling and running it it displays the "next years population prediction in the spot for 2012.
...
Code: [Select]
#define SPOP 9870 /* Starting population for calculation */
#define PRCT .10 /* The annual percentage growth in population */
#define POP_MAX 30000 /* The maximum population to be reached */


int
main(void){
/* Variable declarations */
int cpop, /* current pop */
year; /* year */

/* display header for population */
printf("     POPULATION     YEAR\n\n");


year = 2012;

/* Display the table */
for ( cpop = SPOP;
cpop <= POP_MAX ;
cpop++) {
cpop = cpop + cpop * PRCT;

printf("%11c%d%4c%5d\n", ' ', cpop, ' ', year);
year += 1;
}
/* calculate total year */
year = year - 2012;
/* Display total number of years */
printf("The total year it takes to reach a population in excess of 30000 is %d\n", year);

return(0);
}
« Last Edit: July 29, 2012, 08:41:06 PM by Stefan Pendl »

CommonTater

  • Guest
Re: Postfix increment operator problem
« Reply #1 on: July 29, 2012, 07:00:16 PM »
Cant get the postfix increment operator to work correctly for the cpop ?
After compiling and running it it displays the "next years population prediction in the spot for 2012.
...

Code: [Select]
#define SPOP 9870         /* Starting population for calculation */
#define PRCT .10         /* The annual percentage growth in population */
#define POP_MAX 30000      /* The maximum population to be reached */


int
main(void){
   /* Variable declarations */
   int      cpop,         /* current pop */
         year;         /* year */

   /* display header for population */
   printf("     POPULATION     YEAR\n\n");

   
   year = 2012;
   
   /* Display the table */
   for      ( cpop = SPOP;
          cpop <= POP_MAX ;
          cpop++) {
       cpop = cpop + cpop * PRCT;
     
        }
   /* calculate total year */
    year = year - 2012;
   /* Display total number of years */
   printf("The total year it takes to reach a population in excess of 30000 is %d\n", year);

   return(0);
}

That's likely because you are doing the next year calculation and printing it before you are incrementing the year... Try moving the cpop =  line below the printf() line...
 
Also you are mainpulating cpop in your loop counter and in your calculations... this will give wrong answers.
 
Finally the calcluation itself can give you wrong answers...
Code: [Select]
cpop = cpop + cpop * PRCT;  // 10 + 10 = 20    20 * 0.1 = 2
You should bracket the expression to control the order of evaluation...
Code: [Select]
cpop = cpop + (cpop * PRCT); // 10 * 0.1 = 1   10 + 1 = 11
Or you could write it as...
Code: [Select]
cpop += cpop * PRCT;

You might better write you loop like this...
Code: [Select]
year = 2012;
cpop = SPOP;
do
 {
     printf("%11c%d%4c%5d\n", ' ', cpop, ' ', year);
     cpop += cpop * PRCT;
     year++;
  }
while (cpop < POP_MAX);

This eliminates double manipulations of the variable and exits when the max is reached.
 
If you are stuck using a for() loop ...
Code: [Select]
year = 2011;
 
for (cpop = SPOP; cpop < MAX_POP; cpop += cpop * PRCT)
  {
     year++;
     printf("%11c%d%4c%5d\n", ' ', cpop, ' ', year);
  }

 

Hope that helps...
 
« Last Edit: July 29, 2012, 07:48:35 PM by CommonTater »

EAJ1569

  • Guest
Re: Postfix increment operator problem
« Reply #2 on: July 29, 2012, 08:16:05 PM »
 ;D Awsome that helped and I worked in your other suggestions... I see how they work small program now. THANKS!!

CommonTater

  • Guest
Re: Postfix increment operator problem
« Reply #3 on: July 29, 2012, 08:31:08 PM »
;D Awsome that helped and I worked in your other suggestions... I see how they work small program now. THANKS!!

C, like most programming languages, is all about "order of execution"...
Do stuff in the wrong order and you get wrong answers.   :D

Glad to help.