Thread: Program with daynumber help

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    1

    Program with daynumber help

    I'm trying to write a c program that determines the day number (1 to 366) in a year for a date provided by a user.
    How do I adjust this program for a leap year like (december 31,2012) is day 366 since it was a leap year. Is there a better way(I really want to keep the functions and the switch ? I am also getting an error (expected primary expression before token on line 82)This is my code so far please help


    Code:
      #include <stdio.h>
                       
                       int isleap(int year);
                
                       void  displaydate(int month ,int date ,int year,int daynumber);
    
                       int main()
    
                      {
        
                           int month;
                           int day;
                           int year;
                           int leapyear;
                           int daynumber;
                           
                           
                           
                          printf(" enter a month");
                          scanf("%d",&month);
     
                         printf(" enter a day");
                         scanf("%d",&day);
     
                        printf(" enter a year");
                        scanf("%d",&year);
     
                        leapyear= isleap( year);
                        
                         daynumber = leapyear && month > 2 ? day+1 : day;
                 
                 
                           switch(month)
                           {
                               
                         case 1 : daynumber = day;
                       break;
                     
                      case 2 : daynumber = 31 + day;
                       break;
                      
                      case 3 : daynumber = 59 + day + 1; 
                      break;
                      
                      case 4 : daynumber = 89 + day;
                      break;  
                      
                      case 5 : daynumber = 120 + day;
                      break;  
                     
                      case 6 : daynumber = 150 + day;
                      break; 
                     
                      case 7 : daynumber = 181 + day;
                      break;  
                      
                      case 8 : daynumber = 212 + day;
                      break;   
                     
                      case 9 : daynumber = 232 + day;
                      break;  
                      
                      case 10 : daynumber = 263 + day;
                      break;  
                      
                      case 11 : daynumber = 293 + day;
                      break;  
                     
                      case 12 : daynumber = 324 + day;
                      break;  
                      
                      default:
              
                               
                               
                           }
                
        
               return 0;
          }
          
          
          
                     int isleap(int year)
                     {
                         
                         if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
                        return 1;
                        
                        else  
                        return 0;
                     }
                     
                     
                      void  displaydate(int month ,int date ,int year,int daynumber)
                      {
                          
                          printf("\n your date is %d and the day number is %d",month,date,year,daynumber);
                     
                      }

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by tammysmith View Post
    I'm trying to write a c program that determines the day number (1 to 366) in a year for a date provided by a user.
    How do I adjust this program for a leap year like (december 31,2012) is day 366 since it was a leap year. Is there a better way(I really want to keep the functions and the switch ? I am also getting an error (expected primary expression before token on line 82)This is my code so far please help
    You should check the input from user.
    Code:
    …
    int days_per_month[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    …
    
    // after the user entered the input in main
    
    // check year
    if (year < 1601) {
        fprintf(stderr, "The gregorian calendar does not exist in year %d! exit.\n", year);
        return 1;
    }
    
    leapyear = isleap(year);
    
    // check month
    if (month < 1 || month > 12) {
        fprintf(stderr, "Invalid month %d! exit.\n", month);
        return 2;
    }
    
    // check day
    if (day < 1 || day > days_per_month[month -1]) {
        fprintf(stderr, "Invalid day %d! exit.\n", day);
        return 3;
    }
    
    if (month == 2 && leapyear == 0 && day > 28) {
        fprintf(stderr, "Invalid day %d! exit.\n", day);
        return 3;
    }
    
    // all inputs are valid
    …
    Your function isleap looks fine, but i have not tested.
    You don't use all parameters in your function displaydate and give 4 integers to printf, but in the format, there are only 2 formatspecifier for integers.
    Please correct this.

    Your error is the empty default-case. With the checks in front, you don't need a default-case. Delete it.
    Last edited by WoodSTokk; 09-22-2017 at 01:56 AM.
    Other have classes, we are class

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by tammysmith View Post
    I'm trying to write a c program that determines the day number (1 to 366) in a year for a date provided by a user.
    How do I adjust this program for a leap year like (december 31,2012) is day 366 since it was a leap year. Is there a better way(I really want to keep the functions and the switch ? I am also getting an error (expected primary expression before token on line 82)This is my code so far please help


    Code:
      #include <stdio.h>
                       
                       int isleap(int year);
                
                       void  displaydate(int month ,int date ,int year,int daynumber);
    
                       int main()
    
                      {
        
                           int month;
                           int day;
                           int year;
                           int leapyear;
                           int daynumber;
                           
                           
                           
                          printf(" enter a month");
                          scanf("%d",&month);
     
                         printf(" enter a day");
                         scanf("%d",&day);
     
                        printf(" enter a year");
                        scanf("%d",&year);
     
                        leapyear= isleap( year);
                        
                         daynumber = leapyear && month > 2 ? day+1 : day;
                 
                 
                           switch(month)
                           {
                               
                         case 1 : daynumber = day;
                       break;
                     
                      case 2 : daynumber = 31 + day;
                       break;
                      
                      case 3 : daynumber = 59 + day + 1; 
                      break;
                      
                      case 4 : daynumber = 89 + day;
                      break;  
                      
                      case 5 : daynumber = 120 + day;
                      break;  
                     
                      case 6 : daynumber = 150 + day;
                      break; 
                     
                      case 7 : daynumber = 181 + day;
                      break;  
                      
                      case 8 : daynumber = 212 + day;
                      break;   
                     
                      case 9 : daynumber = 232 + day;
                      break;  
                      
                      case 10 : daynumber = 263 + day;
                      break;  
                      
                      case 11 : daynumber = 293 + day;
                      break;  
                     
                      case 12 : daynumber = 324 + day;
                      break;  
                      
                      default:
              
                               
                               
                           }
                
        
               return 0;
          }
          
          
          
                     int isleap(int year)
                     {
                         
                         if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
                        return 1;
                        
                        else  
                        return 0;
                     }
                     
                     
                      void  displaydate(int month ,int date ,int year,int daynumber)
                      {
                          
                          printf("\n your date is %d and the day number is %d",month,date,year,daynumber);
                     
                      }
    first thought, without actually writing code to check it.

    drop it into your switch.

    using this information,
    In each leap year, the month of February has 29 days instead of 28. Adding an extra day to the calendar every four years compensates for the fact that a period of 365 days is shorter than a tropical year by almost 6 hours.
    you only have to worry about what? one month adding one day to it. so, all you got a do is worry about number 2 on your switch.being feb.
    Code:
    
    switch (month)
    {
    case 1:
    ....
    break;
    case 2:
    if ( Leap_or_not_leap( year) == 0 )
    do your math for leap year
    else
    do your math for not leap year, 
    or whatever functions you got set up to handle that.
    break;
    case 3:
    ...
    break;
    ........
    default:
         break;
    } // end switch
    Function:
    int Leap_or_not_leap( int year)
    {
      if  leap year  
       return 0;
     else
       return 1;
    }
    adjust everything accordingly.

    I hope you see what I did there.
    Last edited by userxbw; 09-22-2017 at 09:22 AM.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by userxbw View Post
    first thought, without actually writing code to check it.

    drop it into your switch.

    using this information,

    you only have to worry about what? one month adding one day to it. so, all you got a do is worry about number 2 on your switch.being feb.
    Code:
    
    switch (month)
    {
    case 1:
    ....
    break;
    case 2:
    if ( Leap_or_not_leap( year) == 0 )
    do your math for leap year
    else
    do your math for not leap year, 
    or whatever functions you got set up to handle that.
    break;
    case 3:
    ...
    break;
    ........
    default:
         break;
    } // end switch
    Function:
    int Leap_or_not_leap( int year)
    {
      if  leap year  
       return 0;
     else
       return 1;
    }
    adjust everything accordingly.

    I hope you see what I did there.
    Hmmm, so if you calculate the day of 10th of February (month == 2), you add one day if the year is a leap year.
    So 10th of Feb is normaly day 41, but in a leap year it is day 42?
    And 1st of March? Normaly day 60. And in a leap year? With your code it is also day 60.
    If the year is a leap year, you must add 1 day if the month is greater then 2!

    And your information about leap year is wrong. Not every year that can be devided by 4 is a leap year.
    This is true in Julian system, but not in Gregorian system.
    But you can built in a check for calendar system.
    The important date is Friday, 15th October 1582.
    All dates before should be calculated according Julian system and all dates thereafter in Gregorian system.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread