Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.

Slides:



Advertisements
Similar presentations
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Advertisements

1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CS352-Week 2. Topics Heap allocation References Pointers.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Variable Scope Storage Class Recursion
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 5 An Array Class Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Classes and Data Structures Jeffrey S. Childs
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Variables and memory addresses
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
Lecture 01a: C++ review Topics: Setting up projects, main program Memory Diagrams Variables / Types (some of) the many-types-of-const's Input / Output.
1 Introduction to Object Oriented Programming Chapter 10.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Functions CMSC 202. Topics Covered Function review More on variable scope Call-by-reference parameters Call-by-value parameters Function overloading Default.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Chapter 5 Function Basics
Lecture 6 C++ Programming
group work #hifiTeam
Data Structures with C++
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Given the code to the left:
What about multi-dimensional arrays?
Data Structures with C++
Arrays an array of 5 ints is filled with 3,2,4,1,7
Dynamic Memory.
Class: Special Topics Overloading (methods) Copy Constructors
TOPIC: FUNCTION OVERLOADING
Pointers and References
Classes: Arrays group many objects of the same type (in order)
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Week 2

Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3

Functions (cont.): Unlike other languages, the c++ compiler must be made aware of functions before you can use them. E.g., this won’t work: #include using namespace std; int main() { int x = max3(4,7,3); // problem cout << x << endl; return 0; } //main int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3

Functions (cont.): When calling a function, the compiler must be aware of the function. You must declare a function before calling it. #include using namespace std; int max3(int x, int y, int z); // Here is our function declaration int main() { int x = max3(4,7,3); cout << x << endl; return 0; } //main int max3(int num1, int num2, int num3) {int result=max(max(num1,num2),num3); return result; } //max3 Declaration: The compiler can now handle most uses of the function without needing the full definition of the function. Declaring a function lets you write code the compiler can understand without all of the details. Especially useful when one file is using functions in another file

