apcs lab 4

39
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs

Upload: flann

Post on 07-Jan-2016

74 views

Category:

Documents


4 download

DESCRIPTION

APCS LAB 4. Parameters, apvectors, structs. # includes //function declarations(with brief comment) //function definitions (with preconditions and postconditions). int main(). A C++ program. void function1(). int function2(). # includes //function declarations int a,b; - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: APCS   LAB 4

1

Lab 4

Westfield High School

APCS LAB 4

APCS LAB 4

Parameters, apvectors,

structs

Page 2: APCS   LAB 4

2

Lab 4

Westfield High School

A C++ program# includes //function declarations(with brief comment)

//function definitions (with preconditions and postconditions)

int main()

void function1()

int function2()

Page 3: APCS   LAB 4

3

Lab 4

Westfield High School

# includes //function declarations int a,b;//function definitions

int main()

void function1()

int function2(int b)int a,c;

int a, c, d;

int a,d,e;

global ‘a’ not accessible here

global variables - accessibleeverywhere (almost)

global ‘a’ not accessible here

global ‘a’,’b’ not accessiblehere

Page 4: APCS   LAB 4

4

Lab 4

Westfield High School

# includes //function declarationsint a,b;//function definitionsint main()

void function1()

int function2(int b)int a,c;

int a, c, d;

int a,d,e;

How do I refer to the global ‘a’ ?

Why do some functions haveempty parameter lists?

How do I return more than one value?

Page 5: APCS   LAB 4

5

Lab 4

Westfield High School

functions with no argumentsfunctions with no arguments

void menu()

{

cout << “directions appear here” <<endl;

}

Page 6: APCS   LAB 4

6

Lab 4

Westfield High School

function overloadingfunction overloading

int max(int a, int b)

{

if (a > b)

return(a);

else

return(b);

}

double max(double a, double b)

{

if (a > b)

return(a);

else

return(b);

}

Page 7: APCS   LAB 4

7

Lab 4

Westfield High School

Call-by-reference parameters‘remembering’ more than one valueCall-by-reference parameters‘remembering’ more than one value

void swap(int &a, int &b)

{

int temp;

temp = a;

a = b;

b = temp;

}(pg 39)

int max(int a, int b)

{

if (a > b )

return(a);

else

return(b);

}

Page 8: APCS   LAB 4

8

Lab 4

Westfield High School

#include <iostream.h>

int a=1,

b=2;

void dog (int a,int &b);

int main()

{

cout <<a<<" "<<b<<" "<<endl;

dog(a,b);

cout <<a<<" "<<b<<" "<<endl;

return(0);

}

void dog(int a,int &b)

{

a = 5;

b = 6;

cout <<a<<" "<<b<<" "<<endl;

}

Page 9: APCS   LAB 4

9

Lab 4

Westfield High School

precondition: what is needed for the function to do its intended task ?

postcondition : what is returned if precondition is satisfied?

Page 10: APCS   LAB 4

10

Lab 4

Westfield High School

apvectorsapvectors

Quick Reference in Appendix A member functions

• length()• resize()

const reference parameters instead of call by value

Page 11: APCS   LAB 4

11

Lab 4

Westfield High School

n2

SORTSn2

SORTS

BUBBLE SELECTION INSERTION

Page 12: APCS   LAB 4

12

Lab 4

Westfield High School

LAB 4 ASSIGNMENTLAB 4 ASSIGNMENT

Sorts (#1)• sortall.cpp, the client program (written)• sorts.h, the function declarations (written)• sorts.cpp, the implementation (YOU MUST

WRITE)

Page 13: APCS   LAB 4

13

Lab 4

Westfield High School

STRUCTSSTRUCTS

Page 14: APCS   LAB 4

14

Lab 4

Westfield High School

STRUCTSSTRUCTS

Structs are like simplified classes By default, all data and functions are public. A struct generally has

• no member functions• only data• no private stuff

