I am a relative beginner with C. I have written what I think is a fairly simple
C program to iterate a variable ‘season’ inside two for-loops. The code compiles
and runs, but ‘season’ is not being tracked correctly. I am hoping someone may be
willing to offer suggestions on how to correct the code.
Below I provide the results I would like, the current C code, and the results that code
is returning.
I would like the following results:
cohort = 1, class = 1, season = 1
cohort = 1, class = 2, season = 1
cohort = 1, class = 3, season = 2
cohort = 1, class = 4, season = 2
cohort = 1, class = 5, season = 3
cohort = 1, class = 6, season = 3
cohort = 2, class = 1, season = 1
cohort = 2, class = 2, season = 2
cohort = 2, class = 3, season = 2
cohort = 2, class = 4, season = 3
cohort = 2, class = 5, season = 3
cohort = 3, class = 1, season = 2
cohort = 3, class = 2, season = 2
cohort = 3, class = 3, season = 3
cohort = 3, class = 4, season = 3
cohort = 4, class = 1, season = 2
cohort = 4, class = 2, season = 3
cohort = 4, class = 3, season = 3
cohort = 5, class = 1, season = 3
cohort = 5, class = 2, season = 3
cohort = 6, class = 1, season = 3
My C code follows immediately below and after that I present the
results returned by the code:
#include <stdio.h>
#include <math.h>
char quit;
main()
{
int mcoht, mclass, season, nmcohts ;
double season2, cohort2, class2 ;
nmcohts = 6 ;
// cohort loop
for (mcoht=1; mcoht <= nmcohts; mcoht++) {
season = ceil(mcoht/2) ;
// class loop
for (mclass=1; mclass <= (nmcohts-(mcoht-1)); mclass++) {
season2 = season ;
cohort2 = mcoht ;
class2 = mclass ;
printf("cohort is: %10.10lf\n", cohort2);
printf("class is: %10.10lf\n", class2);
printf("season is: %10.10lf\n", season2);
// update season (mclass&1) = 0 if mclass even, 1 if mclass is odd
if(((mcoht&1) + (mclass&1)) == 1) season = season + 1;
} // close class loop
} // close cohort loop
printf("To close type 'quit' and hit the return key\n");
printf(" \n");
scanf("%d", &quit);
return 0;
}
Here are the results returned by the code. Unless I made a mistake
typing the lines below it looks like ‘season’ is correct for the
even-numbered cohorts, but is 1 less than needed with the odd-numbered
cohorts.
Thank you for any suggestions either regarding how to correct the code or how
to improve this post.
cohort = 1, class = 1, season = 0
cohort = 1, class = 2, season = 0
cohort = 1, class = 3, season = 1
cohort = 1, class = 4, season = 1
cohort = 1, class = 5, season = 2
cohort = 1, class = 6, season = 2
cohort = 2, class = 1, season = 1
cohort = 2, class = 2, season = 2
cohort = 2, class = 3, season = 2
cohort = 2, class = 4, season = 3
cohort = 2, class = 5, season = 3
cohort = 3, class = 1, season = 1
cohort = 3, class = 2, season = 1
cohort = 3, class = 3, season = 2
cohort = 3, class = 4, season = 2
cohort = 4, class = 1, season = 2
cohort = 4, class = 2, season = 3
cohort = 4, class = 3, season = 3
cohort = 5, class = 1, season = 2
cohort = 5, class = 2, season = 2
cohort = 6, class = 1, season = 3
I put code-sample quotes around the results only to display them as columns. I do not need the output to be formatted as shown. I simply need ‘season’ to be calculated correctly.
I’m surprised this hasn’t been answered in half an hour…
The problem you’re having with the
ceil()function is that you’re using integer operands for the division. The way C does division with operands that are both integral is that it will produce an integer result, truncating any fractional part.Therefore,
1/2gives a result of0, not0.5.If you want a floating point result, you need to force at least one of the operands to be floating point. So instead of:
try