1 / 39

APCS LAB 4

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;

flann
Télécharger la présentation

APCS LAB 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. APCS LAB 4 Parameters, apvectors, structs

  2. # includes //function declarations(with brief comment) //function definitions (with preconditions and postconditions) int main() A C++ program void function1() int function2()

  3. # 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

  4. # 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?

  5. functions with no arguments void menu() { cout << “directions appear here” <<endl; }

  6. 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

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

  8. #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; }

  9. precondition: what is needed for the function to do its intended task ?postcondition : what is returned if precondition is satisfied?

  10. apvectors • Quick Reference in Appendix A • member functions • length() • resize() • const reference parameters instead of call by value

  11. n2SORTS • BUBBLE • SELECTION • INSERTION

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

  13. STRUCTS

  14. 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

  15. An Example • struct employType { apstring fname; apstring lname; bool creditRisk; int age; double income; }

  16. USE employType faculty; faculty.fname = “sue”; faculty.lname = “smith”; faculty.creditRisk = false; faculty.age = 21; faculty.income = 38000.00;

  17. Why use structs??? • structures data • keeps information about 1 employee in one place • can manipulate employee (or a list of employees) easily

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

  19. 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;

  20. 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); }

  21. apvector of structs • student records • employee records • medical records • sports statistics • basketball teams...............

  22. CELTICS :::::

  23. 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.

  24. celtics Walter McCarty f 5.7 Popeye Jones f 5.2 Ron Mercer g 17.0 what does this look like

  25. structs with initializer lists Struct StudentType { StudentType(); //constructor member function apstring last; apstring first; char initial; int class; GenderType gender; apvector<int> grades; }

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

  27. 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) { } };

  28. celtics Walter McCarty f 5.7 none none x 0.0 Popeye Jones f 5.2 Ron Mercer g 17.0 what about this?

  29. 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

  30. After we have our apvector of celtics… What does a “team” look like???

  31. team ??? Kenny Anderson g 12.1 apvector<playernode> ? ???

  32. team ??? 07 apvector<int> ? ???

  33. CELTICS Efficient check vs Inefficient check

  34. 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

  35. 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).

  36. 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

  37. 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();

  38. 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.

  39. Lab 4 pages 20-38 in your labbook MBCS pages 1-18

More Related