Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:40:05+00:00 2026-05-31T04:40:05+00:00

I am a relative beginner with C. I have written what I think is

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:40:07+00:00Added an answer on May 31, 2026 at 4:40 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a relative beginner at git - I have two branches: master and
Questions - (Relative Code Below) I have a custom class which is a BindableDictionary().
I'm a relative beginner with WPF so please bear with me. I have a
My problem is relative simple, but I'm not getting much luck with searches for
I have developed Relative layout for 3.7 droids with Relative layout and difficult relations
I am having an issue with a string manipulation method I have written. The
I'm a relative beginner with OpenGL (I'm not counting the ver. 1.1 NeHe tutorials
Relative newbie to php, running into an issue with namespaces. Inside my own class
Multilevel relative import I have following folder structure top\ __init__.py util\ __init__.py utiltest.py foo\
I'm a relative beginner to PHP and I'm having an issue with a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.