Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project.

Slides:



Advertisements
Similar presentations
1 Pointers and Strings Section 5.4, , Lecture 12.
Advertisements

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)
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
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.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 CS 105 Lecture 12 Pass- By-Reference, 2-D Arrays Version: Wed, Apr 20, 2011, 6:25 pm.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
1 CS 105 Lecture 13Vectors version of Wed, Apr 27, 2011, 6:20 pm.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Announcements Final Exam:. Review Passing Arrays as Parameters Pass Array Name into Function int intArray[100]; func(intArray); Accept into Function as.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
ITEC 320 C++ Examples.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
1 Chapter 1 C++ Basics Review Reading: Sections 1.4 and 1.5.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
1 Object-Oriented Programming Using C++ A tutorial for pointers.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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, 
Motivation and Overview
Pointers Psst… over there.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Vectors.
Pointers & Functions.
Lec 14 Oct 23, 02.
File Review Declare the File Stream Object Name
CS150 Introduction to Computer Science 1
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
COP 3330 Object-oriented Programming in C++
Pointers and dynamic objects
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
Class rational part2.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Announcements Exam 2 Lecture Grades Posted on blackboard
Presentation transcript:

Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project and Evaluations –Final Exam Reviews Final Exam Times: –Mon 10:00 Class: TBD –Mon 11:25 Class: TBD –Wed 6:25 Class: TBD –Changes: Send to NO LATE OR MAKEUP EXAMS GIVEN!!!

Survey Time Survey available at: survey.iit.edu

Vectors A Vector Is a Resizable Array Must Include File Declaration of Vector Syntax: vector variableName; vector variableName( size ); vector variableName( size, initialValue );

#include using namespace std; int main() { vector vals1; vector vals2(5); vector vals3(5,100); return(0); }

Using Vectors May Access Data the Same as Arrays Use Square Brackets ( [ ] )

#include using namespace std; int main() { vector vals1; vector vals2(5); vector vals3(5,100); vals2[0] = 6; cout << vals3[2]; return(0); }

Vector Public Functions Remember: ofstream Type Has Public Functions open() and close() Vector Has Public Functions as Well: –resize(int) to Change the Size of the Vector (usually larger) –size() to Return the Current Number of Members in the vector –push_back(element_type elem) a copy of elem is placed at the end of the vector.

#include using namespace std; int main() { vector vals1; vector vals2(5); vector vals3(5,100); vals2[0] = 6; cout << vals3[2] << endl; vals1.resize(50); cout << vals1.size() << endl; vals1.push_back(150); cout << vals1.size() << endl; return(0); }

Try This Declare a vector of 5 integers Assign the value 0 to each member of the vector 5 minutes go!

#include using namespace std; const int MAXVEC = 5; int main() { vector vals(MAXVEC); for (int i = 0; i < MAXVEC; i++) vals[i] = 0; return(0); }

Pass by Reference Pass by Reference Means Passing a Location of a Variable into a Function Modifying Variable in Function Will Modify Value in Original Variable In C++, Function Asks for Pass by Reference by using the Ampersand (&) before Parameter Name E.g. int func(int &num)

Passing Vectors to Functions Pass by Value by Default Not the Same as Passing Arrays Can Pass by Reference for Efficiency

Passing Vectors to Functions #include using namespace std; void func(vector &); int main() { vector intVector(5); int i; for(i = 0; i < 5; i++) intVector[i] = i; cout << intVector[0]; func(intVector); cout << intVector[0]; return(0); } void func(vector &passedVector) { passedVector[0] = 1000; }