210 likes | 307 Views
Returning Structures Lesson xx. Objectives. Review return value from a function Returning structures Program returning a structure. Passing Arguments. int ans = compute ( z ); int compute ( float zz ) { int a; . . . return a; }. Returning Structures. struct student
E N D
Objectives • Review return value from a function • Returning structures • Program returning a structure
Passing Arguments intans = compute ( z ); int compute ( float zz) { int a; . . . return a; }
Returning Structures struct student { long id; int age; char sex; }; int main ( ) { student s; . . . s = fun (3); . . . } student fun (int n) { student found; . . . return found; }
Why Return a Structure ? struct student { long id; int age; char sex; }; int main ( ) { student s; . . . s = fun (3); . . . } student fun (int n) { student found; . . . return found; }
Program Specifications Write a program that will: Read in a start time and end time in the form hh mm. 2. Calculate and print the total time worked.
Program Code Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; struct time { int hr; int min; }; int main() { time start, end, hoursworked; inti; time calc_hrs (time s, time e);
Program Code Part 2 cout << "enter starting time in the form hh mm "; cin >>start.hr >>start.min; cout << "enter ending time "; cin >>end.hr >>end.min; hoursworked = calc_hrs (start, end); cout << "hours worked = " << hoursworked.hr << " minutes = “ << hoursworked.min; return 0; }
Program Code – Part 3 • /**************function to calculate number of hrs worked******/ • struct time calc_hrs (struct time s, struct time e) • { • struct time hw; • if (s.hr > e.hr) • hw.hr = 24-s.hr +e.hr; /*s = 2300 e = 0115*/ • else • hw.hr = e.hr - s.hr; /*s = 0115 e = 0315*/ • if (e.min < s.min ) /*s = 1215 e = 1509*/ • { • hw.min = 60 - s.min + e.min; • hw.hr--; • } • else • hw.min = e.min - s.min; /*s = 1212 e = 1413*/ • return (hw); • }
Structure Definition struct time { int hr; int min; };
Declarations int main() { time start, end, hoursworked; inti; time calc_hrs (time s, time e); hoursworked end hoursworked.hr end.hr hoursworked.min end.min start start.hr start.min
Inputs cout << "enter starting time in the form hh mm "; cin >>start.hr >>start.min; cout << "enter ending time "; cin >>end.hr >>end.min; hoursworked end hoursworked.hr 11 end.hr hoursworked.min 35 end.min start 9 start.hr 32 start.min
Function Call hoursworked = calc_hrs (start, end);
Print Time Worked cout << "hours worked = " << hoursworked.hr << " minutes = “ << hoursworked.min;
calc_hrs ( ) Function hoursworked = calc_hrs (start, end); time calc_hrs (time s, time e) { struct time hw; s e hw 9 11 s.hr hw.hr e.hr 32 35 hw.min s.min e.min
Calculate Hours Worked if (s.hr > e.hr) hw.hr = 24-s.hr +e.hr; /*s = 2300 e = 0115*/ else hw.hr = e.hr - s.hr; /*s = 0115 e = 0315*/
Calculate Minutes Worked if (e.min < s.min ) /*s = 1215 e = 1509*/ { hw.min = 60 - s.min + e.min; hw.hr--; } else hw.min = e.min - s.min; /*s = 1212 e = 1413*/
Returning the Time Worked return (hw); s e hw 9 11 s.hr hw.hr 2 e.hr 32 35 3 hw.min s.min e.min
Passing & Returning Mechanism hoursworked = calc_hrs (start, end); time calc_hrs (time s, time e) { struct time hw; . . . return hw; } s e hw 9 11 s.hr hw.hr 2 e.hr 32 35 3 hw.min s.min e.min
Why Return a Structure timecalc_hrs (time s, time e) { struct time hw; . . . return hw; }
Summary • Review return value from a function • Returning structures • Program returning a structure