Lab 4 APCS LAB 4 Parameters apvectors structs

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

Lab 4 A C++ program # includes //function declarations(with brief comment) //function definitions (with preconditions and postconditions) int main() void function 1() int function 2() 2 Westfield High School

Lab 4 # includes //function declarations int a, b; //function definitions int main() int a, c, d; void function 1() int a, d, e; global variables - accessible everywhere (almost) global ‘a’ not accessible here int function 2(int b) int a, c; global ‘a’, ’b’ not accessible here 3 Westfield High School

Lab 4 # includes //function declarations int a, b; //function definitions int main() int a, c, d; How do I refer to the global ‘a’ ? void function 1() int a, d, e; Why do some functions have empty parameter lists? int function 2(int b) int a, c; How do I return more than one value? 4 Westfield High School

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

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

Lab 4 Call-by-reference parameters ‘remembering’ more than one value void swap(int &a, int &b) { int temp; temp = a; a = b; b = temp; int max(int a, int b) { if (a > b ) return(a); else return(b); } } (pg 39) 7 Westfield High School

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

Lab 4 precondition: what is needed for the function to do its intended task ? postcondition : what is returned if precondition is satisfied? 9 Westfield High School

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

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

Lab 4 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) 12 Westfield High School

Lab 4 STRUCTS 13 Westfield High School

Lab 4 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 14 Westfield High School

Lab 4 An Example u struct employ. Type { apstring bool int double fname; lname; credit. Risk; age; income; 15 } Westfield High School

Lab 4 USE employ. Type faculty; faculty. fname = “sue”; faculty. lname = “smith”; faculty. credit. Risk = false; faculty. age = 21; faculty. income = 38000. 00; 16 Westfield High School

Lab 4 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 17 Westfield High School

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

Lab 4 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<<"t. Student ID#"<<"t"; cout <<"Student Name"<<"t"<<"GPA"<<endl; out<<"t======"<<"t======” <<"t==="<<endl; Westfield High School 111 ann smith 3. 5 222 joe jones 3. 24 333 amy johnson 2. 86 19
![Lab 4 while (instream>>num ) { apcs[count]. id = num; instream>>apcs[count]. first; instream>>apcs[count]. last; Lab 4 while (instream>>num ) { apcs[count]. id = num; instream>>apcs[count]. first; instream>>apcs[count]. last;](http://slidetodoc.com/presentation_image_h2/435dd977594b2c1c25751a5eec2d749f/image-20.jpg)
Lab 4 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 20 } Westfield High School

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

Lab 4 CELTICS : : : 22 Westfield High School

Lab 4 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. 23 Westfield High School

Lab 4 celtics Walter Popeye Ron Mc. Carty Jones Mercer f f g 5. 7 5. 2 17. 0 what does this look like Westfield High School 24

Lab 4 structs with initializer lists Struct Student. Type { Student. Type(); apstring char int Gender. Type apvector<int> } //constructor member function last; first; initial; class; gender; grades; 25 Westfield High School

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

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

Lab 4 celtics Walter none Popeye Ron Mc. Carty none Jones Mercer f x f g 5. 7 0. 0 5. 2 17. 0 what about this? Westfield High School 28

Lab 4 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 29 Westfield High School

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

Lab 4 team ? ? ? Kenny Anderson g 12. 1 apvector<playernode> ? ? ? ? 31 Westfield High School

Lab 4 team ? ? ? 07 apvector<int> ? ? 32 Westfield High School

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

Lab 4 Aqua. Fish 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<int> my. Position. Counts 34 Westfield High School

Lab 4 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 (tank. Size – 1). 35 Westfield High School

Lab 4 Tanksize = 10 Steps in this simulation = 20 Number of fish in this simulation = 8 Fish# Position Hits 0 1 2 3 4 5 6 7 8 9 -----------------------------------1 1 2 5 7 4 1 0 0 2 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 4 7 6 3 6 0 0 1 1 1 2 8 7 7 5 6 2 4 3 0 0 0 8 0 2 4 4 4 3 2 1 0 0 Westfield High School 36

Lab 4 Modifications: 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 aqua. Fish. h apvector<int> my. Position. Counts. 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 Print. Position. Counts(); 37 u Westfield High School

Lab 4 Modifications u Add a void public member function, Tank. Size() 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 18). There were 20 steps in the simulation so that if you sum the elements of each fish’s position. Count vector, the result is 20. 38 Westfield High School

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