1

I'm completing an O'Reilly textbook on my own and I'm now at structures. One of the programming exercises is:

Design a structure to store time and date. Write a function to find the difference between two times in minutes.

I believe I have the structure part down, I'm confused about the difference function though. I'm being very lazy by not taking into consideration the amount of days apart, but the question asks for time apart so I'm going to pretend as if it's just a 24 hour day they are talking about. Can I call a structure in the parameters of a function? I certainly tried to. Any suggestions would help. Thanks

My code thus far(in no way completed):

#include <iostream>


int difference(struct date_time);


int main()
{
    return 0;
}


struct date_time{
    int day;
    char month[20];
    int year;
    int second;
    int minute;
    int hour;
} super_date_time = {
    29,
    "may",
    2013,
    30,
    30,
    23
    };
int difference(date_time)
{
    int second1 = 45;
    int minute1 = 50;
    int hour1 = 24;

    std::cout << "Time difference is " << hour1 - int hour
    return 0;
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Maurice Abney
  • 223
  • 3
  • 14
  • ' Can I call a structure in the parameters of a function?' yes, you can. What was the other question? – Matthias May 30 '13 at 04:13
  • If the question is _how to pass a struct to a function_, the answers are here: 1) http://stackoverflow.com/questions/15181765/passing-structs-to-functions 2) http://stackoverflow.com/questions/15144396/passing-struct-between-functions-c – jogojapan May 30 '13 at 04:15
  • I don't share your satisfaction with your `struct date_time`. I know that disciplined use of accessors means that client code can access elements of the struct w/o regard to the order of elements within it, spare a thought for the poor sap who one day has to modify the code and has to bear in mind, constantly, that an expression such as `29,"may",2013,30,30,23` has to be interpreted as `DDMMMYYYYssmmhh`. It's just confusing. Alongside that the issue of using a string to represent month rather than an integer is a minor blemish. Don't forget, the poor sap doing the maintenance will be you. – High Performance Mark May 30 '13 at 05:43
  • 1
    @HighPerformanceMark I understand this isn't the best code in the world. I'm nowhere near even decent at coding yet. I just "really" got into programming two days ago. I'm doing these programming exercises to experience how things work. I'm going to keep at it everyday though. I enjoy positive criticism just as much as the next guy, but would you critic a 5 year old learning how to ride a bike? I really am I noob at this stuff. – Maurice Abney May 30 '13 at 06:22

2 Answers2

2

Sticking with your data structure ...

// Passing your structures by reference (&)
double MA_TimeDiffMinutes(const struct date_time& t1, const struct date_time& t2) {
  // As per your instruction, ignore year, month, day
  int diff = ((t1.hour - t2.hour)*60 + t1.minute - t2.minute)*60 + t1.second - t2.second;
  return diff/60.0;
}

int main() {
  struct date_time t_first;
  struct date_time t_next;
  // TBD fill the fields of t_first and t_next.
  cout << MA_TimeDiffMinutes(t_next, t_first) << endl;
}

Consider using an integer representation of month rather than a string.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Thanks for the help. Could you explain what t1 and t2 is? are they just a generic variables for a structure date_time? – Maurice Abney May 30 '13 at 18:24
  • `t1` and `t2` are the local names for the 1st and 2nd parameters of `MA_TimeDiffMinutes()`. The are simple generic variables names here. No special meaning other than `// MA_TimeDiffMinutes(t1, t2) calculates t1 - t2 difference in minutes` – chux - Reinstate Monica May 30 '13 at 19:12
0

yes, you can pass Structure as a parameter to function.

process(struct date_time T1) or
process(struct date_time *T1) (struct pointer)

you can compute difference by using a function like

difference(struct date_time *T1, struct date_time *T2) {  //T2 is recent time
  //process...
  std::cout<<"differ: "<<T2->hour-T1->hour<<"h "<<T2->minute-T1->minute<<"m "<<T2->seconds-T1->seconds<<"s "<<endl;
}

[subtract: recent time - old time]

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43