C Ranges

This is a discussion on C Ranges within the C Programming forums, part of the General Programming Boards category; Hope some one can help: I am new to programming and need to learn how to set up a range. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Unhappy C Ranges

    Hope some one can help: I am new to programming and need to learn how to set up a range. Hope I am wording this right but, I am working on a project that requests user input(an integer) now after the input I would like to search a range of integers and if the user input is in that "range" conduct a "printf". If the integer is not in the range then conduct another "printf"...etc.

    Example:

    char X[]="Nebraska";
    char XX[]="Texas";
    char XXX[]="California";
    char XXXX[]="Arkansas";
    int ask;

    range=(1-100) //This is that range I need//

    int main()

    {
    scanf( "%i", &ask);

    if ("%i", ask) = (1-25) //This is like the comparison I need to do//
    printf("Nebraska\n");

    if ("%i", ask) = (26-50) //" "//
    printf("Texas\n");

    if ("%i", ask) = (51-75) //" "//
    printf("California\n");

    if("%i", ask) = (76-100) //" "//
    printf("Arkansas\n");

    if ("%i", ask) > 100
    printf("This is out of range\n");

    return 0;
    }

    Sorry in advance if I didn't explain my problem correctly but, this should give
    you an idea of what I am trying to accomplish. Plus I am 2 weeks into
    C programming (Well trying to learn all I can off the internet at least).

    ~EvoTone~

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Like this -

    Code:
    scanf( "%d", &ask);
    
    if ( ask>0 && ask <=25 )
    {
      whatever
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Thanks for the help but, I tryed it like you wrote it and I get

    *syntax error of the && inputs

    Any other help would be appreciated.

    ~EvoTone~

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by EvoTone
    Thanks for the help but, I tryed it like you wrote it and I get

    *syntax error of the && inputs

    Any other help would be appreciated.

    ~EvoTone~
    It's always helpful if you paste your *exact* relevant code, to show us. If you typed it correctly, then try making the following change:

    Code:
    scanf( "%d", &ask);
    
    if ( (ask>0) && (ask <=25) )
    {
      whatever
    }
    The C language has some quirks in it's order of operations. It's always a good idea to wrap any quantity (term), that must be evaluated, in parenthesis, to ensure that the order of operations on that quantity or term, is evaluated in the order you expect.

    Try the above, and let us know, OK?

    Adak

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak
    The C language has some quirks in it's order of operations.
    It may, but there not for that example.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Corrected with code tag

    Thanks for the tip on wrapping evaluations but, what I don't want is for you guys to write my
    program for me. Cause then I have no idea what the code does in a step-by-step method. And I am none the wiser for it~~

    Here is what I am learning on:
    Code:
    /* * * * * * * * * * * * * * ** *
    *C Program Code::Blocks v1.0*
    *Written by: EvoTone *
    *Date: Sept., 2006 *
    * * * * * * * * * * * * * * * * */
    #include <stdio.h>
    int main(void)
    
    {
    
    int JULIAN; /*input value from user, needing converted.*/
    
    
    
    /*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");
    
    
    /*Calcualtion Section*/
    if((JULIAN >0) && (<=31)) printf("And we also know it's: January\n");
    
    if(JULIAN >31 && <=59 ) printf("And we also know it's: Febuary\n");[/color]
    
    if(JULIAN>59 && <=91) printf("And we also know it's: March\n");
    
    if(JULIAN>91 && <=121) printf("And we also know it's: April\n");
    
    if(JULIAN>121 && <=152) printf("And we also know it's: May\n");
    
    if(JULIAN>152 && <=182) printf("And we also know it's: June\n");
    
    if(JULIAN>182 && <=213) printf("And we also know it's: July\n");
    
    if(JULIAN>213 && <=244) printf("And we also know it's: August\n");
    
    if(JULIAN>244 && <=274) printf("And we also know it's: September\n");
    
    if(JULIAN>274 && <=305) printf("And we also know it's: October\n");
    
    if(JULIAN>305 && <=335) printf("And we also know it's: November\n");
    
    if(JULIAN>335 && <=365) printf("And we also know it's: December\n");
    else
    if(JULIAN>365) printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
    return (0);
    
    }

    So here is my exact source code copied and pasted from my IDE, code::blocks v.1.0(GNU GCC
    Compiler). If there is another way to make this operation work please take the time to explain it, if it's way out in left field that is. And please don't comment on how to complete any other part of this program except this part........~please~

    ~EvoTone~

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if(JULIAN>335 && <=365)
    Read that outloud. If Julian is greater than 335 AND ______ is less than or equal to 365 ...

    You also don't even have to have half of those checks in.
    Code:
    if( x < 0 )
        error, do something...
    else
    if( x < 10 )
        between 0 and 9
    else
    if( x < 20 )
        between 10 and 19
    ...
    But it's up to you.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, I just didn't know if I could describe it well enough, otherwise.

    Do take note of quzah's tip on the if statements. You're leaving out the second "JULIAN", inside the parenthesis.

    if ((hour > 2) && (hour < 4)) printf("\n Time to get that report out to division");

    hour needs to be included before the && as well as after it. Each part of the compound expression is evaluated separately, and then later, evaluated as a whole. That's what the extra parenthesis ensure.

    Quote Originally Posted by EvoTone
    Thanks for the tip on wrapping evaluations but, what I don't want is for you guys to write my
    program for me. Cause then I have no idea what the code does in a step-by-step method. And I am none the wiser for it~~

    Here is what I am learning on:
    ALWAYS PUT YOUR CODE BETWEEN CODE TAGS!

    Code:
    /* * * * * * * * * * * * * * ** *
    *C Program Code::Blocks v1.0*
    *Written by: EvoTone              *
    *Date: Sept., 2006                  *
    * * * * * * * * * * * * * * * * */
    #include <stdio.h>
    int main(void)
    
    {
    
        int JULIAN; /*input value from user, needing converted.*/
    
        /*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");
    
    
                                /*Calcualtion Section*/
    if((JULIAN >0) && (<=31))  printf("And we also know it's: January\n");
    
        if(JULIAN >31 && <=59 )  printf("And we also know it's: Febuary\n");[/COLOR]
    
            if(JULIAN>59 && <=91)  printf("And we also know it's: March\n");
    
                if(JULIAN>91 && <=121)  printf("And we also know it's: April\n");
    
                    if(JULIAN>121 && <=152)  printf("And we also know it's: May\n");
    
                       if(JULIAN>152 && <=182)  printf("And we also know it's: June\n");
    
                            if(JULIAN>182 && <=213)  printf("And we also know it's: July\n");
    
                                if(JULIAN>213 && <=244)   printf("And we also know it's: August\n");
    
                                   if(JULIAN>244 && <=274)  printf("And we also know it's: September\n");
    
                                       if(JULIAN>274 && <=305)  printf("And we also know it's: October\n");
    
                                         if(JULIAN>305 && <=335)  printf("And we also know it's: November\n");
    
                                           if(JULIAN>335 && <=365)  printf("And we also know it's: December\n");
                                           else
                     if(JULIAN>365)  printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    So here is my exact source code copied and pasted from my IDE, code::blocks v.1.0(GNU GCC
    Compiler). If there is another way to make this operation work please take the time to explain it, if it's way out in left field that is. And please don't comment on how to complete any other part of this program except this part........~please~

    ~EvoTone~
    Any time you see a good deal of something in your code that's repeated several times, especially if it's repeated in the same function, you can be sure the design of the program should be improved.

    Look how many times the phrase "And we can be sure that..." is included in your code. Try and make it so it's only included once!

    Instead of using 12 if statements in a row, why not make it one switch statement? That's exactly the kind of thing that the switch statement was created for. The indentation you have for the if statements reflects a chain of interlinked if statements, like a cascade. In fact, they all are independent statements, and should all go to the left side, even with the first if statement.
    (but all should be replaced by one switch statement)

    In your prompt for "whole number format", it'd be helpful if you showed an example:
    of just what a "whole number format" date, really is. (eg: give your example here). Since you're getting critical input from your user, you need to think about making sure that the user understands, and that you are actually checking, their input. (you'll do a lot more input checking on programs, later on)

    Be considerate of your user - "silly" and "stupid" reflect an inconsiderate and juvenile light on your program. It's rarely good to put a little humor into a program, just because it gets old, and what's funny to one person, isn't to others. You'd be surprised how angry user's get when they're called names by a computer program!

    Adak
    Last edited by Adak; 09-24-2006 at 12:07 AM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > If there is another way to make this operation work please take the time to explain it,
    Do you know about structures yet?

    > Instead of using 12 if statements in a row, why not make it one switch statement?
    Because standard C doesn't have ranges on cases.
    365 lines of switch/case is hardly clear code.

    Oh, and the cascasded indent on your if - not pretty.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Or you could try ...

    Code:
    /****************************
    *C Program Code::Blocks v1.0*
    *Written by: EvoTone        *
    *Date: Sept., 2006          *
    *****************************/
    #include <stdio.h>
    
    typedef struct
    {
        const int lower;
        const int upper;
        const char * month;
    } DATE_RANGE;
    
    static const DATE_RANGE date_range[]
    {
        {   0,  31, "January"   },
        {  31,  59, "February"  },
        {  59,  91, "March"     },
        {  91, 121, "April"     },
        { 121, 152, "May"       },
        { 152, 182, "June"      },
        { 182, 213, "July"      },
        { 213, 244, "August"    },
        { 244, 274, "September" },
        { 274, 305, "October"   },
        { 305, 335, "November"  },
        { 335, 365, "December"  }
    };
    
    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*/
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++)
        {
            if ((julian > date_range[i].lower) && (julian <= date_range[i].upper))
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        if( i == ( sizeof(date_range) / sizeof(date_range[0]) ) )
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    Just a thought ...

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Have you been programming in Java?
    Code:
    static const DATE_RANGE date_range[]
    {
    ->
    Code:
    static const DATE_RANGE date_range[] =
    {
    That's pretty much how I'd do it, too, except as Quzah said, if you order the dates correctly, you only have to store their upper bounds.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    SKeane

    Code:
    /* ***************************
    *C Program Code::Blocks v1.0         *
    *Written by: EvoTone                       *
    *Date: Sept., 2006                           *
    **************************** */
    #include <stdio.h>
    
    typedef struct /*This structs DATE_RANGE w/ 3 constants num/num/char*/
    {
        const int lower; /*Defines a number as lower*/
        const int upper; /*Defines a number as "upper"*/
        const char * month; /*Defines month names*/
    } DATE_RANGE; /*Name of the struct*/
    
    static const DATE_RANGE date_range[]/*Defines struct as array,date_range[]*/
    {
        {   0,  31, "January"   },   /*Defines lower/upper/month variables in struct*/
        {  31,  59, "February"  }, /*"           "*/
        {  59,  91, "March"     },   /*"           "*/ 
        {  91, 121, "April"     },     /*"           "*/ 
        { 121, 152, "May"       },   /*"           "*/ 
        { 152, 182, "June"      },    /*"           "*/ 
        { 182, 213, "July"      },      /*"           "*/ 
        { 213, 244, "August"    },    /*"           "*/ 
        { 244, 274, "September" },  /*"           "*/ 
        { 274, 305, "October"   },     /*"           "*/ 
        { 305, 335, "November"  },   /*"           "*/ 
        { 335, 365, "December"  }     /*"           "*/ 
    };
    
    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*/
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++) /*Loop*/
        {
            if ((julian > date_range[i].lower) && (julian <= date_range[i].upper))
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        if( i == ( sizeof(date_range) / sizeof(date_range[0]) ) )
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    I think I'm getting what this is supposed to do but, there is syntax errors at line 15 and 48 that I can't figure out. I hadn't previously fooled with "struct" or setting an "array" in this manner so, even after reading up on it, I can't fix it. Little help please. I have added notes to places, telling the thought process I am taking in my attempt to explaining this.
    ~EvoTone~
    Last edited by Ken Fitlike; 09-26-2006 at 02:38 PM. Reason: fixed [code][/code] tags

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Talking Thanks!!

    Thanks dwks
    static const DATE_RANGE date_range[] =
    {
    That fixed the two errors.

  14. #14
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    For some reason in the cut'n'paste operation the "=" went missing. I did actually compile, run and test the code (honest).

    Of course you aren't dealing with leap years, so your program will only work approximately 3/4 of the time. This year's OK, next year's OK, then in 2008 it will break.
    Last edited by SKeane; 09-27-2006 at 04:13 AM.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    123
    Completely untested, but even that complete code has redundancy in it. Take a look at this:

    Code:
    /* ***************************
    *C Program Code::Blocks v1.0         *
    *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[]/*Defines struct as array,date_range[]*/
    {
        {   0,  "January"   },   /*Defines lower/upper/month variables in struct*/
        {  31,  "February"  }, /*"           "*/
        {  59,  "March"     },   /*"           "*/ 
        {  91, "April"     },     /*"           "*/ 
        { 121, "May"       },   /*"           "*/ 
        { 152, "June"      },    /*"           "*/ 
        { 182, "July"      },      /*"           "*/ 
        { 213, "August"    },    /*"           "*/ 
        { 244, "September" },  /*"           "*/ 
        { 274, "October"   },     /*"           "*/ 
        { 305, "November"  },   /*"           "*/ 
        { 335, "December"  }     /*"           "*/ 
    };
    
    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*/
        for (i = 0; i < (sizeof(date_range) / sizeof(date_range[0])); i++) /*Loop*/
        {
            if (julian > date_range[i].lower)
            {
                printf("And we know it's: %s\n", date_range[i].month);
                break;
            }
        }
    
        if (i  > 365 )
            printf("Don't mess with me stupid!!...Julian Dates are only 1-365!\n");
    
        return (0);
    
    }
    You only need to test for either the upper or the lower bound, and as the loop breaks out afterwards it will not continue.

    As has been pointed out, modifications to the hardcoded values are required to cater for leap years (bearing in mind that altough 2000 was fortunately a leap year, 2100 will not be).
    I think you can put a signature here.

Page 1 of 2 12 LastLast
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, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21