Function Arguments:  Default is call by value  This means that a copy of the actual value is copied into the parameter. void swap (int j, int k); int main() { int x = 7; int y = 3; swap (x,y); cout << x << endl; //what gets printed? cout << y << endl; return 0; } //main void swap (int num1, int num2) {int temp = num1; num1 = num2; num2 = temp; cout << num1 << endl; //what gets printed? cout << num2 << endl; } //main  Works this way for all primitive types (including strings)

void swap(int *x, int *y); // function declaration int main () { int a = 100; int b = 200; /* calling a function to swap the values. * &a gets the address of a * &b gets the address of b */ swap(&a, &b);//we’re not sending in the values, we’re sending in addresses in memory cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; } //main void swap(int *x, int *y) // *x is a variable that holds an address of another variable that holds an int { int temp; temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put x into y return; } //swap Now we can change parameter values and they will stay changed outside of the function! Call by Pointer:  Copies the address of an argument into the parameter.  Inside the function, the address is used to access the actual value.  This means that changes made to the parameter affect the value.

& versus *  int x = 4; // this actually sets aside an address in memory at a location large enough to hold an int. It then puts the value 4 into that address.  cout << x << endl; // prints out the value at location x. Compiler assumes you want to use the value at that location, not the location.  cout << &x << endl; // this gives you access to the address of x in memory  int y = &x; // can’t do. The address of x is not an int, and y is automatically making a new address at a location in memory that will hold an int.  int *y = &x; //can do. Now I’m saying the variable y is an address that holds the address of another variable that holds an int  …or y points to a location that holds an int,  cout << y << “ “ << *y << endl; //gives you 0x32ff1c 4

& versus *  int x = 4;  cout << x << endl;  cout << &x << endl;  int *y = &x;  cout << y << endl;  cout << &y << endl;  cout << *y << endl;  //y points to x – gives us the value at //the address inside of y, not the value //inside of y x ( 0x32ff3c ) y ( 0x4f3d21 ) 4 Space in memory for an int 0x32ff3c Space in memory for a memory address (for an int) (getting address)

Call by value vs. by reference (pointers):  Call by Reference:  Can change variables from one function within the confines of another function  Makes code more readable  Reduces or eliminates the need for global variables  Better form (I was taught never to use global variables)  Easier to read  Don’t have variables sticking around when you’re done with them  Smaller memory footprint  We’re not making a new copy of each value passed into the function as a parameter. Why not always use Call by Reference?  Call by Value:  Can’t accidentally change a value in another function  Make sure, if you’re working with others, they don’t accidentally change the value  Maintain “privacy”

Random Numbers: #include … int x = rand(); // generates a random number between 0 and a max number defined in stdlib  Problem: if not given a specific seed, it will always start with the same seed. So you will get the same sequence of random numbers.  We want to give it a seed, and each time we run our program, we want a new seed. So we often use the current time as our new seed.  To generate a seed using the current time: #include … srand(time(NULL)) // creates a seed based on the current time (down to the millisecond) // Only need to create a seed once in a program // Must create seed BEFORE you use rand for the first time. int x = rand(); // now rand uses this seed in the calculation of the random number. // Now you won’t always get the same sequence of random numbers Question: How would you generate a random number between 1 and 10?

Math stuff:  #include  …  double d = ;  int i = 100;  int j = -100;  cout << sin(d) << endl; //  cout << squrt(i) << endl; // 10  cout << pow(i,2) << endl; //  cout << floor(d) << endl; // 200  cout << abs(j) << endl; //100 - only works with ints  cout << fabs(d) << endl; // – only works with doubles and floats

Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0; float b[3] = {3.2,1.4};  makes an array of spaces for 7 ints; the array at 3 holds 24, rest holds nothing (if you print, will print their memory addresses ) int c[7]; c[3] = 24; cout << c[2] << endl; // prints something like 0x32ff3c, the memory address  Makes an array of 3 ints, holding 4,1,2 int d[] = {4,1,2};  Next: int d[]; d[0] = 4;  yeah, no. d[] is space for an address that will be where we hold the first int in the array, but so far no space has been set aside for the int, so there’s no address for the int.  Can we set aside space? Hold that thought…

Passing arrays into functions:  In terms of memory space, do we want to make a new, local copy of the array when we call a function?  To get the address of the array (aka a pointer to the array): int x[4] = {3,2,1,4}; cout << x << endl; //address of array, of where first value in array is cout << &x[0] << endl; //same value, address of first value in array

Arrays and Functions:  Note: no straightforward way of getting size of an array. We most likely want to pass in the length of the array along with a pointer to the array (aka the address of the array), e.g., int main() { int x[4]= {3,2,4,1}; double k = getAverage(&x[0],4); // gets the address of the first value in the array // Or double l = getAverage(x,4); // the address of the first value in the array cout << k << endl; } // main Function definition: double getAverage(int *arr, int size) // a pointer, a variable that holds an address {int sum = 0; for (int i = 0; i < size; i++) {sum += arr[i]; // now, in essence, the value at (the address of arr + i) } //for double avg = double(sum) / size; return avg; }//main Or double getAverage(int arr[], int size) { //holds the address of the first value in the array }

Returning an array int * createArray(int size) // what is returned??? { int r[size] = {3,2,4,1}; return r; // or return &r[0]; } //createArray int main() { int *a; a = createArray(4); for (int x = 0; x < 4; x++) { cout << a[x] << endl; } //for return 0; }//main

Dynamically allocated arrays  Means we make a pointer and then allocate the space for the values in the array  Maybe later, when we find out how many things will be in the array  Quick info: If the size of something is known when the program is compiled, then memory is set aside for it on the stack  int x = 3;  float y;  If the size of something is determined when the program is run, then there’s something called the heap (still in memory, but in a different place) that holds data. Stuff that goes on the heap is “dynamically allocated”.  The operator new sets aside space in the heap.  We often use pointers  If we allocate it, we should free it up

New and delete new int; // sets aside memory space on the heap for an int (but no variable points to it int *x; x = new int; // now x points to (holds the address of) space for an int on the heap *x = 34; // now the x points to 34; What if there’s not enough space on the heap? int *x; if( !(x = new int )) { cout << "Error: out of memory." <<endl; // checking to see if there’s space on the heap exit(1); } //if else { *x = 2; } //else And when you’re done with it… int *x; if( !(x = new int )) { cout << "Error: out of memory." <<endl; // checking to see if there’s space on the heap exit(1); } //if else { *x = 2; } //else delete x; //deallocates the space in memory

What about arrays? int *x = NULL; // Pointer initialized with null (default is undefined) cout << “Enter the number of numbers you want” << endl; int y; cin >> y; x = new int[y]; // Request memory on heap for space for 20 ints (sequentially) for (int k = 0; k < y; k++) { x[k] = k; } //for And now to free an array? delete [] x;

What about multi-dimensional arrays? int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { x[i][j] = i+j; } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { cout << x[i][j]; } //for cout << endl; } //for

How about deleting? int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { x[i][j] = i+j; } //for Why can’t we just do this? delete [] x; // compiles just fine…

Deleting a multi-dimensional array int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 3x4 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int i = 0; i < 4; i++) { delete [] x[i]; } //for delete [] x;

Quick: int x = 3; int y = x; int *z = &x; cout << “x is “ << x <<endl; cout << “ y is “ << y << endl; cout << “ z is “ << *z << endl; x += 4; cout << “x is “ << x <<endl; cout << “ y is “ << y << endl; cout << “ z is “ << *z << endl;

Structs  Arrays group many objects of the same type (in order)  Structs group objects of different types  Structs are similar to classes  Struct properties are, by default, public.  Structs often hold different types together (that logically belong together)  Example: struct StudentInfo { string fname; string lname; int id; }; //StudentInfo int main() { StudentInfo Sam; Sam.fname = "Sam"; Sam.lname = "Stone"; Sam.id = 3241; StudentInfo studarr[5]; studarr[1].fname = "Taylor"; cout << studarr[1].fname << endl; return 0; } //main

What do you think? struct StudentInfo { string fname; string lname; int id; }; //StudentInfo void changeStud(struct StudentInfo x) { x.fname = "Samantha"; } //changeStud int main() { StudentInfo Sam; Sam.fname = "Sammy"; Sam.lname = "Stone"; Sam.id = 3241; changeStud(Sam); cout << Sam.fname << endl; // what do you think? return 0; } //main

Call by Pointer struct StudentInfo { string fname; string lname; int id; }; //StudentInfo void changeStud(struct StudentInfo *x) { x->fname = "Sammy"; //new, means go to the name property in the address in x } //changeStud int main() { StudentInfo Sam; Sam.fname = "Sam"; Sam.lname = "Stone"; Sam.id = 3241; changeStud(&Sam); cout << Sam.fname << endl; // what do you think? return 0; } //main

Classes class Rect { int len; //default for properties and methods: private int wid; public: // everything below this is now public (unless I change it back) int calcArea(); // method DECLARATION void setLen(int x) { //method definition (setter) len = x; } void setWid(int x) { wid = x; } int getLen() { // getter return(len); } int getWid() { return(wid); } }; int Rect::calcArea() { // Rect method definition (in the scope of Rect) return len * wid; } int main() { Rect r; r.setLen(2); r.setWid(3); cout << r.getLen() << endl; cout << r.calcArea() << endl; return 0; } //main

Constructors class Rect { int len; int wid; public: Rect(int x, int y); //CONSTRUCTOR DECLARATION Rect(); //CONSTRUCTOR DECLARATION int calcArea(); void setLen(int x) { len = x; } void setWid(int x) { wid = x; } int getLen() { return(len); } int getWid() { return(wid); } }; Rect::Rect() { //CONSTRUCTOR ( no input parameters) len = 3; wid = 5; } Rect::Rect(int x, int y) { // CONSTRUCTOR (2 input parameters) len = x; wid = y; } int Rect::calcArea() { return len * wid; } int main() { Rect r(7,2); cout << r.getLen() << endl; cout << r.calcArea() << endl; return 0; } //main

Overloading methods… #include using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; pd.print(5); // Call print to print integer pd.print( ); // Call print to print float pd.print("Hello C++"); // Call print to print character return 0; }