Lecture 2
Lecture 2
Ref.: International Edition, Malik, C++ Programming: Program Design Including Data Structures, 6th edition, CENGAGE Learning
Functions
Functions - aka modules
ceil(56.34) 57.0 ex
fabs(x)
floor(x)
|x|
floor(45.67) = 45.0
tolower(x)
toupper(x)
tolower(H) = h
toupper(h) = H
User-Defined Functions
Value-returning functions have a return type Void functions do not have a return type
Value-Returning Functions
e.g.
heading or function header
int abs(int number) { if (number < 0) a parameter body number = -number; return number; } All of the above is called the functions definition.
Arrays
simple data type a variable can only store 1 value structured data type a variable can store several values e.g. input 5 integer test scores called test1, test2, , test5. Output the average and output those test scores that are less than the average
Arrays
int test1, test2, cin >> test1 >> test2 >> average = (test1 + test2 + if (test1 < average) if (test2 < average) This is clumsy!
Arrays
Array: A collection of a fixed number of components, all of the same type. 1-dimensional array: a list declaration: dataType arrayName [intExp]; e.g. int num[5] The components are num*0+, num*1+, num[4]
arrayName [indexExp]
called index
an integer 0