Thread: Entry Level Help.

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    Entry Level Help.

    I am trying to write a program that asks the user to enter an integer representing an hour (an hour range from 0 - 167) Lets assume Monday at midnight is 0 and Sunday at 11pm is 167.. I need to print out the day of the weak and time am or pm. Here is what i have thats not working.

    Code:
     int main(void)
        10
        11  {
        12
        13  int hour;
        14  #define day (Monday || Tuesday || Wednesday || Thursday || Friday || Saturday || Sunday)
        15
        16  printf("Enter hours: ");
        17  scanf("%d", &hour);
        18
        19  if (hour < 24)
        20  day = Monday;
        21  else if (hour > 23 || hour < 48)
        22  day = Tuesday;
        23  else if (hour > 47 || hour < 72)
        24  day = Wednesday;
        25  else if (hour > 71 || hour < 96)
        26  day = Thursday;
        27  else if (hour > 95 || hour < 120)
        28  day = Friday;
        29  else if (hour > 119 || hour < 144)
        30  day = Saturday;
        31  else ( hour > 143 || hour < 168)
        32  day = Sunday;
        33
        34  printf("Result: %d\n", day);

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    okay, think of this in two parts:

    1) use a number [0-167] to find the day.

    2) once you find the day, you have some remainder of an integer, use that to [0 - 23] to find the hour.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You need to use the logic AND operator && not the logical OR.
    And the initial #define doesn't make any sense unless you want to set up a bit mask, which i guess you don't.
    Perhaps you meant to use an enum instead.
    Code:
    enum day {Monday, Tuesday, Wednesday, Thirsday, Friday, Saturday, Sunday};
    Also, consider the fact that days are multiples of 24, and a simple division will give you the day based on hours.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Quote Originally Posted by xuftugulus View Post
    You need to use the logic AND operator && not the logical OR.
    And the initial #define doesn't make any sense unless you want to set up a bit mask, which i guess you don't.
    Perhaps you meant to use an enum instead.
    Code:
    enum day {Monday, Tuesday, Wednesday, Thirsday, Friday, Saturday, Sunday};
    Also, consider the fact that days are multiples of 24, and a simple division will give you the day based on hours.
    I tried this enum and am still getting an error when I used the word day in my code. Can you give me an example of using divisions to find the day...

  5. #5
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    If we use the proposed enum to represent days, then the words Monday,Tuesday, etc will be automatically assigned by the C compiler to the constant numbers 0,1,..
    The fact is that integer division in C, is done like it would cutoff decimals if decimals were there.
    Eg: 23/5 = 4.6, but if 23 and 4 are of type int, then 23/5 == 4.
    Consider the fact that every day has 24 hours, and a simple C integer division with 24 will give you the day of the week as a number, starting from 0.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    I am looking in my book, I do not see a way to printf an enum.

    I have been working on this for 3 hours now. =(

  7. #7
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    The definition of enum is that the names within it are mapped to numbers, constant numbers for the program.
    Code:
    enum fruits {Apples, Bananas, Oranges, Lemons};
    The type of Apples, Bananas, Oranges, Lemons is int.
    Their values are 0,1,2,3
    It is just the C way of not having to write 0,1,2,3 where you would want to refer to a specific 'fruit'.
    You can use enum fruits as a type for a variable, it evaluates to int.
    So:
    Code:
        enum fruits z;
        int k;
    Is the C way of telling the same thing. enum is there to provide a short and easy way to make tag markers for simple sets with elements that are unique and different and are represented as a number.
    Do you find it clearer now?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    I understand how enum works but what are z and k doing


    Code:
        9  int main(void)
        10
        11  {
        12
        13  int hour, day;
        14  enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} s;
        15
        16  printf("Enter hours: ");
        17  scanf("&#37;d", &hour);
        18
        19  if (hour < 24)
        20  day = Monday;
        21  else if (hour > 23 || hour < 48)
        22  day = Tuesday;
        23  else if (hour > 47 || hour < 72)
        24  day = Wednesday;
        25  else if (hour > 71 || hour < 96)
        26  day = Thursday;
        27  else if (hour > 95 || hour < 120)
        28  day = Friday;
        29  else if (hour > 119 || hour < 144)
        30  day = Saturday;
        31  else
        32  day = Sunday;
        33
        34  printf("Result: %d\n", s);
        35  printf("PROGRAM ENDS\n");
        36  
        37  return 0;
        38  }
    I still cannot get it to print out the day of the week. There are no more errors when I compile it, but it always gives me a huge negative number when I enter a number.

  9. #9
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    1. Try moving the enum type declaration outside main.
    2. If you insist to find the day using if, then read aloud what you are testing for in every case, and look again at my first post where i mention something about using the wrong logical operator.
    3. You find the day in the variable named 'day', but print the variable named 's'.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    I understand my mistake with logical operators. Thank you. Now I get a value of 0,1,2,3,4,5,6when I run the program. It is printing the enums and not the days of the week.

    Code:
         7  #include <stdio.h>
         8
         9
        10  enum day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
        11
        12
        13  int main(void)
        14
        15  {
        16
        17  int hour, day;
        18
        19  printf("Enter hours: ");
        20  scanf("&#37;d", &hour);
        21
        22  if (hour < 24)
        23  day = Monday;
        24  else if (hour > 23 && hour < 48)
        25  day = Tuesday;
        26  else if (hour > 47 && hour < 72)
        27  day = Wednesday;
        28  else if (hour > 71 && hour < 96)
        29  day = Thursday;
        30  else if (hour > 95 && hour < 120)
        31  day = Friday;
        32  else if (hour > 119 && hour < 144)
        33  day = Saturday;
        34  else
        35  day = Sunday;
        36
        37  printf("Result: %d\n", day);
        38  printf("PROGRAM ENDS\n");
    Last edited by alex1067; 03-10-2008 at 10:22 PM.

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Good to see you understand the errors, and more so that you learn from them.
    As i told you the program would print numbers. We use enum, so that we can write programs that are more descriptive of their operation.
    Code:
        ....
        26  else if (hour > 47 && hour < 72)
        27  day = 2;
    Which is more understandable, your version or the one i wrote here?
    If you need to print the days of the week in human readable format you will need to tell your program, to map the numbers to text. We usually use an array of constant string to do that.
    For example.
    I would write:
    Code:
    enum fruit {Apple, Orange, Banana, Lemon};
    char fruitName[4][7] = {"Apple", "Orange", "Banana", "Lemon"};
    
    int main()
    {
        int iLike;
    
        printf("What fruit do you like?\n");
        printf("1. Apple\n");
        printf("2. Orange\n");
        printf("3. Banana!\n");
        printf("4. Lemon?\n");
        scanf("&#37;d", &iLike);
        iLike++;   /* the enum constants start from 0 in this example */
        if(iLike==Apple || iLike==Orange || iLike==Banana || iLike==Lemon)
            printf("So you really like [%s]? Me too!\n", fruitName[iLike]);
        else
            printf("Hmmm... raw meat sound good to you huh?\n");
        return 0;
    }
    Notice the use of the human readable names to verify that the user actually likes a fruit.
    Doesn't it make it much easier to read the purpose of the program?
    Last edited by xuftugulus; 03-10-2008 at 10:42 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Honestly, I understand the program I wrote more clearly. I am 8 weeks into "Intro to computing" course. Thank you for your help. My next task is to try and get it to print the day of week and not the number.

  13. #13
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by alex1067 View Post
    Honestly, I understand the program I wrote more clearly. I am 8 weeks into "Intro to computing" course. Thank you for your help. My next task is to try and get it to print the day of week and not the number.
    The reason you understand yours better is because you aren't willing to try and grasp what he's doing.

    Also, your solution might be quick and dirty and somewhat easy to understand, but what if you had a much larger data range? Eg, not just up to 144 hours? What if the max were 10240?

    You could solve your problem with integer division and an array with the proper days in it. Using that solution, it would be no more than .. 4 lines of code or so, excluding the mandatory main function and such.

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Few errors/fixes in the fruit example
    Code:
    enum fruit {Apple = 0, Orange, Banana, Lemon, Num_Fruits};
    char fruitName[4][7] = {"Apple", "Orange", "Banana", "Lemon"};
    
    int main()
    {
        int iLike;
    
        printf("What fruit do you like?\n");
        printf("1. Apple\n");
        printf("2. Orange\n");
        printf("3. Banana!\n");
        printf("4. Lemon?\n");
        scanf("&#37;d", &iLike);
        iLike--;   /* the enum constants start from 0 in this example */
        if(iLike < Num_Fruits)
            printf("So you really like [%s]? Me too!\n", fruitName[iLike]);
        else
            printf("Hmmm... raw meat sound good to you huh?\n");
        return 0;
    }

  15. #15
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    johnyc@pazuzu:~/Projects/test$ cat enum.c
    Code:
    #include <stdio.h>
    
    enum {A,B,C,D};
    
    int main()
    {
            printf("A: &#37;d, B: %d, C: %d, D: %d\n",A,B,C,D);
            return 0;
    }
    johnyc@pazuzu:~/Projects/test$ gcc -o enum enum.c
    johnyc@pazuzu:~/Projects/test$ gcc -Wall -ansi -pedantic -o enum enum.c
    johnyc@pazuzu:~/Projects/test$ ./enum
    A: 0, B: 1, C: 2, D: 3
    johnyc@pazuzu:~/Projects/test$
    I believe enumerated constants start at 0 by default.

    As for the test, it was purposefully written such that it demonstrates the readability of enum constants versus "magic" numbers.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  2. Diablo's random level generation
    By glo in forum Game Programming
    Replies: 7
    Last Post: 07-19-2008, 03:04 AM
  3. Listing binary tree numbers by level
    By Nazgulled in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 10:36 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. Salary for entry level C position- How much can you make?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-10-2002, 11:54 PM