Presentation is loading. Please wait.

Presentation is loading. Please wait.

Passing Structures Lesson xx

Similar presentations


Presentation on theme: "Passing Structures Lesson xx"— Presentation transcript:

1 Passing Structures Lesson xx
In this module, we’ll talk about passing structures to functions.

2 Objectives Review functions and passing arguments Passing structures
Program passing structures Our goal is to review functions and passing arguments to functions. Then, we’ll talk about passing structures to functions. Lastly we’ll write a program to demonstrate the usefulness of structures.

3 Passing Arguments fun (a, 5.6); void fun (int x, float y) { . . . }
Let’s review functions. The statement in red is how you call a function. You put the name of the function and then inside a set of ( )s, you list the arguments. In this case, we are calling function fun() and sending in a and 5.6. a has to be an int variable and 5.6 is a float. The header of the function is: void fun (int x, float y). Void means that the function doesn’t return anything, fun is the name of the function. int x, float y means that we have 2 arguments that are being sent in. x is an in and y is a float.

4 Passing Structures struct student { long id; int age; char sex; };
int main ( ) student s; . . . fun (s); } void fun (student x) Here is an example of how you pass a structure into a function. We have defined a structure called student at the beginning of our code. In order to declare a structure variable, we write student s; Once we have initialized s with some data, we can call function fun and pass s as an argument. When we get into the function, a copy of what is in s is copied into the local variable x. By the way, structures are passed by value. Thismeans that changing x will not change s. If want to change s to change, you need to pass by reference by using the & between the data type and the argument.

5 Program Specifications
Write a program that does the following: Reads in today’s date 2. Calculates and prints tomorrow’s date Let’s now write a program that demonstrates passing structures. We’ll read in today’s date, calculate and print tomorrow’s date.

6 Flowchart ! EOM Add 1 to day EOY New year New month F T F T
Here is a flow chart of the main part of the code. Let’s think about the general logic. Most of the times you add 1 to the day and you get tommorrow’s date. Ok so, 1) we check to see if it is not the end of the month (!EOM). If it is not the end of the month we add 1 to the day, month and year remain the same. 2) if it is the end of the month, there are two separate cases, a)end of year or b) regular end of the month. If it is the end of the year (EOY), tomorrow is the 1st of January in the next year. If it is a regular end of the month, tomorrow is the 1st of the next month.

7 Code – Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; struct date {   int month;   int day;   int year; }; int lastDayOfMonth(const date&); // prototype int leapYear(const date&); // prototype int main() {   date today, tomorrow;   cout << "enter today's date (mm dd yyyy):" << endl;   cin >> today.month >> today.day >> today.year; Part 1 of the code sets up the structure and reads in today’s date.

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;   } Part 2 of the code is the heart of the program which figures out tomorrows date.

9 Code Part 3   cout << "tomorrow's date is "     << tomorrow.month << " " << tomorrow.day     << " " << tomorrow.year << endl;   return 0; } int lastDayOfMonth(const date& d) {   int num;   int daysPerMonth[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; } Part 3 of the code prints out tomorrows date. It also contains the function which figures out the last day of the month.

10 Code – Part 4 int leapYear(const date& da) {   int flag = 0;   if ((((da.year % 4) == 0)     && ((da.year % 100) != 0))     || ((da.year % 400) == 0))     flag = 1;   return flag; } Part 4 of the code is the leap year function. We will now go through the entire program.

11 Structure Definition struct date { int month; int day; int year; };
Our goal in this program is to read in today’s date and print tomorrow’s date. A typical date consists of a month, day and year. Our structure called date allows us to group these items together and think of a date as 1 entity rather than 3 separate items.

12 Declarations int lastDayOfMonth(const date&); // prototype int leapYear(const date&); // prototype int main() {   date today, tomorrow;   today tomorrow.month tomorrow tomorrow.year tomorrow.day today.month today.day today.year Outside of main(), we have 2 function declarations. We’ll discuss the functions later. Inside main(), we have declared 2 date structures called today and tomorrow. We will read today’s date into today and we will store tomorrow’s date in the variable tomorrow.

13 Input   cout << "enter today's date (mm dd yyyy):" << endl;   cin >> today.month >> today.day >> today.year; tomorrow today 10 17 2020 today.month today.day today.year tomorrow.month tomorrow.day tomorrow.year These 2 lines of code prompt the user for today’s date and reads it into the structure variable today. At this point in the program, if the user entered the date 10/17/2020, the memory looks as shown.

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;   }   In the top piece of code, we are testing to see if the date read in is not the end of the month. If it is not the end of the month we calculate tomorrow by adding 1 to the day, month and year remain the same. The code on the bottom does the same thing but breaks the problem down in to smaller pieces.

