C++ For Science and Engineering Lecture 15

Similar documents
C++ For Science and Engineering Lecture 2

C++ For Science and Engineering Lecture 12

CSc Introduction to Computing

CSC 211 Intermediate Programming. Arrays & Pointers

pointers + memory double x; string a; int x; main overhead int y; main overhead

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

C++ For Science and Engineering Lecture 27

Exercise 1.1 Hello world

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

4. C++ functions. 1. Library Function 2. User-defined Function

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

C++ Structures Programming Workshop 2 (CSCI 1061U)

Object-Oriented Principles and Practice / C++

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

CS2141 Software Development using C/C++ C++ Basics

by Pearson Education, Inc. All Rights Reserved.

IT 1033: Fundamentals of Programming Data types & variables

LECTURE 02 INTRODUCTION TO C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Getting started with C++ (Part 2)

Lecture 2 Tao Wang 1

BITG 1113: Function (Part 2) LECTURE 5

CPSC 427: Object-Oriented Programming

Week 3: Pointers (Part 2)

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Understand Execution of a Program

Integer Data Types. Data Type. Data Types. int, short int, long int

Data Types & Variables

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Chapter 2. Procedural Programming

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Programming in C++: Assignment Week 8

Lecture 23: Pointer Arithmetic

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

QUIZ. What are 3 differences between C and C++ const variables?

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CMSC 202 Midterm Exam 1 Fall 2015

CPSC 427: Object-Oriented Programming

Implementing an ADT with a Class

CS 376b Computer Vision

Goals of this Lecture

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

C++ Casts and Run-Time Type Identification

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

6.096 Introduction to C++ January (IAP) 2009

W3101: Programming Languages C++ Ramana Isukapalli

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Fundamentals of Programming. Lecture 19 Hamed Rasifard

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Homework Assignment #2 (revised)

Computer Programming

Fast Introduction to Object Oriented Programming and C++

Objectives. In this chapter, you will:

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Introduction to C++ Systems Programming

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Review of Important Topics in CS1600. Functions Arrays C-strings

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Functions and Recursion

Chapter 2: Basic Elements of C++

C++ Quick Guide. Advertisements

Advanced Systems Programming

Lecture 7. Log into Linux New documents posted to course webpage

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CSCE 110 PROGRAMMING FUNDAMENTALS

COEN244: Class & function templates

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

EE 355 Lab 4 - Party Like A Char Star

Functions BCA-105. Few Facts About Functions:

Array Elements as Function Parameters

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Lab Instructor : Jean Lai

CSCE 110 PROGRAMMING FUNDAMENTALS

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Object Reference and Memory Allocation. Questions:

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

EL2310 Scientific Programming

Function Overloading

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Programming: From Problem Analysis to Program Design, Third Edition

Fundamentals of Programming Session 20

CS 261 Data Structures. Introduction to C Programming

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

A brief introduction to C++

Lesson 13 - Vectors Dynamic Data Storage

Midterm Review. PIC 10B Spring 2018

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

UNIT- 3 Introduction to C++

CE221 Programming in C++ Part 1 Introduction

Chapter Two MULTIPLE CHOICE

CPSC 427a: Object-Oriented Programming

Transcription:

C++ For Science and Engineering Lecture 15 John Chrispell Tulane University Wednesday September 29, 2010

Function Review Recall the basics you already know about functions. Provide a function definition. Provide a function prototype. Provide a call to the function. Sometimes aspects of this are done for you (using a standard library or package). Other times we do them ourself. #include <iostream > void s i m p l e ( ) ; i n t main ( ) { s i m p l e ( ) ; void s i m p l e ( ) { s t d : : cout << I m but a s i m p l e f u n c t i o n. << s t d : : e n d l ; John Chrispell, Wednesday September 29, 2010 slide 3/23

