Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters.

Similar presentations


Presentation on theme: "1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters."— Presentation transcript:

1 1 Principles of Programming I Note Set #12

2 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters and Strings Structures/Records Binary File I/O

3 3 Overview Structured Data

4 4 Concept A structure is a container – It can hold a collection of pre-defined variables Used to organize these things into nice, neat packages

5 5 What’s in a struct? Each part in a structure is called a member Each member has a name, type and value Names of members follow rules of variable names Types can be any pre-defined types

6 6 Simple Structure Definition struct studentGrades { int hw1;// homework grades int hw2; int test1;// test grades int final; // final grade float avg;// final average }; structure name structure body

7 7 Using a struct By declaring a struct, you are in essence creating a new data type Once you have the struct defined, you can now create variables of the new type studentGrades stu1; studentGrades stu2;

8 8 Accessing Members You can treat the members of a struct just like variables Access the members of a struct using ‘.’ notation (dot-notation) cout << stu1.hw1; cout << stu2.hw1; 2 different variables

9 9 Accessing Members Still use ‘.’ notation to store data in struct members. stu1.test1 = 89; stu2.test2 = 99; Struct variable name Member of struct

10 10 Pointers to Strctures Because our structure is a type, we can declare a pointer to it. We can dynamically allocate a structure studentGrades *sgPtr; sgPtr = new studentGrades;

11 11 Pointers to Strctures To access a member with a pointer, we use the indirection operator ( -> ) studentGrades *sgPtr; sgPtr = new studentGrades; sgPtr->test1 = 101; cout test1; cin >> sgPtr->test2;

12 12 Arrays of Structures Since your struct is a type, you can have arrays of the structure. studentGrades cse1341[15]; cse1341[1].test1 = 93; cse1341[2].hw2 = 15; cout << “Test 1: “ << cse1341[1].test1;

13 13 Passing Structs Can pass to functions by Value, Reference, or Pointer Remember: Passing by value won’t change the original parameter from the calling function.

14 14 Passing By Value //in main cout << stu1.avg; updateAvg(stu1); cout << stu1.avg; void updateAvg(studentGrades sg) { float sum = sg.hw1 + sg.hw2 +sg.test1 + sg.final; sg.avg = sum/220; } Won’t Work!!!

15 15 Passing By Reference //in main cout << stu1.avg; updateAvg(stu1); cout << stu1.avg; void updateAvg(studentGrades &sg) { float sum = sg.hw1 + sg.hw2 +sg.test1 + sg.final; sg.avg = sum / 220; } Pass by Reference

16 16 Passing by Pointer //in main cout << stu1.avg; updateAvg(&stu1); cout << stu1.avg; void updateAvg(studentGrades * sg) { float sum = sg->hw1 + sg->hw2 +sg->test1 + sg->final; sg->avg = sum/220; }

17 17 DON’T DO THIS! Can’t initialize members in struct definition! struct geoInfo { char cityName[25]=“Dallas”; char state[15]; unsigned int population=3000000; };

18 18 Where to put struct definitions Can define anywhere before they are used. Typically (and required for 1341) they are placed in a separate file called a header file myStructFile.h. Use #include “myStructFile.h” quotes for your own header files

19 19 Structure Exercise Write a program that uses the geoInfo struct. We need to be able to keep track of up to 10 cities and need functionality that will allow us to find the average population of all the cities.

20 20 Arrays of Structures struct payInfo { int hours; float payRate; }; An Array of 5 payInfo Structures would be: payInfo employees[5];

21 21 Arrays of Structures cout << “Enter the hours worked by 5 “ << “employees and their hourly” << “ rates.” << endl; for (int i = 0; i < 5; i++) { cout << “Emp “ << i+1 << “:”; cout << “ Hours: “; cin >> employees[i].hours; cout << “ Pay Rate: “; cin >> employees[i].payRate; }

22 22 Arrays of Structures Write a function that will calculate and return the total amount the company will have to pay out for the 5 employees for the pay period.

23 23 Defining and Initializing payInfo worker1 = {40, 5.75}; payInfo emps[3] = { {40, 5.75}, {32, 9.00}, {41, 15.00} }; payInfo Worker[50] = { {0} }; Initializes the 1 st member of the 1 st element to this, and the rest to 0, just like before…

24 24 Defining and Initializing Declare an array of 5 payInfo structs and initialize the hours member to 40. payInfo abc[5] = { {40}, {40}, {40}, {40}, {40} };

25 25 Nested Structures A structure can be a member of another structure A structure can not have a member of its own data type. However, it can have a pointer to its own datatype

26 26 Nested Structures struct date { int mon; int day; int yr; }; struct place { char street[20]; char city[10]; char state[2]; char zip[7]; }; struct empInfo { char name[20]; int payRate; date birthDate; place homeAddy; };

27 27 Using Nested Structrues empInfo emp1; strcpy(emp1.name, “John Doe”); emp1.payRate = 5.75; emp1.birthDate.mon = 8; emp1.birthDate.day = 18; emp1.birthDate.yr = 1965; strcpy(emp1.homeAddy.street, “Airline”); strcpy(emp1.homeAddy.city, “Dallas”); strcpy(emp1.homeAddy.state, “TX”); strcpy(emp1.homeAddy.zip, “75275”);

28 28 Nested Structures struct empInfo { char Name[20]; int payRate; date birthDate; place homeAddy; } Notice that we are moving towards grouping variables together that represent one thing or one concept.

29 29 Nested Structures struct locat { float x; locat y; locat z; }; Structure can’t have a member whose data type is that structure

30 30 Nested Structures struct node { int pieceOfData; node * next; }; Used in a very powerful data structure called the linked list.

31 31 Structure Storage struct myLilStruct { int myInt; float myFloat; char myChar[5]; };

32 32 More with Structures and Functions

33 33 Structures and Functions struct rectangle { float length; float width; };

34 34 Structures and Functions void display(rectangle r) { cout << r.length << endl; cout << r.width << endl; } int main() { rectangle r = {5, 10}; display(r); return 0; }

35 35 Structures and Functions rectangle getInput() { rectangle rec; cout << “Enter length: “; cin >> rec.length; cout << “Enter width: “; cin >> rec.width; return rec; } int main() { rectangle r; r = getInput(); return 0; }

36 36 Questions?? ?


Download ppt "1 Principles of Programming I Note Set #12. 2 Semester Overview Functions Character File I/O Arrays Pointers and Dynamic Memory Allocation Characters."

Similar presentations


Ads by Google