390 likes | 574 Views
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;
E N D
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; //function definitions global variables - accessible everywhere (almost) int main() global ‘a’ not accessible here int a, c, d; void function1() global ‘a’ not accessible here int a,d,e; int function2(int b) int a,c; global ‘a’,’b’ not accessible here
# includes //function declarations int a,b; //function definitions int main() How do I refer to the global ‘a’ ? int a, c, d; void function1() Why do some functions have empty parameter lists? int a,d,e; int function2(int b) int a,c; How do I return more than one value?
functions with no arguments void menu() { cout << “directions appear here” <<endl; }
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); } function overloading
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); } Call-by-reference parameters‘remembering’ more than one value
#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; }
precondition: what is needed for the function to do its intended task ?postcondition : what is returned if precondition is satisfied?
apvectors • Quick Reference in Appendix A • member functions • length() • resize() • const reference parameters instead of call by value
n2SORTS • BUBBLE • SELECTION • INSERTION
LAB 4 ASSIGNMENT • Sorts (#1) • sortall.cpp, the client program (written) • sorts.h, the function declarations (written) • sorts.cpp, the implementation (YOU MUST WRITE)
STRUCTS • 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
An Example • struct employType { apstring fname; apstring lname; bool creditRisk; int age; double income; }
USE employType faculty; faculty.fname = “sue”; faculty.lname = “smith”; faculty.creditRisk = false; faculty.age = 21; faculty.income = 38000.00;
Why use structs??? • structures data • keeps information about 1 employee in one place • can manipulate employee (or a list of employees) easily
another example... struct studentnode { int id; apstring first; apstring last; apvector<int> grades; bool sports; };
111 • ann • smith • 3.5 • 222 • joe • jones • 3.24 • 333 • amy • johnson • 2.86 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;
111 • ann • smith • 3.5 • 222 • joe • jones • 3.24 • 333 • amy • johnson • 2.86 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); }
apvector of structs • student records • employee records • medical records • sports statistics • basketball teams...............
Celtics 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.
celtics Walter McCarty f 5.7 Popeye Jones f 5.2 Ron Mercer g 17.0 what does this look like
structs with initializer lists Struct StudentType { StudentType(); //constructor member function apstring last; apstring first; char initial; int class; GenderType gender; apvector<int> grades; }
Using initializer lists StudentType::StudentType() :grades(10,-1) //grades initialized to 10 items each storing -1.
Might want to look at struct3.cpp • initializer list for constructor of struct 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) { } };
celtics Walter McCarty f 5.7 none none x 0.0 Popeye Jones f 5.2 Ron Mercer g 17.0 what about this?
Celtics 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
After we have our apvector of celtics… What does a “team” look like???
team ??? Kenny Anderson g 12.1 apvector<playernode> ? ???
team ??? 07 apvector<int> ? ???
CELTICS Efficient check vs Inefficient check
AquaFish 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
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).
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 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
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();
Modifications • 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.
Lab 4 pages 20-38 in your labbook MBCS pages 1-18