1 / 23

Passing Structures Lesson xx

Passing Structures Lesson xx. Objectives. Review functions and passing arguments Passing structures Program passing structures. Passing Arguments. fun (a, 5.6); void fun ( int x, float y) { . . . }. Passing Structures. struct student { long id; int age;

zamora
Download Presentation

Passing 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. Passing StructuresLesson xx

  2. Objectives • Review functions and passing arguments • Passing structures • Program passing structures

  3. Passing Arguments fun (a, 5.6); void fun (int x, float y) { . . . }

  4. Passing Structures struct student { long id; int age; char sex; }; int main ( ) { student s; . . . fun (s); . . . } void fun (student x) { . . . }

  5. Program Specifications Write a program that does the following: Reads in today’s date 2. Calculates and prints tomorrow’s date

  6. Flowchart T F ! EOM T F EOY Add 1 to day New month New year

  7. Code – Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; struct date { int month; int day; int year; }; intlastDayOfMonth(const date&); // prototype intleapYear(const date&); // prototype int main() {   date today, tomorrow; cout << "enter today's date (mm ddyyyy):" << endl; cin >> today.month >> today.day >> today.year;

  8. Code - Part 2 if (today.day != lastDayOfMonth(today))   { tomorrow.day = today.day + 1; tomorrow.month = today.month; tomorrow.year = today.year;   }   else if (today.month == 12)   { tomorrow.day = 1; tomorrow.month = 1; tomorrow.year = today.year + 1;   }   else   { tomorrow.day = 1; tomorrow.month = today.month + 1; tomorrow.year = today.year;   }

  9. Code Part 3 cout << "tomorrow's date is "     << tomorrow.month << " " << tomorrow.day    << " " << tomorrow.year << endl;   return 0; } intlastDayOfMonth(const date& d) { int num; intdaysPerMonth[12] =     {31, 28, 31, 30, 31, 30,      31, 31, 30, 31, 30, 31};   if (leapYear(d) && (d.month == 2))     num = 29;   else     num = daysPerMonth[d.month - 1];   return num; }

  10. Code – Part 4 intleapYear(const date& da) { int flag = 0;   if ((((da.year % 4) == 0)     && ((da.year % 100) != 0))     || ((da.year % 400) == 0))     flag = 1;   return flag; }

  11. Structure Definition struct date { int month; int day; int year; };

  12. Declarations intlastDayOfMonth(const date&); // prototype intleapYear(const date&); // prototype int main() {   date today, tomorrow; tomorrow today.month tomorrow.month tomorrow.day today.day today.year tomorrow.year today

  13. Input cout << "enter today's date (mm ddyyyy):" << endl; cin >> today.month >> today.day >> today.year; 10 today.month tomorrow.month 17 tomorrow.day today.day 2020 today.year tomorrow.year tomorrow today

  14. Test for Not End of Month if (today.day != lastDayOfMonth(today))   { tomorrow.day = today.day + 1; tomorrow.month = today.month; tomorrow.year = today.year;   }   int x = lastDayOfMonth(today); if (today.day != x )   { tomorrow.day = today.day + 1; tomorrow.month = today.month; tomorrow.year = today.year;   }

  15. Explanation int x = lastDayOfMonth(today); if (today.day != x )   { tomorrow.day = today.day + 1; tomorrow.month = today.month; tomorrow.year = today.year;   }

  16. New Year else if (today.month == 12)   { tomorrow.day = 1; tomorrow.month = 1; tomorrow.year = today.year + 1;   }

  17. New Month else   { tomorrow.day = 1; tomorrow.month = today.month + 1; tomorrow.year = today.year;   }

  18. Print Tomorrow’s Date cout << "tomorrow's date is "     << tomorrow.month << " " << tomorrow.day    << " " << tomorrow.year << endl;   return 0; }

  19. lastDayof Month ( ) Function intlastDayOfMonth(const date& d) { int num; intdaysPerMonth[12] =     {31, 28, 31, 30, 31, 30,      31, 31, 30, 31, 30, 31};   if (leapYear(d) && (d.month == 2))     num = 29;   else     num = daysPerMonth[d.month - 1];   return num; }

  20. lastDayof Month ( ) Code intlastDayOfMonth(const date& d) { int num; intdaysPerMonth[12] =     {31, 28, 31, 30, 31, 30,      31, 31, 30, 31, 30, 31};   if (leapYear(d) && (d.month == 2))     num = 29;   else     num = daysPerMonth[d.month - 1];   return num; }

  21. leapYear() Function intleapYear(const date& da) { int flag = 0;   if ((((da.year % 4) == 0)     && ((da.year % 100) != 0))     || ((da.year % 400) == 0))     flag = 1;   return flag; }

  22. Advantage of Passing Structures int x = lastDayOfMonth (today); intlastDayOfMonth(const date & d) int x = lastDayOfMonth ( m, d , y ); int= lastDayOfMonth (int mm, intdd, intyy)

  23. Summary Review functions and passing arguments Passing structures Program passing structures

More Related