15 Explanation int x = lastDayOfMonth(today);
if (today.day != x )   {     tomorrow.day = today.day + 1;     tomorrow.month = today.month;     tomorrow.year = today.year;   }   Let’s look at the modified code. In the lastDayOfMonth ( ) function, we pass in a structure called today and it returns the # of days in a given month. For instance, if the structure today contained 10/17/2020 the function would return 31 into x because there are 31 days in October . If today.day != x, this implies that it is not the end of the month.

16 New Year else if (today.month == 12)   {     tomorrow.day = 1;     tomorrow.month = 1;     tomorrow.year = today.year + 1;   } We get to this piece of code if the date read in is the end of the month. However, there are 2 situations which we have to account for: new year and new month. In this case, if it is the end of the month and the month happens to be 12, tomorrow’s date is January 1 of the next year.

17 New Month else   {     tomorrow.day = 1;     tomorrow.month = today.month + 1;     tomorrow.year = today.year;   } We get to this piece of code if the date read in is the end of the month and the month is != 12. This implies that it is the end of the month and the next day is the 1st of the next month. The day becomes 1, we add 1 to the month and the year remains the same.

18 Print Tomorrow’s Date   cout << "tomorrow's date is "     << tomorrow.month << " " << tomorrow.day     << " " << tomorrow.year << endl;   return 0; } In the if statements, we have calculated and stored tomorrow’s date in the structure variable called tomorrow. All we have to do here at the end of main() is to print the members of tomorrow and we get our answer.

19 lastDayof Month ( ) Function
int lastDayOfMonth(const date& d) {   int num;   int daysPerMonth[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; } In the lastDayofMonth() function, we send in a structure variable which contains the month, day and year. This function returns the # of days in a given month. For example, if the date read in is 3/17/2012, this function would return the #31 because the last day of March is 31. This function also takes care of leap year by calling the leapYear() function.

20 lastDayof Month ( ) Code
int lastDayOfMonth(const date& d) {   int num;   int daysPerMonth[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; } Let’s go thru the specifics of the lastDayofMonth() function. We set up an array called dayPerMonth which is loaded with the # of days in each month. We check to see if it is leap year and the month is February. If it is, there are 29 days in that month. If it isn’t leap year, we can look up the # of days in the dayPerMonth array. Either way, num contains the # of days in the given month and we return that value.

21 leapYear() Function int leapYear(const date& da) {   int flag = 0;   if ((((da.year % 4) == 0)     && ((da.year % 100) != 0))     || ((da.year % 400) == 0))     flag = 1;   return flag; } If a year is divisible by 4 and not divisible by 100, it is a leap year. Or, if a year is divisible by 400 it is also a leap year. The function returns a 1 if it is leap year and returns a 0 if it is not leap year.

22 Advantage of Passing Structures
int x = lastDayOfMonth (today); int lastDayOfMonth(const date & d) Let’s see why it is so advantageous to use structures. In the top box, we pass in 1 structure into the lastDayOfMonth() function. If we didn’t have structures, we’d have to pass the month, day and year as separate items as in the bottom box. Structures allow you to group related items together and think of them a 1 single entity. int x = lastDayOfMonth ( m, d , y ); int = lastDayOfMonth (int mm, int dd, int yy)

23 Summary Review functions and passing arguments Passing structures
Program passing structures In this module, we reviewed passing arguments to functions. We discussed passing structures to functions and finally we wrote a program where we passed a structure to a function.


Download ppt "Passing Structures Lesson xx"

Similar presentations


Ads by Google