230 likes | 381 Views
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;
E N D
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; char sex; }; int main ( ) { student s; . . . fun (s); . . . } void fun (student x) { . . . }
Program Specifications Write a program that does the following: Reads in today’s date 2. Calculates and prints tomorrow’s date
Flowchart T F ! EOM T F EOY Add 1 to day New month New year
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;
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; }
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; }
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; }
Structure Definition struct date { int month; int day; int year; };
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
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
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; }
Explanation int x = lastDayOfMonth(today); if (today.day != x ) { tomorrow.day = today.day + 1; tomorrow.month = today.month; tomorrow.year = today.year; }
New Year else if (today.month == 12) { tomorrow.day = 1; tomorrow.month = 1; tomorrow.year = today.year + 1; }
New Month else { tomorrow.day = 1; tomorrow.month = today.month + 1; tomorrow.year = today.year; }
Print Tomorrow’s Date cout << "tomorrow's date is " << tomorrow.month << " " << tomorrow.day << " " << tomorrow.year << endl; return 0; }
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; }
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; }
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; }
Advantage of Passing Structures int x = lastDayOfMonth (today); intlastDayOfMonth(const date & d) int x = lastDayOfMonth ( m, d , y ); int= lastDayOfMonth (int mm, intdd, intyy)
Summary Review functions and passing arguments Passing structures Program passing structures