Page 15: APCS   LAB 4

15

Lab 4

Westfield High School

An ExampleAn Example

structemployType

{apstring fname;

apstring lname;

bool creditRisk;

int age;

double income;

}

Page 16: APCS   LAB 4

16

Lab 4

Westfield High School

USEUSE

employType faculty;

faculty.fname = “sue”;

faculty.lname = “smith”;

faculty.creditRisk = false;

faculty.age = 21;

faculty.income = 38000.00;

Page 17: APCS   LAB 4

17

Lab 4

Westfield High School

Why use structs???Why use structs???

structures data keeps information about 1 employee in one

place can manipulate employee (or a list of

employees) easily

Page 18: APCS   LAB 4

18

Lab 4

Westfield High School

another example...another example...

struct studentnode

{

int id;

apstring first;

apstring last;

apvector<int> grades;

bool sports;

};

Page 19: APCS   LAB 4

19

Lab 4

Westfield High School

int main()

{

apvector <studentnode> apcs(10);

int num;

int count=0;

apstring s;

ifstream instream ("f:\\trees\\studinfo.txt");

if (instream.fail())

cout <<"failed"<<endl;

else

{

cout<<"\tStudent ID#"<<"\t";

cout <<"Student Name"<<"\t"<<"GPA"<<endl;

out<<"\t==========="<<"\t============”

<<"\t==="<<endl;

111annsmith3.5222joejones3.24333amyjohnson2.86

Page 20: APCS   LAB 4

20

Lab 4

Westfield High School

while (instream>>num )

{

apcs[count].id = num;

instream>>apcs[count].first;

instream>>apcs[count].last;

instream>>apcs[count].gpa;

apcs[count].sports = false;

cout<<setiosflags(ios::showpoint|ios::fixed);

cout<<setprecision(1);

cout<<"\t"<<setw(8)<<apcs[count].id;

cout<<"\t"<<apcs[count].first" ;cout "<<apcs[count].last<<"\

t"<<apcs[count].gpa<<endl;

cout<<endl;

count++;

}

} return(0);

}

111annsmith3.5222joejones3.24333amyjohnson2.86

Page 21: APCS   LAB 4

21

Lab 4

Westfield High School

apvector of structsapvector of structs

student records employee records medical records sports statistics basketball teams...............

Page 22: APCS   LAB 4

22

Lab 4

Westfield High School

CELTICS :::::

# fname lname position PPG

07 Kenny Anderson guard 12.1

08 Antoine Walker guard 18.7

34 Paul Pierce forward 16.5

40 Tony Battie center 10.0

05 Ron Mercer guard 17.0

Page 23: APCS   LAB 4

23

Lab 4

Westfield High School

Celtics assignmentCeltics assignment

Read info from file celtics. fill array of structs. jersey # is not a field...it is the index of the

array element. Print team info in good format for the user.

Page 24: APCS   LAB 4

24

Lab 4

Westfield High School

celticsceltics 0 1 2 3 4 5 6

Walter

McCarty

f

5.7

Ron

Mercer

g

17.0

Popeye

Jones

f

5.2

what does

this

look like

Page 25: APCS   LAB 4

25

Lab 4

Westfield High School

structs with initializer listsstructs with initializer lists

