1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Time addition program

Discussion in 'C' started by NoobProgrammer, Jan 16, 2009.

  1. NoobProgrammer

    NoobProgrammer New Member

    I am a noob programmer. I had a class in regular C programming that didn't prepare me for my C++ programming class. So my knowledge of programming is very limited. With that said, I've got an assginment here that I've started and have no idea how to complete. I was hoping that someone here could make this program. I know what you're thinking: How is that going to help you learn and why should I do it. As to the learning part, I really need to see what this program would look like to learn anything about it. Well, do with this what you'd like:



    **************************************************
    DESCRIPTION

    1.1
    Write a program that uses a class.

    1.2
    Your project will contain files CTime.h, CTime.cpp and TimeApp.cpp.

    1.2.1 (Class declaration)

    * Place the class declaration and inline functions in a seperate header file, CTime.h.

    * Class declaration:

    declare CTime as a class
    public:

    declare a defualt constructor
    define an inline destructor with an empty code body
    declare getHours as a member function
    declare getMinutes as a member function
    declare getSeconds as a member function
    declare setHours, setMinutes, setSeconds as a member function
    declare add as a member function with CTime return and a CTime parameter

    private:

    declare hours, minutes and seconds as an integer

    make the constructor, destructor, get- and set- functions inline.

    1.2.2 (Implementation CTime.cpp)

    * #include file CTime.h

    * place the noninline member function add in a separate file, CTime.cpp

    -convert the time values to total seconds before adding them
    example:
    CTime tm;
    long t1Seconds;
    t1Seconds = (long)tm.hours*3600 + tm.minutes*60 + tm.seconds;
    -add the total seconds together:
    t3Seconds = t1Seconds + t2Seconds;
    -divide them back out to make the new time hours, minutes and seconds.
    - store them in a class CTime variable before returning it.

    1.2.3 (member function add)

    Module add

    Return: CTime

    Paramaters: define t2 as a CTime

    Data:
    define t1Seconds, t2Seconds and t3Seconds as long integers
    Processing:

    convert hours, minutes and seconds to a long and assign to
    t1Seconds

    convert t2.hours, t2.minutes and t2.seconds toa long and assign to t2Seconds

    SET t3Seconds to t1Seconds + t2Seconds

    derive values from t3Seconds to assign to temp.hours, temp.minutes and temp.seconds

    return temp

    END add

    1.2.4 (Application file TimApp.cpp)

    this is where the prgoram asks the user for 2 times entered in this format:

    12:59:59

    the program adds the 2 times and displays the sum


    Here's the code I've done:


    CTime.h file:

    Code:
    /******************************* FILE: CTime.h* PROGRAMMER: * DATE: 01/14/09** Declaration of class CTime.*******************************/#ifndef CTime_H#define CTime_H    #include <iostream.h>    class   CTime     {    public:           CTime();           ~CTime()  {}                 int getHours();           int getMinutes();           int getSeconds();           int add();                 void setHours();           void setMinutes();           void setSeconds();                          private:            int hours;            int minutes;            int seconds;    };        inline int CTime::getHours()    {      return hours;         }    inline int CTime::getMinutes()    {      return minutes;        }    inline int CTime::getSeconds()    {      return seconds;        }    inline int CTime::add(CTime)    {      return CTime    }    inline void CTime::setHours(int hr)    {      hours = hr;              }    inline void CTime::setMinutes(int min)    {      minutes = min;             }    inline void CTime::setSeconds(int sec)    {      seconds = sec;             }        #endif
    and here's CTime.cpp
    Code:
    /******************************
    * FILE: CTime.cpp
    * PROGRAMMER: 
    * DATE: 01-14-09
    *
    * Implementation of class CTime.
    *******************************/
    #include "CTime.h"
    #include <iostream>using namespace std;int main(int argc, char *argv[])
    {
        int t1Seconds;
        int t2Seconds;
        int t3Seconds;
        
        int CTime::time()
        {
            t1Seconds = (long)time.hours*3600 + time.minutes*60 + time.seconds;
        
        
                     
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

  3. xpi0t0s

    xpi0t0s Mentor

    Doesn't work consistently: 1h 1min + 2h 2min = 3h 3min, but 101+202+40=343 = 3h 43 min.

    1h50 + 1h50 + 1h50 = 3h 150 = 5h 30, but 150*3=450; add 40=490. You have to add 40 twice to get the "correct" result of 530.

    So for this to work, you have to know how many overflows you've had, and add 40 for each one.

    And if you're going to calculate overflows, you may as well convert to minutes, add them up normally, then convert back, e.g. 1h30*3 = 90 min * 3 = 270 min; integer divide by 60=4h; 270-4*60=30 for the minutes.
     
  4. xpi0t0s

    xpi0t0s Mentor

    OP: no, you're wrong. You don't learn programming by looking at someone else's completed answer, you learn by doing. That's why the course presents a series of programming exercises for you to complete, rather than just going "Lesson 1, here look at this completed code. Lesson 2, here look at this completed code" and so on. So I'll help you fix the code, but I'm not going to write it for you.

    Most of your CTime.cpp is wrong; this file is just for the definition of the CTime class so you don't need any of the io stuff or a main function. I don't know why you've started a CTime::time() function; this is not in the class definition.

    What you need to do in CTime.cpp is pretty much already spelled out in detail:

    1.2.2 (Implementation CTime.cpp)
    * #include file CTime.h
    * place the noninline member function add in a separate file, CTime.cpp

    So do you know how to declare a non-inline member function? (Hint: it's exactly the same as an inline member function except (a) for the inline keyword and (b) it doesn't go in the header file.)

    Your definition in the header of add is slightly wrong:

    declare add as a member function with CTime return and a CTime parameter

    but you wrote:

    inline int CTime::add(CTime) { return CTime }

    which has an int return instead of a CTime return, and while it has a CTime parameter that parameter has no name. Also this function (a) is inline, which is not required and (b) has a function body, so when you declare the function in CTime.cpp you will get a duplicate symbol error. The function add() should just be a prototype.
     
  5. activ@

    activ@ New Member

    Good good......................
    You are clever
     
  6. shabbir

    shabbir Administrator Staff Member

    Accepted its not consistent.

    Now here is one more and probably better than that link. The idea is same but the algo is totally from my mind and may not work for all but brute force it as well and let me know the output

    1 hr 35 Min
    4 Hr 55 Min

    1.35
    3.55
    -----
    4.90
    -----

    + .40 only if greater than equal to .60 till it gets less than .60

    5.30 i.e 5 hours and 30 Mins

    1hr 1min
    2hr 2min

    1.1
    2.2
    ----
    3.3
    ----

    No need to add +.40

    1hr 50mins
    1hr 50mins
    1hr 50mins

    1.50
    1.50
    1.50
    -----
    4.50
    -----
    Now add 0.40

    4.90

    >= 0.60

    Add 0.40

    5.30

    Brute force for other cases and see if it works for all. I have tested for quite a few
     
  7. xpi0t0s

    xpi0t0s Mentor

    1.50
    1.50
    1.50
    -----
    4.50
    -----
    Now add 0.40

    But how do you know that you have to add .40? Dividing by 100 doesn't simplify the problem, which is essentially all you've done.

    2.00
    1.25
    1.25
    ===
    4.50

    So do we add .40 or not? Why?
     
  8. shabbir

    shabbir Administrator Staff Member

    By this logic.

    Add .40 only if greater than equal to .60 till it gets less than .60
     
  9. xpi0t0s

    xpi0t0s Mentor

    Only if what is >=60?
    155+155=310, so what do we compare with 60?
     
  10. shabbir

    shabbir Administrator Staff Member

    Agreed, Let me think over it then.
     
  11. asadullah.ansari

    asadullah.ansari TechCake

    As per my point of view,...

    Example 1h 55m + 1h 50 m
    Add 155+155 take 55 + 55 =110 , nCount=0
    Now compare it with 60 (if 110 > 60 ) 110 - 60 =50 ++nCoun;
    Answer will be (1+1+ ncount)H 50M

    Example2:
    1h50m + 1h 50m + 1h 50m
    => 50 +50 +50 =150 which is greater than 60 => 150-60 =90 , ncount=1
    => till less than 60 i.e. 90 - 60 =30 , nCount =2
    => So answer will be 1h+1h+1h+2=5h 30m


    NOTE: It's slightly diverse from your type of solution ...
     
  12. xpi0t0s

    xpi0t0s Mentor

    OK, so what's the point of the 155 value if you're going to reckon hours and minutes separately anyway?
     
  13. asadullah.ansari

    asadullah.ansari TechCake

    Same thing you are doing as adding 1h55m + 1h55m = 310
    for i=1 to numberofoverflow=1
    add 40 t0 310

    result will be : 3h 50m

    Example2 : 1h50m + 1h50m +1h50m = 450
    for i=1 to numberofoverflow=2
    add 40 to 450

    result = 5h 30m


    Now adding 40 in this case or substracting 60 where you have take minute parametere only .
    No difference....
     
  14. xpi0t0s

    xpi0t0s Mentor

    How might you implement that in C?
     
  15. asadullah.ansari

    asadullah.ansari TechCake

    Code:
    #include<iostream.h>
    #include<malloc.h>
    
    #define uint unsigned int
    int main()
    {
      uint noOfOverFlow=0,reqSum=0,i,hr[3],min[3],minuteSum=0;
       for(i=0;i<3;++i)
       {
         cout<<"Enter "<<i<< " Hour"<<endl;;
         cin>>hr[i];
         cout<<"Enter "<<i<< " minutes"<<endl;
         cin>>min[i];
       }
      minuteSum=min[0]+min[1]+min[2];
      noOfOverFlow = minuteSum/60;
      for(i=0;i<3;++i)
        reqSum= reqSum+ hr[i]*100+min[i];
      cout<<"reqSum"<<reqSum<<endl;
      minuteSum=(reqSum)%100;
        cout<<"MinuteSum"<<minuteSum<<endl;
    
      for(i=0;i<noOfOverFlow; ++i)
      {
        minuteSum = minuteSum +40;    
      }
     cout<<"MinuteSum"<<minuteSum<<endl;
    
    cout<<"Finally Sum is"<<((hr[0]+hr[1]+hr[2])+ noOfOverFlow)<<"hr"<<minuteSum%100<<endl;;
      
      return 0;
    }
    
    Output:
    Code:
    asadulla ~/temp> g++ test.cc
    asadulla ~/temp> ./a.out
    Enter 0 Hour
    1
    Enter 0 minutes
    50
    Enter 1 Hour
    1
    Enter 1 minutes
    50
    Enter 2 Hour
    1
    Enter 2 minutes
    50
    reqSum450
    MinuteSum50
    MinuteSum130
    Finally Sum is5hr30
    asadulla ~/temp> ./a.out
    Enter 0 Hour
    1 
    Enter 0 minutes
    90
    Enter 1 Hour
    1
    Enter 1 minutes
    90
    Enter 2 Hour
    1
    Enter 2 minutes
    90
    reqSum570
    MinuteSum70
    MinuteSum230
    Finally Sum is7hr30

    As i will suggest substraccting 60 is better solution than adding 40....
    adding 40 method is better if are calculating manually i.e. writing Competetive examination like CAT, GMAT etc.
     
  16. xpi0t0s

    xpi0t0s Mentor

    Seems a lot more work than:
    Code:
    void go4e_15847()
    {
      uint noOfOverFlow=0,reqSum=0,i,hr[3],min[3],minuteSum=0;
       for(i=0;i<3;++i)
       {
         cout<<"Enter "<<i<< " Hour"<<endl;;
         cin>>hr[i];
         cout<<"Enter "<<i<< " minutes"<<endl;
         cin>>min[i];
       }
      minuteSum=min[0]+min[1]+min[2]+(hr[0]+hr[1]+hr[2])*60;
      cout<<"Finally sum is "<<(minuteSum/60)<<"h "<<(minuteSum%60)<<"min"<<endl;
    }
    
     
  17. asadullah.ansari

    asadullah.ansari TechCake

    i have already told that ...
     

Share This Page