Passing Structures Lesson xx

23
Passing Structures Lesson xx

description

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; - PowerPoint PPT Presentation

Transcript of Passing Structures Lesson xx

Slide 1

Passing StructuresLesson xx

In this module, well talk about passing structures to functions.1Objectives Review functions and passing argumentsPassing structuresProgram passing structures

Our goal is to review functions and passing arguments to functions. Then, well talk about passing structures to functions. Lastly well write a program to demonstrate the usefulness of structures.2Passing Argumentsfun (a, 5.6);

void fun (int x, float y) { . . . }

Lets 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 doesnt 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.3Passing Structuresstruct 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.4Program SpecificationsWrite a program that does the following:

Reads in todays date

2. Calculates and prints tomorrows date

Lets now write a program that demonstrates passing structures. Well read in todays date, calculate and print tomorrows date.5Flowchart

! EOMAdd 1 to dayEOYNew yearNew monthTTFF

Here is a flow chart of the main part of the code. Lets think about the general logic. Most of the times you add 1 to the day and you get tommorrows 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.6Code Part 1#include 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 today.month >> today.day >> today.year;

Part 1 of the code sets up the structure and reads in todays date.7Code - 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.8Code Part 3 cout