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

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Functions Prototypes, parameter passing, return values, activation frams.
OBJECT ORIENTED FEATURES AND IMPLEMENTATION OF UNIVERSAL QUANTIFICATION IN C++ Karoly Bosa RISC SS2000.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Structure.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Functions Pass by Value Pass by Reference IC 210.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
Flow of Control and Program Style n Nested if statements n C++ struct n Program Style n Lab Exercise.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CMSC 202 Lesson 5 Functions I. Warmup Use setw() to print the following (in tabular format) Fred Flintstone Barney Rubble Bugs Bunny Daffy Duck.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 C++ Classes and Data Structures Course link…..
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Introduction to Programming
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Specifications What? Not how!.
Global & Local Identifiers
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Stack Memory 2 (also called Call Stack)
Name: Rubaisha Rajpoot
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS150 Introduction to Computer Science 1
Reference Parameters.
Lab 1 Introduction to C++.
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
The Function Prototype
CS150 Introduction to Computer Science 1
CSCE 206 Lab Structured Programming in C
Scope Rules.
Presentation transcript:

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

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 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 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 Lab 4 Westfield High School functions with no arguments void menu() { cout << “directions appear here” <<endl; }

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 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 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 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 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 Lab 4 Westfield High School n 2 SORTS u BUBBLE u SELECTION u INSERTION

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

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 Lab 4 Westfield High School An Example u structemployType { apstringfname; apstringlname; boolcreditRisk; intage; doubleincome; }

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

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 Lab 4 Westfield High School another example... struct studentnode { intid; apstring first; apstring last; apvector grades; bool sports; };

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 joe jones amy johnson 2.86

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 joe jones amy johnson 2.86

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

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 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 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 Lab 4 Westfield High School Using initializer lists StudentType::StudentType() :grades(10,-1) //grades initialized to 10 items each storing -1.

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 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 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 Lab 4 Westfield High School After we have our apvector of celtics… What does a “team” look like???

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

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

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

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 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 Lab 4 Westfield High School Fish# Position Hits Tanksize = 10 Steps in this simulation = 20 Number of fish in this simulation = 8

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 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 Lab 4 Westfield High School Lab 4 pages in your labbook MBCS pages 1-18