Struct StudentType{StudentType(); //constructor member functionapstring last;apstring first;char initial;int class;GenderType gender;apvector<int> grades;}

Page 26: APCS   LAB 4

26

Lab 4

Westfield High School

Using initializer listsUsing initializer lists

StudentType::StudentType()

:grades(10,-1)//grades initialized to 10 items each storing -1.

Page 27: APCS   LAB 4

27

Lab 4

Westfield High School

Might want to look at struct3.cppMight want to look at struct3.cpp

initializer list for constructor of structstruct studentnode

{

int id;

apstring first;

apstring last;

double gpa;

bool sports;

studentnode(int num = 0, apstring f = "some first name", apstring l = "some last name", double g = 0.0, bool s = true):

id(num), first(f), last(l), gpa(g), sports(s)

{ }

};

Page 28: APCS   LAB 4

28

Lab 4

Westfield High School

celticsceltics 0 1 2 3 4 5 6

Walter

McCarty

f

5.7

Ron

Mercer

g

17.0

Popeye

Jones

f

5.2

what

about

this?

none

none

x

0.0

Page 29: APCS   LAB 4

29

Lab 4

Westfield High School

Celtics assignmentCeltics assignment Ask user to enter 5 jersey numbers. Inform user if team chosen if valid team (2 guards, 2

forwards, 1 center) User should be able to enter as many teams as he/she

wishes. invalid teams:

• guards<>2, center<>1,forwards<>2• player on team more than once• invalid jersey number entered

Page 30: APCS   LAB 4

30

Lab 4

Westfield High School

After we have our apvector of celtics…

After we have our apvector of celtics…

What does a “team” look like???

Page 31: APCS   LAB 4

31

Lab 4

Westfield High School

team ???team ???

Kenny

Anderson

g

12.1

???

apvector<playernode> ?

Page 32: APCS   LAB 4

32

Lab 4

Westfield High School

team ???team ???

07

???

apvector<int> ?

Page 33: APCS   LAB 4

33

Lab 4

Westfield High School

CELTICSCELTICS

Efficient check

vs

Inefficient check

Page 34: APCS   LAB 4

34

Lab 4

Westfield High School

AquaFish revisitedAquaFish revisited

Keep a record of how many times each position in the fish tank was occupied by your fish.

• add to your aquafish private parts– apvector<int> myPositionCounts

Page 35: APCS   LAB 4

35

Lab 4

Westfield High School

Print frequency distribution for a vector of aquafish

Print frequency distribution for a vector of aquafish

Our fish’s next experience with an apvector will be keeping a record of how many times each position in the fish tank was occupied by each of the fish in an apvector. Recall that our fish tank is represented by a number line of positions 0 to (tankSize – 1).

Page 36: APCS   LAB 4

36

Lab 4

Westfield High School

Fish# Position Hits 0 1 2 3 4 5 6 7 8 9---------------------------------------------------------------------- 1 1 2 5 7 4 1 0 0 0 0  2 0 0 0 0 0 4 6 5 4 1  3 1 2 1 3 7 5 1 0 0 0  4 5 7 2 1 2 2 1 0 0 0  5 0 0 0 0 0 0 4 7 6 3  6 0 0 0 0 1 1 1 2 8 7  7 5 6 2 4 3 0 0 0 0 0  8 0 2 4 4 4 3 2 1 0 0

Tanksize = 10Steps in this simulation = 20Number of fish in this simulation = 8

Page 37: APCS   LAB 4

37

Lab 4

Westfield High School

Modifications:Modifications:  Change aquafish.h and aquafish.cpp so that there is

one default constructor requiring no parameters.  Add a constant, TANKSIZE = 10 to aquafish.cpp  Add a private data field to aquaFish.h

apvector<int> myPositionCounts . This vector will hold the frequency of position hits for one aquafish.

 Add a void member function to print, in an appropriate format, the position counts of an aquafish : void PrintPositionCounts();

Page 38: APCS   LAB 4

38

Lab 4

Westfield High School

ModificationsModifications

Add a void public member function, TankSize() that will return an Aquafish’s tanksize.

 Set mydebugging to false  Revise aquamain.cpp so that a sample run similar to

the one given will result. Notice that the constant TANKSIZE for this simulation was set to 10. There were 8 fish in the simulation (numbered 1-8). There were 20 steps in the simulation so that if you sum the elements of each fish’s positionCount vector, the result is 20.

Page 39: APCS   LAB 4

39

Lab 4

Westfield High School

Lab 4

pages 20-38 in your labbook

MBCS pages 1-18