Thread: C Ranges

  1. #16
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    When you put back in the missing "=" after static const DATE_RANGE date_range[] it will compile, but it doesn't actually work, as all values of "julian" (bar zero) give the answer january!

    Nice try though.

  2. #17
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    /******************************
    * C Program Code::Blocks v1.3 *
    * Written by: EvoTone         *
    * Date: Sept., 2006           *
    *******************************/
    #include <stdio.h>
    
    typedef struct /*This structs DATE_RANGE w/ 2 constants num, char */
    {
        const int lower; /*Defines a number as lower*/
        const char * month; /*Defines month names*/
    } DATE_RANGE; /*Name of the struct*/
    
    static const DATE_RANGE date_range[] =
    {
        { 335, "December"  },
        { 305, "November"  },
        { 274, "October"   },
        { 244, "September" },
        { 213, "August"    },
        { 182, "July"      },
        { 152, "June"      },
        { 121, "May"       },
        {  91, "April"     },
        {  59, "March"     },
        {  31, "February"  },
        {   0, "January"   }
    };
    
    int main(void)
    {
        int i;
        int julian; /*input value from user, needing conversion.*/
    
        /*Intro. & Instructions*/
        printf("This is a Julian to Calendar Date conversion program\n");
        printf("you may input any Julian date and the corresponding\n");
        printf("Calendar Date will be calculated for you\n\n");
    
                                 /*Input Section*/
        printf("Please enter the Julian date: (whole number format)\n");
        scanf("%i", &julian);
        printf("Well silly we know it's Year: 2006!\n\n");
    
    
                                /*Calculation Section*/
    
        if ( (julian < 1) || (julian > 365) )
        {
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
            return(1);
        }
    
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++)
        {
            if (julian > date_range[i].lower)
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        return (0);
    
    }

  3. #18
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Or (more obtusely), and less portable, but shorter and leap-year compliant!

    Code:
    /******************************
    * C Program Code::Blocks v1.4 *
    * Written by: EvoTone         *
    * Date: Sept., 2006           *
    *******************************/
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        int julian; /*input value from user, needing conversion.*/
        time_t t;
        struct tm * ptm;
        char date_str[ 50 ];
    
        /* Introduction & Instructions */
    
        printf("This is a Julian to Calendar Date conversion program\n");
        printf("you may input any Julian date and the corresponding\n");
        printf("Calendar Date will be calculated for you\n\n");
    
        /* Input Section */
    
        printf("Please enter the Julian date: (whole number format)\n");
        scanf("%i", &julian);
    
        /* Calculation Section */
    
        if ( (julian < 1) || (julian > 365) )
        {
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
            return(1);
        }
    
        t = time( (time_t *) NULL );  /* Today's date/time */
    
        ptm = localtime( &t );
    
        if ( ptm != NULL )
        {
            ptm->tm_mday = 1; /* 1st of month */
            ptm->tm_mon  = 0; /* January      */
    
            /* Add "julian - 1" days to start of year */
            t = mktime( ptm ) + ( (julian - 1) * 24 * 60 * 60);
    
            if ( t >= 0 )
            {
                ptm = localtime( &t );
    
                if ( ptm != NULL )
                {
                    /* Convert to string in format "Monday January 01 2006" */
                    if ( strftime(date_str, sizeof(date_str), "%A %B %d %Y", ptm) != 0)
                        printf("And we know it's: %s\n", date_str);
                }
            }
        }
    
        return (0);
    }

  4. #19
    Java and C newbie Xero's Avatar
    Join Date
    Aug 2006
    Location
    Philippines
    Posts
    21
    Looks like the program has been served, with different versions.
    I'm sure the thread starter will be so happy. =P
    Code:
    Noob - a word used to describe someone like me. (noun)

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Thumbs up To all who lent a hand..Thank You!!

    You only need to test for either the upper or the lower bound, and as the loop breaks out afterwards it will not continue.
    You are right I found that I can test for the upper value but, the lower doesn't seem to work due to the fact that "January" is displayed everytime julian is greater than i.

    Thanks guys!! I have learned a lot.... I am still to unexperienced to worry about leap years yet but, I have enough with the code still needing to define the days of the month compared to the days of a calendar year. After all I am trying to convert a "Julian Date" into a 200x/January/2 format. I'm sure as I head into this next phase of the project I will have more questions. For now thanks to all who helped out.

    ~EvoTone~

    __________________________________________________ _____________

    If you wake up every morning and you can think of nothing else but, programming. You are a programmer!

  6. #21
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    As an added bonus, you may also want to make it work for the year 1752 (check the month of September for the number of days that month had)

    Code:
    server:/home$ cal 09 1752
          September 1752      
    Sun Mon Tue Wed Thu Fri Sat  
             1   2  14  15  16
    17  18  19  20  21  22  23
    24  25  26  27  28  29  30
    I think you can put a signature here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with matrix size declaration
    By angelica in forum C++ Programming
    Replies: 13
    Last Post: 04-11-2008, 09:03 AM
  2. Asserts and their ranges
    By Matamoros123 in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2006, 12:32 PM
  3. iterators cursors and ranges in STL
    By superchritch in forum C++ Programming
    Replies: 0
    Last Post: 09-20-2005, 09:03 AM
  4. data type ranges, what is the largest?
    By timberwolf5480 in forum C++ Programming
    Replies: 9
    Last Post: 10-16-2003, 05:47 PM
  5. vector<double> ranges
    By correlcj in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2002, 12:49 PM