Thread: Hi, check out my elapsed_time prog. Help with implementation!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Hi, check out my elapsed_time prog. Help with implementation!

    I need to know what I am missing so far and what errors I've made.
    First of all this is a program that computes the elapsed time between the two times you see within the code.

    Help me out a little, thanks.

    Code:
    #include <stdio.h>
    
    
    
    
    //Time display structure
    struct time
    {
        int hour;
        int minutes;
        int seconds;
        struct time1= hh:mm:ss
        struct time2= hh:mm:ss
    }
    
    
    
    
    
    
    
    
    ;
    
    
    //main function
    int main ()
    {
          struct time time1=3 45 15;
          struct time time2=9 44 03;
           elapsed_time(time);
    
    
        struct time;
    
    
    
    
        printf("Time elapsed from %i to %i is %i", time1, time2, );
        scanf("%i:%i:%i", time);
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    //elapsed time
    elapsed_time (time1, time2)
    {
        //time1
        ++time.seconds;
        if(time.seconds==60){
        time.seconds =0;
        ++time.minutes;
    
    
            if(time.minutes==60){
            time.minutes=0;
           ++ time.hour;
    
    
           if(time.hour==24)
           time.hour=0;
    
    
    
    
             }
    }
    
    
    return time;

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    What was wrong with the old thread you had: What else do I need in this simple time program...? Generally you only start a new thread for a new topic, not because you didn't like the help you got in the last one or whatever.

    You are nowhere near ready to actually start coding this project. For starters, you lack some serious C fundamentals. Check out our tutorials, and Google for some more. Crack open your textbooks and start reading and working through the examples. Also, you need to understand how to solve the problem before you start coding. That means you need to work it out on paper first, then translate that to code. Also, your code formatting is crap. Get rid of the excessive white space/blank lines and learn to indent properly so we, and you, can read it more easily. Read Indent cpWiki, then I suggest one of the top 3 styles listed here: Indent style - Wikipedia, the free encyclopedia.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    Obviously because people here never answer my question.

    Then they have the arrogance to reply with something like "oh you don't know how to program, you need to learn from a textbook young man".

    Perhaps you should just answer the question so I won't re-thread. Otherwise, you have no obligation to answer. And why is it that I have to be extremely specific with people in this forum? They just waste my time blabbing about how they can't read the code or some BS.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    New code, help me define time within the elapsed_time function

    Code:
    #include <stdio.h>
    
    
    
    
    //Time display structure
    struct time
    {
        int hour;
        int minutes;
        int seconds;
    };
    
    
    //main function
    int main ()
    {
          struct time time1={3,45,15};
          struct time time2={9,44,03};
          struct time elapsed_time();
    
    
        printf("Time elapsed from %i to %i is %i", time1, time2, elapsed_time );
    
    
    }
    
    
    //elapsed time
    struct time elapsed_time(time1, time2)
    {
    
    
        //time1
        ++time.seconds;
        if(time.seconds==60){
        time.seconds =0;
        ++time.minutes;
    
    
            if(time.minutes==60){
            time.minutes=0;
           ++ time.hour;
    
    
           if(time.hour==24)
           time.hour=0;
            }
    
    
             }
      return time;
    
    
    }

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    first off you need to change your parameters to take in a struct, right now you have them as time1 and time2, it should be (struct time time1, struct time time2)

    also in your function time elapse
    right under your comment for time1
    you need to refer to time1 as -----> time1.seconds++;
    what is this sample code suppose to output?
    Last edited by camel-man; 10-12-2011 at 01:06 PM.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Dom View Post
    Then they have the arrogance to reply with something like "oh you don't know how to program, you need to learn from a textbook young man".

    Perhaps you should just answer the question so I won't re-thread. Otherwise, you have no obligation to answer. And why is it that I have to be extremely specific with people in this forum? They just waste my time blabbing about how they can't read the code or some BS.
    It is obvious from your code that you don't have the basics of C down just yet. That's why people are telling you to start learning from a textbook; you have to crawl before you can walk. We can't just give you a few hints which will help you complete this program, it requires a lot more than that. Since people here are spending their (valuable) free time helping out, most of us wants to help with specific problems, not rewriting incomplete programs.

    I'll give some suggestions on things you have to fix:
    Your struct intialization is wrong, you can do it like this: (edit: it is actually not wrong, I was mixing it up with partially initializing structs)
    Code:
    struct time time1 {
        .hour = 1,
        .minutes = 1.
        .seconds = 2
    };
    You are returning a non-existing time struct from the elapsed_time function.

    Code:
    struct time elapsed_time();
    Not sure what you are trying to do here, having this inside main().
    Last edited by iceaway; 10-12-2011 at 01:32 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It's not arrogance. We're not putting you down to feel good about ourselves or because we're too proud to help you with a simple problem. You came to us for help, and we gave you some things you need to work on -- you threw a tantrum. We don't generally hand out code here, and we aren't here to teach you the fundamentals of C, we're here to point out things you need to work on and give you a nudge in the right direction. It is your job to learn to program and our job to help you over the bumps. The code you posted may or may not reflect your actual programming skills, but it's all we have to go on. It tells us you're woefully under prepared for solving a problem like this, and that if we give you a solution, you will remain confused and learn nothing. Many other people here receive exactly the same kind of advice you did, accept it gratefully, learn from it and create a working program through largely their own efforts. I don't really know what kind of answer you're looking for, but your current attitude is not going to help you get it.

    You post shows us you are missing two things necessary for coding a solution to your problem:

    1. C fundamentals. You don't have this. Your code is rife with syntax errors. The more critical parts are not even close to the correct syntax. I wouldn't dream of teaching a kid algebra if they couldn't do basic arithmetic, nor would I consider helping you code a solution to the problem of elapsed time with you when you don't have a good grasp of functions or structs. You need to learn how to define and call functions, and how to define, initialize and manipulate structs before you start coding a solution to this problem.

    2. Understanding of the problem and solution. Any experienced programmer will tell you that you need to understand the problem and be able to solve it generally before you start coding a solution. You can't code the solution to a problem you don't understand. I think you understand the problem, but maybe don't know how to solve it.

    Now, onto business:

    You're looking for the elapsed time, which laserlight told you is the difference between two times. Take the sample numbers in your program. You want the difference between 3:45:15 and 9:44:03. Here are some questions to consider:
    1. Can you solve this by hand?
    2. What steps did you take to do so?
    3. Your code makes use of the increment (++) operator. Did you increment when working by hand?
    4. Did you use any operations besides addition/increment?
    5. Did you need to repeat any steps?

    Now, if you can solve this by hand, you need to tell us, in clear English, what steps you took so we can help you work out a solution. I guarantee, if you can describe the problem clearly, the code will largely fall into place. It wont write itself in 3 lines, but it will become much clearer. Remember, the computer works in very small steps, so you need to describe the process in considerable detail.

    Us telling you your code is unreadable is not BS. Readability matters. Readable code makes it easier for you and for us, to see what's going on and to keep track of what statements belong to which if or else, which loop, etc. That's a good thing when you're having problems. It also shows that you're putting in some effort, not just throwing some stuff at a wall to see what sticks, or hoping somebody will take pity on you and hand you a ready-made solution. And seriously, read some tutorials on structs and functions. I'm not being a a-hole, I'm telling you you need to brush up on that stuff to solve this problem.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    42
    Quote Originally Posted by camel-man View Post
    first off you need to change your parameters to take in a struct, right now you have them as time1 and time2, it should be (struct time time1, struct time time2)

    also in your function time elapse
    right under your comment for time1
    you need to refer to time1 as -----> time1.seconds++;
    what is this sample code suppose to output?

    it outputs time so that it won't say 70 seconds...it will be 1 minutes and 10 seconds...thats what the increments are for.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Okay, looks like you figured out how to define a struct, and you properly define and initialize time1 and time2. That's a good start. You'll need to define a variable to store the result of the elapsed_time function, and you'll need to actually call that function, passing it time1 and time2.

    In main:
    Code:
    struct time elapsed_time();
    That is a function prototype. You should never put those inside other functions. They either go in a header file, or near the top of the file, between your #includes/#defines and the actual functions. Also, that does not call the function, so you don't actually calculate anything.
    Code:
    printf("Time elapsed from %i to %i is %i", time1, time2, elapsed_time );
    printf only knows basic types, so you can't use a whole struct. %i is for integers, so you have to give it an int, like time1.hours. Your use of 'elapsed_time' in that call is incorrect in a couple ways. You don't have a format specifier in the string for it, so printf may crash your program. Also, using a function name without parentheses results in a function pointer (the address of that function). It does not call the function or put the result in there to be magically printed by printf.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    New code

    Just point out all the parts that are wrong so I may fix them. I do not like reading the errors on codeblocks since they don't explain why it is wrong.

    help guys.



    Code:
    #include <stdio.h>
    
    
    
    
    //Time display structure
    struct time
    {
        int hour;
        int minutes;
        int seconds;
    };
    
    
    //elapsed time
    struct time elapsed_time(struct time time1, struct time time2)
    {
        //time1
            ++time1.seconds;
            if(time1.seconds==60){
            time1.seconds=0;
                ++time1.minutes;
    
    
                if(time1.minutes==60){
                time1.minutes=0;
                ++time1.hour;
    
    
                    if(time1.hour==24)
                    time1.hour=0;
            }
          ++time2.seconds;
            if(time2.seconds==60){
            time2.seconds=0;
                ++time1.minutes;
    
    
                if(time2.minutes==60){
                time2.minutes=0;
                ++time2.hour;
    
    
                    if(time2.hour==24)
                    time2.hour=0;
    
    
             }
      return time1;
      return time2;
    
    
    }
    
    
    //main function
    int main (void)
    {
    
    
          struct time time1={.hour=3,.minutes=45,.seconds=15};
          struct time time2={.hour=9,.minutes=44,.seconds=03};
          struct time timediff;
    
    
          timediff= elapsed_time(time1, time2);
    
    
    
    
    
    
        printf("Time elapsed from %i to %i is %i", );
    
    
        return 0;
    }

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by Dom View Post
    Code:
    //elapsed time
    struct time elapsed_time(struct time time1, struct time time2)
    {
        //time1
            ++time1.seconds;
            if(time1.seconds==60){<---- where is closing brace?
            time1.seconds=0;
                ++time1.minutes;
    
    
                if(time1.minutes==60){
                time1.minutes=0;
                ++time1.hour;
    
    
                    if(time1.hour==24)
                    time1.hour=0;
            }
          ++time2.seconds;
            if(time2.seconds==60){<---- where is closing brace?
            time2.seconds=0;
                ++time1.minutes;
    
    
                if(time2.minutes==60){
                time2.minutes=0;
                ++time2.hour;
    
    
                    if(time2.hour==24)
                    time2.hour=0;
    
    
             }
      return time1;
      return time2;
    
    
    }

    your if statement braces are off you dont have a closing one for 2 of them and you can only return 1 thing in a function, have you learned about pointers yet?

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    42

    help

    I'd like to get this running now but what do I need to fix. Just tell me what parts need to be fixed because the errors the program gives me doesn't explain very well.

    Code:
    #include <stdio.h>
    //Time display structure
    struct time
    {
        int hour;
        int minutes;
        int seconds;
    };
    
    
    //main function
    int main (void)
    {
          struct time time1={.hour=3,.minutes=45,.seconds=15};
          struct time time2={.hour=9,.minutes=44,.seconds=03};
          struct time timediff;
    
    
          timediff= elapsed_time(time1, time2);
    
    
        printf("Time elapsed from %i to %i is %.2i:%.2i:%.2i\n",time1, time2, time.hour, time.minutes, time.seconds);
    
    
        return 0;
    }
    
    
    //elapsed time
    struct time time1
        //time1
    
    
            ++time1.seconds;
    
    
            if(time1.seconds==60){
            time1.seconds=0;
                ++time1.minutes;
    
    
                if(time1.minutes==60){
                time1.minutes=0;
                ++time1.hour;
    
    
                    if(time1.hour==24)
                    time1.hour=0;
             }
      return time1;
    }
    
    
    struct time time2
        //time1
    
    
            ++time2.seconds;
    
    
            if(time2.seconds==60){
            time2.seconds=0;
                ++time2.minutes;
    
    
                if(time2.minutes==60){
                time2.minutes=0;
                ++time2.hour;
    
    
                    if(time2.hour==24)
                    time2.hour=0;
             }
      return time2;
    }

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Annnnnnnd what errors does it give you?

  14. #14
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by Dom View Post
    I'd like to get this running now but what do I need to fix. Just tell me what parts need to be fixed because the errors the program gives me doesn't explain very well.

    Code:
    #include <stdio.h>
    //Time display structure
    struct time
    {
        int hour;
        int minutes;
        int seconds;
    };
    
    
    //main function
    int main (void)
    {
          struct time time1={.hour=3,.minutes=45,.seconds=15};
          struct time time2={.hour=9,.minutes=44,.seconds=03};
          struct time timediff;
    
    
          timediff= elapsed_time(time1, time2);
    
    
        printf("Time elapsed from %i to %i is %.2i:%.2i:%.2i\n",time1, time2, time.hour, time.minutes, time.seconds);
    
    
        return 0;
    }
    
    
    //elapsed time
    struct time time1
        //time1
    
    
            ++time1.seconds;
    
    
            if(time1.seconds==60){
            time1.seconds=0;
                ++time1.minutes;
    
    
                if(time1.minutes==60){
                time1.minutes=0;
                ++time1.hour;
    
    
                    if(time1.hour==24)
                    time1.hour=0;
             }
      return time1;
    }
    
    
    struct time time2
        //time1
    
    
            ++time2.seconds;
    
    
            if(time2.seconds==60){
            time2.seconds=0;
                ++time2.minutes;
    
    
                if(time2.minutes==60){
                time2.minutes=0;
                ++time2.hour;
    
    
                    if(time2.hour==24)
                    time2.hour=0;
             }
      return time2;
    }
    First you need to define the "elapsed_time" function and have it calculate the difference. Once you get that working, you can put together an "normalize_time" function or some such to perform all of those "sanity checks" (which as of right now aren't even properly placed within functions). I'd also recommend reading through a C programming book before proceeding much further. Might as well get the basics down first...

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Dom View Post
    Then they have the arrogance to reply with something like "oh you don't know how to program, you need to learn from a textbook young man".
    Given your code samples... THAT is probably the best advice you could possibly get.

    It's not arrogance or deprocation... it's to help you write code that actually works.

    Ok... you don't speak French... but you move to France... Are you going to get ........ed at the French people when they try to help you learn the language?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a problem with the check digi in the isbn prog
    By polypic in forum C Programming
    Replies: 11
    Last Post: 04-17-2010, 01:02 PM
  2. Check for text file updation using C prog
    By me_ansh in forum C Programming
    Replies: 8
    Last Post: 02-02-2009, 04:57 PM
  3. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  4. C and implementation
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-04-2002, 08:00 AM
  5. Check out my prog
    By emus21 in forum Windows Programming
    Replies: 9
    Last Post: 07-01-2002, 01:34 AM