1 / 21

Returning Structures Lesson xx

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

maida
Download Presentation

Returning Structures Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Returning StructuresLesson xx

  2. Objectives • Review return value from a function • Returning structures • Program returning a structure

  3. Passing Arguments intans = compute ( z ); int compute ( float zz) { int a; . . . return a; }

  4. 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; }

  5. 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; }

  6. 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.

  7. 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);

  8. 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; }

  9. 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); • }

  10. Structure Definition struct time { int hr; int min; };

  11. 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

  12. 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

  13. Function Call hoursworked = calc_hrs (start, end);

  14. Print Time Worked cout << "hours worked = " << hoursworked.hr << " minutes = “ << hoursworked.min;

  15. 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

  16. 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*/

  17. 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*/

  18. 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

  19. 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

  20. Why Return a Structure timecalc_hrs (time s, time e) { struct time hw; . . . return hw; }

  21. Summary • Review return value from a function • Returning structures • Program returning a structure

More Related