1. Applications of 2 and 3 DimensionalArrays
a. Matrix transposition:
i. Matrix – two dimensional array of numbers
ii. Atransposition is a flip among the diagonal.
b. Swapping Two Numbers:
i. Two variables: x and y, and we want to exchange their values. Ex: x = 1, y
1. we need to store the original value in a temporary variable, so we
can assign this “temp” variable into the other variable without
losing it during initialization.
ii. transpose1.c (possible homework solution)
a. Modular Design:
1. Breaking overall task in to smaller modules, each constructed from
even smaller ones.
2. Construct each part individually.
3. Easier to debug, and gain access to smaller parts of code easily.
ii. Implemented using functions. It is a convenient way to encapsulate long
1. They accept parameters and return a value.
1. Consists of 3 parts:
1. Tells compiler what to expect. Consists of name, parameters, and
type of value returned.
2. return-type function-name(parameter-list);
1. Complete description of what function does. Consists of: a. Declaration
1. Computes the function
a. int square(int x);
b. int square(int x)
1. Functions terminate upon encountering any
“return” value, or bracket closing the
v. Function Parameters
1. Functions can have multiple parameters
2. Afunction can also have no parameters, as declared by void.
vi. Multiple Return Statements
1. Functions can have multiple possible return statements, such as in
an if-else statement, that gives the option of multiple return
statements, although only one is actually going to be returned.
vii. Void Functions 1. Some functions perform a task and return no value. These
functions are labeled with the term “void” upon declaration. It does
not return a value in a sense that beforehand, it returns something
that can be assigned to a variable.A“printf” statement, for
example, is not a value that can be returned.
a. void messages(int i)
viii. Order of Functions
1. Functions appear sequentially, not inside other functions.
2. The order in which functions appear when writing does not matter,
as long as they are called before the function itself appears.
3. If definition appears before call, a separate declaration is not
4. With small programs, you would want to define the functions first,
before the call, so you do not need declarations again.
5. With large programs, you want to declare functions first, call, then
define the function.
ix. Parameters are declared according to the location in the list, not by name.
1. Variables inside and outside function bear no relation to each other,
thus, there can be two variables with the same name
c. Revisiting: With Functions
1. Without functions, if you would want to take the Greatest Common
Denominator (gcd) of more than two numbers, you would have to
take the gcd twice. With functions, you would only need to declare
the function taking the gcd of two numbers once. You can take the
gcd of the first two numbers, and then take the gcd of that gcd of
the first tow numbers and the third number.
ii. nr_fun.c iii. pi_fun.c
a. What happens at Function Call?
i. double myfunction(double x, double y);
ii. z = myfunction(9.9*a, 2/lambda);
iii. Memory is allocated to the function parameters double x and
iv. Expressions 9.9*a and 2/lambda are evaluated, order unspecified.
1. These resulting values are assigned into variables x and y. Type
conversions performed as needed. This is known as “call by
b. What Happens at Function Return?
i. if the function contains the statement “return ‘expression’”, then
the function execution stops as soon as the first instance of such statement
1. the expression is evaluated, type converted if needed, and the value
is assigned to the variable z.
c. You Cannot Change the Calling Parameters
i. When f(x) calls a function f(int y), the value of x is copied into y.
1. f has no access to x, and f cannot change x. It can only change y.
ii. The function cannot change the values of the function that called it.
1. Look at the two quizzes in the lecture.
4. Array Parameters
a. Array Elements and Parameters
i. Both array elements and whole arrays can be parameters. ii. Ex: Replacing all array elements by their absolute value, then printing all
a. The function takes an integer and calls it with an element of
the array, abs_val(myArray[i]), with the function
returning an absolute value.
iii. With passing whole arrays as parameters, we want to pass the array name
and the length separately.
a. because functions do not know the length of the array, the
array name and the array length must be two separate
parameters. Hence, in “int main()” it calls using
myArray, and 5, the length of myArray.
b. In the absAll function, it uses “int length” as a
bound for “i”.
1. Element parameters in absAll to abs Val are declared as int, and
passed as array[i].
2. Array parameters (from main to absAll, and printAll) are declared
through int ar, and passed into myArray.
v. Unlike regular variables (int, etc.), array parameters can be changed by
b. When only declaring a function, the declaration does not require parameter
names. However, in order to show that a parameter is an array ty