Web Design and Web Development Forum

  1. #1
    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
  2. Jeriko Yepez is on a distinguished road
  3. Timer exercise

    Hey man, Once again i am stuck on a problem. This time igot most of it except for minor details. I am sposed to have a program that has a timer, and its point to count however long the user inputs. In between someof the time, its sposed to remind the user to have brekafast and lunch n dinner The part that I am confused is that when i put the time for the clock to run, it either goes crazy and counts forever or it wont do anything? Can you help me out? Thanx This is what i have done!
    Code:
    #define hours 20
    #define min 12
    #define sec 15
    
    int time (int h, int m, int s, int runh, int runm, int runs);
    
    #include <stdio.h>
    
    int main (void) {
    
      int ans = 0, timeh = 0, timem = 0, times = 0;
      int timehr, timemr, timesr, i;
    
      while (ans != 3) {
     
        printf("Choose one of the following menu options:\1. set time\n 2. start timer\n 3. quit\n");
        scanf("%d", &ans);
        if (ans > 3){
           printf("Sorry, that is not a valid menu choice.\n\n");
            printf("Choose one of the following menu options:\n1: set time\n2: start timer\n3: quit\n");
          scanf("%d", &ans);
        }
       
        if (ans == 1) {
          printf("What is the current time?\n");
           scanf("%d : %d : %d", &timeh, &timem, &times);
           
           if (timeh > hours || timem > min || times > sec) {
             printf("Time is invalid please enter again\n");
              scanf("%d : %d : %d", &timeh, &timem, &times);
           }
        }
     
        else if (ans == 2) {
          printf("How long would you like me to count?\n");
          scanf("%d : %d : %d", &timehr, &timemr, &timesr);
            printf("\nHERE WE GO!\n");
            time(timeh, timem, times, timehr, timemr, timesr);
        }
      }
     
      printf("\nThank You!\n");
      return 0;
    }
    
    int time (int h, int m, int s, int runh, int runm, int runs) {
     
      int i;
     
      if (runs > 0){
        for (i=0; i<runs; i++){
           if (s+1 > sec){
              s = 0;
              if (m+1 > min){
                m = 0;
                if (h+1 > hours)
                   h = 0;
                else
                   h = h+1;
                }
               else
                m = m+1;
              }
            else
              s = s+1;
            printf("%d:%d:%d\n", h, m, s);
        }
      }
     
      if (runm > 0) {
        for (i=0; i<(runm*sec)+1; i++) {
           if (s+1 > sec){
              s = 0;
              if (m+1 > min){
                m = 0;
                if (h+1 > hours)
                   h = 0;
                else
                   h = h+1;
                }
               else
                m = m+1;
              }
            else
              s = s+1;
            printf("%d:%d:%d\n", h, m, s);
        }
      }
     
      if (runh > 0) {
        for (i=0; i<(((runh*min)+ 1)*(sec+1)); i++) {
           if (s+1 > sec){
              s = 0;
              if (m+1 > min){
                m = 0;
                if (h+1 > hours)
                   h = 0;
                else
                   h = h+1;
                }
               else
                m = m+1;
              }
            else
              s = s+1;
            printf("%d:%d:%d\n", h, m, s);
        }
      }
      printf("\nDONE COUNTING!\n\n");
    }
    Last edited by hot_cakes; 03-05-2006 at 05:59 PM.
    Reply With Quote Reply With Quote
  4. #2
    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
  5. Jeriko Yepez is on a distinguished road
  6. Re: C Program help!!

    this was part of what we had to do just incase my explenation wasnt clear.

    Else if the user enters 2, then the program will ask how many hours, minutes, and seconds the user would like the timer to run (note that if the ending time exceeds 19:11:14; the timer must continue from 0:0:0). Then the program will start running the timer and print out the current time second by second. The program also prints out messages reminding the meal times.

    The breakfast message should come sometime between 4:0:0 to 5:0:0. The lunch message should come sometime between 7:0:0 to 9:0:0. The dinner message should come sometime between 14:0:0 to 18:0:0. All the messages will be printed at times selected randomly at the intervals given above. To pick a random time in a given interval, determine the number of seconds in that interval. For example, in between 4:0:0 and 5:0:0, there are 12x15 = 180 seconds. Next, pick a random number in between 0 and 180, say 35. Then the random time that corresponds to this number is 35 seconds after the beginning of the interval, which is 4:2:5.
    Reply With Quote Reply With Quote
  7. #3
    hot_cakes's Avatar
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    30
    Posts
    2,913
    Rep Power
    9
  8. hot_cakes will become famous soon enough
  9. Re: Ahh Once again!!

    I have to say that is the most unclear specification for a program I have ever seen. If it's not too much trouble, it would be useful to copy&paste the complete assignment spec here as it seems some context is lacking based on what you've provided.

    As an illustration of my confusion:
    note that if the ending time exceeds 19:11:14; the timer must continue from 0:0:0
    What? Why?

    For example, in between 4:0:0 and 5:0:0, there are 12x15 = 180 seconds
    . Are 4:0:0 and 5:0:0 hours or what? If so surely there are 60x60 = 3600 seconds between these two times...?

    It's basically a big mess as far as I can see.

    Edd
    Visit me at: mr-edd.co.uk
    Languages: Python | Lua
    Compilers: MinGW | MSVC++9
    Libraries: Boost | gtkmm
    Reference: Dinkumware | a.c.l.l.c-c++ FAQ
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
  11. Jeriko Yepez is on a distinguished road
  12. Re: Timer exercise

    lol sorry man ure right ok lemme paste it to you.


    Introduction to C - Programming Assignment #3

    Assigned: 2/22/06 (Wednesday)
    Due: 3/6/06 (Monday) at 3:00am WebCT time

    Objective
    1. How to use if statements.
    2. How to use loops to implement a menu driven program.
    3. How to design a nested loop structure.

    Problem: Interactive Timer
    A program is needed to help manage the day of a hardworking Martian programmer. We all know that programmers forget about the times for their meals once they sit and start programming. The program needs to remind Martian programmer of the breakfast, lunch, and dinner times. Note that the Martian time measure is different than ours. There are 20 hours in a day, 12 minutes in an hour, and 15 seconds in a minute. Therefore the day starts from 0:0:0 and ends at 19:11:14. Please define these three values as constants using the #define directive, and utilize the symbolic constants in your code.

    The program will print a menu of available options and ask the user’s choice. The user may want to set the time (1), start the interactive timer (2), or quit (3).

    If the user enters 1, then the program asks the current Martian time in hours, minutes, and seconds (the default time is 0:0:0 if user never selects this option and starts using the other options directly).

    Else if the user enters 2, then the program will ask how many hours, minutes, and seconds the user would like the timer to run (note that if the ending time exceeds 19:11:14; the timer must continue from 0:0:0). Then the program will start running the timer and print out the current time second by second. The program also prints out messages reminding the meal times.

    The breakfast message should come sometime between 4:0:0 to 5:0:0. The lunch message should come sometime between 7:0:0 to 9:0:0. The dinner message should come sometime between 14:0:0 to 18:0:0. All the messages will be printed at times selected randomly at the intervals given above. To pick a random time in a given interval, determine the number of seconds in that interval. For example, in between 4:0:0 and 5:0:0, there are 12x15 = 180 seconds. Next, pick a random number in between 0 and 180, say 35. Then the random time that corresponds to this number is 35 seconds after the beginning of the interval, which is 4:2:5.

    Since the program gives the messages at randomly specified times, if the user specified a short interval that intersected with a meal time, the program does not have to produce the message in that interval necessarily. For example, if the user sets the current time to 4:0:0 and runs the timer for the duration of 0:1:0, the program does not have to remind the user about breakfast within this one minute because it might be the case that the program has decided to print the breakfast message at 4:10:12.

    When the message is printed, the user will be asked whether he already had his meal or not. If he has had his meal, the countdown just continues. If he hasn’t, then a message should be printed to the screen telling him how much time he has left to eat the designated meal and then countdown continues.

    If the user enters 3 for the menu option, the program will end. If the user enters an integer other than 1, 2, or 3 when prompted by the menu, an appropriate message should be printed and the user should be re-prompted for their menu choice. The menu will be available until the user asks to quit using option 3.

    Input Specification
    1. The choice of the user for the menu will be an integer.
    2. All the times that are entered will be h:m:s format. In order to read these type of values from the user, use scanf(“%d:%d:%d”,&h,&m,&s).
    3. The user will specify that he already had his meal by entering 1, and a 0 would mean that he did not have his meal yet. The user will not enter any other character.
    4. Any time interval requested by the user will never contain more than one possible meal.


    Output Sample
    Here is one sample output of running the program. Note that this test is NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.


    Sample Run
    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    1

    What is the current time?
    3:11:13

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    2

    How long would you like me to count?

    0:1:0

    HERE WE GO!
    3:11:13
    3:11:14
    4:0:0
    4:0:1
    4:0:2
    4:0:3

    Have you had your breakfast?
    1: Yes
    0: No

    1

    Good!

    4:0:4
    4:0:5
    4:0:6
    4:0:7
    4:0:8
    4:0:9
    4:0:10
    4:0:11
    4:0:12

    DONE COUNTING!

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    1

    What is the current time?
    7:10:0

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    2

    How long would you like me to count?

    0:1:5

    HERE WE GO!
    7:10:0
    7:10:1
    7:10:2
    7:10:3
    7:10:4
    7:10:5
    7:10:6
    7:10:7
    7:10:8
    7:10:9
    7:10:10
    7:10:11
    7:10:12
    7:10:13
    7:10:14
    7:11:0
    7:11:1
    7:11:2

    Have you had your lunch?
    1: Yes
    0: No

    0

    You have 1:0:13 till the end of lunch time.

    7:11:3
    7:11:4

    DONE COUNTING!

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    1

    What is the current time?
    19:11:10

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    2

    How long would you like me to count?

    0:0:8

    HERE WE GO!
    19:11:10
    19:11:11
    19:11:12
    19:11:13
    19:11:14
    0:0:0
    0:0:1
    0:0:2

    DONE COUNTING!

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    4

    Sorry, that is not a valid menu choice.

    Choose one of the following menu options:
    1: set time
    2: start timer
    3: quit

    3

    Thank you.


    Deliverables
    You must submit the following .c source file with the following name:

    1) timer.c, for your solution to problem


    Please submit this file over WebCT.

    Restrictions
    Although you may use other compilers, your program must compile and run using cygwin or gcc. Please use either your olympus account or jGRASP to develop your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. Also, make sure you include ample comments throughout your code describing the major steps in solving the problem.

    Grading Details
    Your program will be graded upon the following criteria:

    1) Your correctness
    2) Your programming style and use of white space. (Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor you could get 10% or 15% deducted from your grade.)
    3) Compatibility to either cygwin in Windows or gcc under olympus. (If your program doesn’t compile in either of these environments, you will get a grade sizable deduction.)
    Reply With Quote Reply With Quote
  13. #5
    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
  14. Jeriko Yepez is on a distinguished road
  15. Re: Timer exercise

    Right now im working on putting those printf in between the timings for breakfast lunch n dinner. Another thing messing me up here is how do I tell the program to continue with its count after the user inputers a yes or no.
    Reply With Quote Reply With Quote
  16. #6
    hot_cakes's Avatar
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    30
    Posts
    2,913
    Rep Power
    9
  17. hot_cakes will become famous soon enough
  18. Re: Timer exercise

    Haha! That makes more sense now!

    Ok, first things first. I wanna see some pseudo code to illustrate your program logic. I've had a nightmare trying to get some in another thread recently -- I hope you can produce the goods! I'm asking for this because I need to know you are thinking about the problem. If I start shooting off about how I would do it, we'd get out of sync and I'd end up doing more damage than good.

    It would also be useful if you could look up the rand() and srand() functions. If you don't understand anything about them, then ask. But you're going to need these.

    Edd
    Last edited by hot_cakes; 03-06-2006 at 01:18 PM.
    Visit me at: mr-edd.co.uk
    Languages: Python | Lua
    Compilers: MinGW | MSVC++9
    Libraries: Boost | gtkmm
    Reference: Dinkumware | a.c.l.l.c-c++ FAQ
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
  20. Jeriko Yepez is on a distinguished road
  21. Re: Timer exercise

    Ok i got it! haha after looking through the main part of my program I realized what I needed. The only thing [I] didnt get a chance to do was put the breakfast, lunch n dinner in between. This is what I did tho. I'd still be nice if you could help me out knowing how i would put the messages in the middle of the timer. Thanx!!


    Code:
    #define hours 20
    #define min 12
    #define sec 15
    
    int time (int h, int m, int s, int runh, int runm, int runs);
    
    #include <stdio.h>
    
    int main (void) {
    
      int ans = 0, timeh = 0, timem = 0, times = 0;
      int timehr, timemr, timesr, i;
    
      while (ans != 3) {
     
        printf("Choose one of the following menu options:\n 1. set time\n 2. start timer\n 3. quit\n");
        scanf("%d", &ans);
        if (ans > 3){
           printf("Sorry, that is not a valid menu choice.\n\n");
            printf("Choose one of the menu options:\n 1. set time\n 2. start timer\n 3. quit\n");
          scanf("%d", &ans);
        }
       
        if (ans == 1) {
          printf("What is the current time?\n");
           scanf("%d : %d : %d", &timeh, &timem, &times);
           
           if (timeh > hours || timem > min || times > sec) {
             printf("Time is invalid please enter again\n");
              scanf("%d : %d : %d", &timeh, &timem, &times);
           }
        }
     
        else if (ans == 2) {
          printf("How long would you like me to count?\n");
          scanf("%d : %d : %d", &timehr, &timemr, &timesr);
            printf("\nHERE WE GO!\n");
            time(timeh, timem, times, timehr, timemr, timesr);
        }
      }
     
      printf("\nThank You!\n");
      return 0;
    }
    
    int time (int h, int m, int s, int runh, int runm, int runs) {
     
      int i;
     
      if (runs > 0){
     	for (i=0; i < runs+1; i++){
    	 if (s+1 > sec){
    		 s = 0;
              if (m+1 >= min|| s+1>sec || h+1>=hours){
    			    m = 0;
              if (h+1 >= hours || m+1>=min || s+1 > sec)
    				h = 0;
                else
                   h = h+1;
                }
               else
                m = m+1;
              }
            else
              s = s+1;
            printf("%d:%d:%d\n", h, m, s);
        }
      }
     
      if (runm > 0) {
       for (i=0; i < runm+1; i++) {
            if (s+1 > sec){
              s = 0;
              if (m+1 >= min|| s+1>sec || h+1>=hours){           
    			  m = 0;
                if (h+1 >= hours || m+1 >= min || s+1 > sec)
                   h = 0;
                else
                   h = h+1;
                }
               else
                m = m+1;
              }
            else
              s = s+1;
            printf("%d:%d:%d\n", h, m, s);
        }
      }
     
      if (runh > 0) {
         for (i=0; i < runh+1; i++) {
    		 if (s+1 > sec){
              s = 0;
             if (m+1 >= min|| s+1>sec || h+1>=hours){
                m = 0;
                if (h+1 >= hours || m+1>=min || s+1 > sec)
                   h = 0;
                else
                   h = h+1;
                }
               else
                m = m+1;
              }
            else
              s = s+1;
            printf("%d:%d:%d\n", h, m, s);
        }
      }
      printf("\nDONE COUNTING!\n\n");
    }


    What ya think?
    Last edited by hot_cakes; 03-06-2006 at 03:31 PM.
    Reply With Quote Reply With Quote
  22. #8
    hot_cakes's Avatar
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    30
    Posts
    2,913
    Rep Power
    9
  23. hot_cakes will become famous soon enough
  24. Re: Timer exercise

    I'd change the name of your "time" function. There's already a function in the standard library called "time". Luckily in this case the names didn't conflict because you didn't #include <time.h>. But it's something to keep in mind.

    I think the loop in main() would be niceer as a do-while loop rather than a while loop:

    Code:
    int ans = 0;
    
    do
    {
        /* ask question + print menu options */
        /* read menu option value */
        /* If not valid print an error, else do the processing */
    }
    while(ans != 3); /* the question and menu will get shown again automatically if needed */
    Would you agree that this way is a bit nicer?

    Generally speaking, more comments certainly wouldn't go a miss. Not just for my benefit, but also for the sucker marking it who has to work out what everything means! Make it easy for them to give you marks!

    #define-d constants and macros are conventionally written in uppercase letters. It's not strictly necessary but it's a convention that's a good idea to stick to so as to avoid name clashes.

    Do you know about pointers yet? If not ignore this next bit. But if you do, then I'd be tempted to make a function:
    Code:
    void next_time(int *hour, int *min, int *sec);
    This would increment the time represented by *hour, *min and *sec by one second. Breaking down the code in to smaller, more manageable bits like this makes things easier to read. Or even better, make a function
    Code:
    void add_seconds(int *hour, int *min, int *sec, int to_add);
    which would increment the represented time by to_add seconds.

    Do you know what the % operator does yet? i.e. what is the value of 5 % 3? If you don't know, don't worry. But if you do, then there are a few places in your code where it would come in handy.

    Right, now for your specific question. You first need to generate 3 random times each inside it's own interval. Did you look up the rand() and srand() functions yet? Have a look at those first. If there's anthing you don't understand about them, then feel free to ask. But we (you, really!) need to look at them before we can generate these 3 random times. The add_seconds() function I mentioned would come in handy too soon if you know how to make it. If not, don't worry -- just look at rand() and srand() and get back to me :)

    Edd
    Visit me at: mr-edd.co.uk
    Languages: Python | Lua
    Compilers: MinGW | MSVC++9
    Libraries: Boost | gtkmm
    Reference: Dinkumware | a.c.l.l.c-c++ FAQ
    Reply With Quote Reply With Quote
  25. #9
    hot_cakes's Avatar
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    30
    Posts
    2,913
    Rep Power
    9
  26. hot_cakes will become famous soon enough
  27. Re: Timer exercise

    Also, for the sake of others reading your post, you can put code-tags around your code snippets to format them in a more readable way. To do this you'd do [CODE ]code goes here[/CODE ] but without spaces anywhere inside the square brackets. I took the liberty of adding code-tags to your posts, but it would be great if you could do this in future.

    Edd
    Visit me at: mr-edd.co.uk
    Languages: Python | Lua
    Compilers: MinGW | MSVC++9
    Libraries: Boost | gtkmm
    Reference: Dinkumware | a.c.l.l.c-c++ FAQ
    Reply With Quote Reply With Quote
  28. #10
    Join Date
    Feb 2006
    Posts
    12
    Rep Power
    0
  29. Jeriko Yepez is on a distinguished road
  30. Re: Timer exercise

    oh man thanx for the reply that was awesome! I completely forgot about the comments! I was so exited that I got the program to work that I didnt even pay attention to the comment blah! ANd yea we have learned about the rand and srand I should have put that into mind when I was working on it! And with your explenation I could have broken the program down into like 15 lines lo! Arg B ut that was an awesome reply man for next time I have alot of stuff I can think about and include! I'll probably get another maybe tomororw or sometime this week, so if problems pop up (no doubt bout it lol) I will post again! Thanks again for your help man appreciate it tons!
    Reply With Quote Reply With Quote

Similar Threads

  1. Visualbasic - Timer Interval Help
    By cancer10 in forum Visual Basic Programming
    Replies: 3
    Last Post: 07-16-2005, 02:14 AM