More on functions. Functions can be broken into two groups: Functions that don t return values: void type. Functions that do return values: non void type. The goal of a function is to preform some simple task. A function that prints Go tigers! an integer number of times: void s i m p l e ( i n t n ){ f o r ( i n t i = 0 ; i < n ; i ++){ s t d : : cout << Go t i g e r s! << s t d : : e n d l ; // Has no r e t u r n s t a t e m e n t. John Chrispell, Wednesday September 29, 2010 slide 5/23

More on functions. Functions that don t return values: void type. void functionname ( P a r a m e t e r L i s t ){ s t atement ( s ) ; return ; // O p t i o n a l Functions that do return values: non void type. typename functionname ( P a r a m e t e r L i s t ){ s t atement ( s ) ; return v a l u e ; // v a l u e i s type c a s t // to type typename. Note if the function is of type double and we return an int. The value is type cast as a double. John Chrispell, Wednesday September 29, 2010 slide 7/23

Return Restrictions C++ can not return an array directly. You may return: basic types (double, int, char... ) structures pointers Note you may return an array that is part of a structure or object, even if you can t return the array directly. We also make note that a function terminates when it executes a return statement. i n t b i g g e r ( i n t a, i n t b ){ i f ( a > b ){ return a ; // t e r m i n a t e s f u n c t i o n e l s e { return b ; // t e r m i n a t e s f u n c t i o n. John Chrispell, Wednesday September 29, 2010 slide 9/23

Prototypes What does the prototype do? Prototypes describe the function interface to the compiler. Tells the compiler number and type of function arguments. Tells the compiler what type of value is returned. Prototyping makes compiling of large programs that span many files easier. Typically we place prototypes in header files so different parts of the code can access them. All functions may not live in the same file. The easy way to get a prototype is to copy the function header and add a semicolon to it. double cube ( double x ) ; double cube ( double ) ; // We can drop v a r i a b l e names. John Chrispell, Wednesday September 29, 2010 slide 11/23

Prototypes Note if you do use variable names in the prototype the do not nee to match the function definition. In classic C function prototyping is optional. In C++ prototyping is manditory. What prototypes do for you! Make the compiler correctly handle the function return value. The compiler checks that you use the correct number of arguments. The compiler checks that the arguments used are of correct type. If possible it converts argument to the correct type. John Chrispell, Wednesday September 29, 2010 slide 13/23

Passing by value C++ normally passes arguments by value. That means the numeric value of the argument is passed to the function, where it is assigned to a new variable. The values of variables are thus insulated inside main from the actions inside the function. A variable that is used to receive passed values is called a formal argument or formal parameter. Variables and parameters declared within a function are private to the function. When the function is called the computer allocates memory and frees the memory for the variables when the function terminates. Variables that are treated in this manner are automatic variables as they are automatically created and destroyed during program execution. John Chrispell, Wednesday September 29, 2010 slide 15/23

lotto.cpp #include <iostream > long double p r o b a b i l i t y ( unsigned numbers, unsigned p i c k s ) ; i n t main ( ) { using namespace std ; double total, choices ; cout << Enter the t o t a l number o f c h o i c e s on the game card and\n the number of p i c k s allowed : \ n ; while ( ( c i n >> t o t a l >> c h o i c e s ) && c h o i c e s <= t o t a l ){ cout << You have one chance i n ; cout << p r o b a b i l i t y ( t o t a l, c h o i c e s ) ; // compute the odds cout << of winning. \ n ; cout << Next two numbers (q to quit ) : ; cout << Done \n ; return 0 ; / ================================================================== / / The f o l l o w i n g f u n c t i o n c a l c u l a t e s the p r o b a b i l i t y o f p i c k i n g p i c k s / / numbers c o r r e c t l y from numbers choices. / / ================================================================== / long double p r o b a b i l i t y ( unsigned numbers, unsigned p i c k s ){ long double r e s u l t = 1. 0 ; // h e r e come some l o c a l v a r i a b l e s long double n ; unsigned p ; f o r ( n = numbers, p = picks ; p > 0 ; n, p ){ r e s u l t = r e s u l t n / p ; return r e s u l t ; John Chrispell, Wednesday September 29, 2010 slide 17/23

arrfun1.cpp #i n c l u d e <iostream > const i n t A r S i z e = 8 ; i n t sum arr1 ( i n t a r r [ ], i n t n ) ; // p r o t o t y p e i n t sum arr2 ( i n t arr, i n t n ) ; // prototype i n t main ( ) { using namespace std ; i n t c o o k i e s [ A r S i z e ] = { 1, 2, 4, 8, 1 6, 3 2, 6 4, 1 2 8 ; i n t sum = 0 ; / some systems r e q u i r e p r e c e d i n g i n t with s t a t i c to / / e n a b l e a r r a y i n i t i a l i z a t i o n / sum = sum arr1 ( cookies, ArSize ) ; cout << Total cookies eaten (1): << sum << \n ; sum = sum arr2 ( cookies, ArSize ) ; cout << Total cookies eaten (2): << sum << \n ; return 0 ; / r e t u r n the sum o f an i n t e g e r a r r a y / i n t sum arr1 ( i n t arr [ ], i n t n ){ i n t t o t a l = 0 ; f o r ( i n t i = 0 ; i < n ; i ++) t o t a l = t o t a l + a r r [ i ] ; return t o t a l ; / a second method means the same / i n t sum arr2 ( i n t arr, i n t n ){ i n t t o t a l = 0 ; f o r ( i n t i = 0 ; i < n ; i ++) t o t a l = t o t a l + a r r [ i ] ; return t o t a l ; John Chrispell, Wednesday September 29, 2010 slide 19/23

arrfun1.cpp Notes on the previous listing: The name of an array is the same as if it were a pointer. c o o k i e s == &c o o k i e s [ 0 ] // a r r a y name i s a d d r e s s o f f i r s t element. There are two exceptions to this rule. 1 Array declarations use the array name to lable the storage. 2 Applying sizeof to an array name yields the size of the whole array in bytes. The notations i n t a r r and i n t a r r [ ] // a r e o n l y synonymous i n the f u n c t i o n header and p r o t o t y p e. we can not use in arr[] to declare a pointer in the body of a function. RECALL a r r [ i ] == ( a r r + i ) // v a l u e s i n two n o t a t i o n s &a r r [ i ] == a r r + i // a d d r e s s e s i n two n o t a t i o n s John Chrispell, Wednesday September 29, 2010 slide 21/23

arrfun2.cpp #include <iostream > const i n t A r S i z e = 8 ; i n t sum arr ( i n t a r r [ ], i n t n ) ; // use s t d : : i n s t e a d o f u s i n g d i r e c t i v e i n t main ( ) { i n t c o o k i e s [ A r S i z e ] = { 1, 2, 4, 8, 1 6, 3 2, 6 4, 1 2 8 ; std : : cout << cookies << = array address, ; s t d : : cout << s i z e o f c o o k i e s << = s i z e o f c o o k i e s \n ; i n t sum = sum arr ( cookies, ArSize ) ; s t d : : cout << Total c o o k i e s e a t e n : << sum << s t d : : e n d l ; sum = sum arr ( c o o k i e s, 3 ) ; // a l i e s t d : : cout << F i r s t t h r e e e a t e r s a t e << sum << c o o k i e s. \ n ; sum = sum arr ( c o o k i e s + 4, 4 ) ; // a n o t h e r l i e std : : cout << Last f o u r e a t e r s ate << sum << c o o k i e s. \ n ; return 0 ; / ==================================== / / r e t u r n the sum o f an i n t e g e r a r r a y / / ==================================== / i n t sum arr ( i n t a r r [ ], i n t n ){ i n t t o t a l = 0 ; s t d : : cout << a r r << = a r r, ; s t d : : cout << s i z e o f a r r << = s i z e o f a r r \n ; f o r ( i n t i = 0 ; i < n ; i ++){ t o t a l = t o t a l + a r r [ i ] ; return t o t a l ; John Chrispell, Wednesday September 29, 2010 slide 23/23