1 principles of programming i note set #12. 2 semester overview functions character file i/o arrays...

36
1 Principles of Programming I Note Set #12

Upload: randolph-cole

Post on 29-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

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

1

Principles of Programming I

Note Set #12

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

2

Semester Overview

FunctionsCharacter File I/O ArraysPointers and Dynamic Memory

AllocationCharacters and StringsStructures/RecordsBinary File I/O

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

3

Overview

Structured Data

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

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

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

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

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

6

Simple Structure Definition

struct studentGrades {int hw1; // homework gradesint hw2;int test1; // test gradesint final; // final gradefloat avg; // final average

};

structure name

structure body

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

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;

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

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 differentvariables

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

9

Accessing Members

• Still use ‘.’ notation to store data in struct members.

stu1.test1 = 89;stu2.test2 = 99;

Struct variablename

Member ofstruct

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

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;

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

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 << sgPtr->test1;

cin >> sgPtr->test2;

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

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;

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

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.

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

14

Passing By Value

//in maincout << 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!!!

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

15

Passing By Reference

//in maincout << 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 byReference

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

16

Passing by Pointer

//in maincout << 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;}

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

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;};

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

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

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

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.

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

20

Arrays of Structures

struct payInfo{

int hours;float payRate;

};

An Array of 5 payInfo Structures would be:

payInfo employees[5];

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

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;}

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

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.

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

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 1st member of the 1st element to this, and the rest

to 0, just like before…

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

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} };

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

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

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

26

Nested Structuresstruct 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;};

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

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”);

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

28

Nested Structuresstruct 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.

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

29

Nested Structures

struct locat {float x; locat y; locat z;

};

Structure can’t havea member whosedata type is thatstructure

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

30

Nested Structures

struct node {int pieceOfData;node * next;

};

Used in a very powerful data structure called the linked list.

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

31

Structure Storage

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

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

32

More withStructures

andFunctions

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

33

Structures and Functions

struct rectangle {float length;float width;

};

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

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;

}

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

35

Structures and Functionsrectangle 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;

}

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

36

Questions??

?