Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs."— Presentation transcript:

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

2 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()

3 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 - accessible everywhere (almost) global ‘a’ not accessible here global ‘a’,’b’ not accessible here

4 4 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; How do I refer to the global ‘a’ ? Why do some functions have empty parameter lists? How do I return more than one value?

5 5 Lab 4 Westfield High School functions with no arguments void menu() { cout << “directions appear here” <<endl; }

6 6 Lab 4 Westfield High School function 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); }

7 7 Lab 4 Westfield High School Call-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); }

8 8 Lab 4 Westfield High School #include 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; }

9 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?

10 10 Lab 4 Westfield High School apvectors u Quick Reference in Appendix A u member functions length() resize() u const reference parameters instead of call by value

11 11 Lab 4 Westfield High School n 2 SORTS u BUBBLE u SELECTION u INSERTION

12 12 Lab 4 Westfield High School LAB 4 ASSIGNMENT u Sorts (#1) sortall.cpp, the client program (written) sorts.h, the function declarations (written) sorts.cpp, the implementation (YOU MUST WRITE)

13 13 Lab 4 Westfield High School STRUCTS

14 14 Lab 4 Westfield High School STRUCTS u Structs are like simplified classes u By default, all data and functions are public. u A struct generally has no member functions only data no private stuff

15 15 Lab 4 Westfield High School An Example u structemployType { apstringfname; apstringlname; boolcreditRisk; intage; doubleincome; }

16 16 Lab 4 Westfield High School USE employTypefaculty; faculty.fname = “sue”; faculty.lname = “smith”; faculty.creditRisk = false; faculty.age = 21; faculty.income = 38000.00;

17 17 Lab 4 Westfield High School Why use structs??? u structures data u keeps information about 1 employee in one place u can manipulate employee (or a list of employees) easily

18 18 Lab 4 Westfield High School another example... struct studentnode { intid; apstring first; apstring last; apvector grades; bool sports; };

19 19 Lab 4 Westfield High School int main() { apvector apcs(10); intnum; intcount=0; apstrings; 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; 111 ann smith 3.5 222 joe jones 3.24 333 amy johnson 2.86

20 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); } 111 ann smith 3.5 222 joe jones 3.24 333 amy johnson 2.86

21 21 Lab 4 Westfield High School apvector of structs u student records u employee records u medical records u sports statistics u basketball teams...............

22 22 Lab 4 Westfield High School CELTICS :::::

23 23 Lab 4 Westfield High School Celtics assignment u Read info from file celtics. u fill array of structs. u jersey # is not a field...it is the index of the array element. u Print team info in good format for the user.

24 24 Lab 4 Westfield High School celtics Walter McCarty f 5.7 Ron Mercer g 17.0 Popeye Jones f 5.2 what does this look like

25 25 Lab 4 Westfield High School structs with initializer lists Struct StudentType { StudentType();//constructor member function apstring last; apstring first; char initial; int class; GenderType gender; apvector grades; }

26 26 Lab 4 Westfield High School Using initializer lists StudentType::StudentType() :grades(10,-1) //grades initialized to 10 items each storing -1.

27 27 Lab 4 Westfield High School Might want to look at struct3.cpp u initializer list for constructor of struct struct studentnode { intid; 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) { } };

28 28 Lab 4 Westfield High School celtics Walter McCarty f 5.7 Ron Mercer g 17.0 Popeye Jones f 5.2 what about this? none x 0.0

29 29 Lab 4 Westfield High School Celtics assignment u Ask user to enter 5 jersey numbers. u Inform user if team chosen if valid team (2 guards, 2 forwards, 1 center) u User should be able to enter as many teams as he/she wishes. u invalid teams: guards<>2, center<>1,forwards<>2 player on team more than once invalid jersey number entered

30 30 Lab 4 Westfield High School After we have our apvector of celtics… What does a “team” look like???

31 31 Lab 4 Westfield High School team ??? Kenny Anderson g 12.1 ??? apvector ?

32 32 Lab 4 Westfield High School team ??? 07 ??? apvector ?

33 33 Lab 4 Westfield High School CELTICS Efficient check vs Inefficient check

34 34 Lab 4 Westfield High School AquaFish revisited u 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 myPositionCounts

35 35 Lab 4 Westfield High School Print frequency distribution for a vector of aquafish u 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).

36 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 = 10 Steps in this simulation = 20 Number of fish in this simulation = 8

37 37 Lab 4 Westfield High School Modifications: u Change aquafish.h and aquafish.cpp so that there is one default constructor requiring no parameters. u Add a constant, TANKSIZE = 10 to aquafish.cpp u Add a private data field to aquaFish.h apvector myPositionCounts. This vector will hold the frequency of position hits for one aquafish. u Add a void member function to print, in an appropriate format, the position counts of an aquafish : void PrintPositionCounts();

38 38 Lab 4 Westfield High School Modifications u Add a void public member function, TankSize() that will return an Aquafish’s tanksize. u 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.

39 39 Lab 4 Westfield High School Lab 4 pages 20-38 in your labbook MBCS pages 1-18


Download ppt "1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs."

Similar presentations


Ads by Google