Mark Miller
Mark Miller

Reputation: 13113

C: iterate with for-loops, a ceil statement and odd vs even statements

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.

Upvotes: 0

Views: 308

Answers (4)

technosaurus
technosaurus

Reputation: 7802

The variable names meant nothing to me so I just changed them to a b and (and c wasn't needed) and used while loops instead (just personal preference). It looks like the object of this lesson is to find the pattern using mod and/or div (column 3 is column a + column b div 2)

#include <stdio.h>

int main(){
int a=0, b, num=6;
while (a++<=num) { b=0;
    while (b++ <= (num-a))
        printf("cohort is:  %d class  is:  %d season is:  %d\n", a, b,(a+b)/2);
}
return 0;
}

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78923

You should avoid the call to ceil completely, there is no need to convert things to double, make a function call and convert back. If I see this correctly integer arithmetic with (mcoht+1)/2 would be completely sufficient.

Upvotes: 1

Blaz
Blaz

Reputation: 3618

And few more things besides the ceil function:

  1. Statement season2 = season; is superfluous, because the season is already in the scope of the inner for loop.
  2. You do not need doubles.
  3. As per C99 standard, you shouldn't omit the function's (main's in your case) return type. And although you can omit main's parameters, you'd better put void there.
  4. Adding a few spaces around the operators would help as well.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340218

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/2 gives a result of 0, not 0.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:

ceil(mcoht/2)

try

ceil(mcoht/2.0)

Upvotes: 1

Related Questions