Algorithm Programming with C++
Algorithm Programming with C++
OUTLINE
Arrays
Pointers
Functions
Linear Algebra: Inverse of Matrix:
Adjoint Method
Gauss Elimination Method
Gauss-Jordan Elimination Method
Gauss-Siedel Iteration Method
LU Decomposition Method
MECHATRONICS STUDY PROGRAM
Arrays (1)
• Arrays
An array stores a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of
the same type.
A specific element in an array is accessed by an index: numbers[0],
numbers[1], and ..., numbers[99]
All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to
the last element.
Declaring arrays:
type arrayName [ arraySize ] ;
MECHATRONICS STUDY PROGRAM
Arrays (2)
• Arrays
Initializing Arrays
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
If you omit the size of the array, an array just big enough to hold
the initialization is created. Therefore, if you write:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
balance[4] = 50.0;
MECHATRONICS STUDY PROGRAM
Arrays (3)
MECHATRONICS STUDY PROGRAM
Arrays (4)
MECHATRONICS STUDY PROGRAM
Arrays (5)
Multidimensional Arrays
C++ allows multidimensional arrays. Here is the general form of a
multidimensional array declaration: type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional 5 . 10
. 4 integer array: int threedim[5][10][4];
Two-Dimensional Arrays: type arrayName [ x ][ y ]; A two-dimensional
array can be think as a table, which will have x number of rows and y
number of columns. A 2-dimensional array a, which contains three rows
and four columns can be shown as below:
MECHATRONICS STUDY PROGRAM
Arrays (5)
Multidimensional Arrays
Initializing Two-Dimensional Arrays:
OUTLINE
Arrays
Pointers
Functions
Linear Algebra: Inverse of Matrix:
Adjoint Method
Gauss Elimination Method
Gauss-Jordan Elimination Method
Gauss-Siedel Iteration Method
LU Decomposition Method
MECHATRONICS STUDY PROGRAM
Pointers (1)
• Pointers
Every variable is a memory location and every memory location
has its address defined which can be accessed using ampersand
(&) operator which denotes an address in memory.
A pointer is a variable whose value is the address of another
variable. Like any variable or constant, you must declare a pointer
before you can work with it.
The general form of a pointer variable declaration is:
type *var-name;
MECHATRONICS STUDY PROGRAM
Pointers (2)
Address of variable
can be accessed
through ampersand
(&) operator
MECHATRONICS STUDY PROGRAM
Pointers (3)
• Pointers
The actual data type of the value of all pointers, whether integer,
float, character, or otherwise, is the same, a long hexadecimal
number that represents a memory address. The only difference
between pointers of different data types is the data type of the
variable or constant that the pointer points to.
Important operations for Pointers:
1. to define a pointer variables
2. to assign the address of a variable to a pointer
3. to access the value at the address available in the pointer
variable
This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand. Following
example makes use of these operations:
MECHATRONICS STUDY PROGRAM
Pointers (3)
MECHATRONICS STUDY PROGRAM
Pointers (4)
• Pointers in details:
MECHATRONICS STUDY PROGRAM
Pointers (5)
• NULL pointers:
– A pointer that is assigned NULL is called a null pointer.
– If all unused pointers are given the null value and you avoid the
use of a null pointer, you can avoid the accidental misuse of an
uninitialized pointer. Many times, uninitialized variables hold some
junk values and it becomes difficult to debug the program.
MECHATRONICS STUDY PROGRAM
Pointers (7)
• Pointer arithmetic:
– pointer is an address which is a numeric value; therefore, you can
perform arithmetic operations on a pointer just as you can a
numeric value. There are four arithmetic operators that can be
used on pointers: ++, --, +, and -.
– Incrementing a Pointer:
MECHATRONICS STUDY PROGRAM
Pointers (8)
• Pointer arithmetic:
– Decrementing a Pointer:
MECHATRONICS STUDY PROGRAM
Pointers (9)
• Pointer arithmetic:
– Pointer Comparisons: pointers may be compared by using
relational operators, such as ==, <, and >. If p1 and p2 point to
variables that are related to each other, such as elements of the
same array, then p1 and p2 can be meaningfully compared.
MECHATRONICS STUDY PROGRAM
Pointers (10)
• Pointers vs arrays:
– Pointers and arrays are strongly related. In fact, pointers and
arrays are interchangeable in many cases. For example, a pointer
that points to the beginning of an array can access that array by
using either pointer arithmetic or array-style indexing.
MECHATRONICS STUDY PROGRAM
Pointers (11)
• Pointers vs arrays:
– However, pointers and arrays are not completely interchangeable.
– It is perfectly acceptable to apply the pointer operator * to var but it
is illegal to modify var value. The reason for this is that var is a
constant that points to the beginning of an array and can not be
used as l-value. Because an array name generates a pointer
constant, it can still be used in pointer-style expressions, as long as
it is not modified.
MECHATRONICS STUDY PROGRAM
Pointers (12)
• Array of pointers:
MECHATRONICS STUDY PROGRAM
Pointers (13)
• Array of pointers:
There may be a situation, when we want to maintain an array,
which can store pointers to an int or char or any other data type
available. Following is the declaration of an array of pointers to an
integer: This declares ptr as an array of MAX integer
pointers. Thus, each element in ptr, now holds a
pointer to an int value.
MECHATRONICS STUDY PROGRAM
Pointers (14)
• Array of pointers:
You can also use an array of pointers to character to store a list of
strings as follows:
MECHATRONICS STUDY PROGRAM
Pointers (15)
• Pointer to Pointer (Multiple Indirection):
A pointer to a pointer is a form of multiple indirection or a chain of
pointers. Normally, a pointer contains the address of a variable.
When we define a pointer to a pointer, the first pointer contains the
address of the second pointer, which points to the location that
contains the actual value.
Pointers (16)
• Pointer to Pointer (Multiple Indirection):
When a target value is indirectly pointed to by a pointer to a pointer,
accessing that value requires that the asterisk operator be applied
twice, as is shown below in the example:
MECHATRONICS STUDY PROGRAM
Pointers (15)
• Passing pointers to functions:
C++ allows you to pass a pointer to a function. To do so, simply
declare the function parameter as a pointer type. Following a
simple example where we pass an unsigned long pointer to a
function and change the value inside the function which reflects
back in the calling function:
MECHATRONICS STUDY PROGRAM
Pointers (16)
• Return pointer from functions:
The function which can accept a pointer, can also accept an array
as shown in the following example::
MECHATRONICS STUDY PROGRAM
OUTLINE
Arrays
Pointers
Functions
Linear Algebra: Inverse of Matrix:
Adjoint Method
Gauss Elimination Method
Gauss-Jordan Elimination Method
Gauss-Siedel Iteration Method
LU Decomposition Method
MECHATRONICS STUDY PROGRAM
Functions (1)
• A function is a group of statements that together perform a task. Every
C++ program has at least one function, which is main(), and all the
most trivial programs can define additional functions.
• You can divide up your code into separate functions. How you divide
up your code among different functions is up to you, but logically the
division usually is so each function performs a specific task.
• A function declaration tells the compiler about a function's name,
return type, and parameters. A function definition provides the actual
body of the function.
• The C++ standard library provides numerous built-in functions that
your program can call. For example, function strcat() to concatenate
two strings, function memcpy() to copy one memory location to another
location and many more functions.
• A function is knows as with various names like a method or a sub-
routine or a procedure etc.
MECHATRONICS STUDY PROGRAM
Functions (2)
• Defining a Function.
– The general form of a C++ function definition is as follows:
– A C++ function definition consists of a function header and a function body. Here
are all the parts of a function:
– Return Type: A function may return a value. The return type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return type is the keyword void.
– Function Name: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
– Parameters: A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain no
parameters.
– Function Body: The function body contains a collection of statements that define
what the function does.
MECHATRONICS STUDY PROGRAM
Functions (3)
• Example:.
– Following is the source code for a function called max(). This
function takes two parameters num1 and num2 and returns the
maximum between the two:
MECHATRONICS STUDY PROGRAM
Functions (4)
• Function Declarations:
– A function declaration tells the compiler about a function name and
how to call the function. The actual body of the function can be
defined separately.
– A function declaration has the following parts:
return_type function_name( parameter list );
– For the above defined function max(), following is the function
declaration:
int max(int num1, int num2);
– Parameter names are not importan in function declaration only their
type is required, so following is also valid declaration:
int max(int, int);
– Function declaration is required when you define a function in one
source file and you call that function in another file. In such case,
you should declare the function at the top of the file calling the
function.
MECHATRONICS STUDY PROGRAM
Functions (5)
• Calling a Function:
– When a program calls a function, program control is transferred to the called function.
A called function performs defined task and when its return statement is executed or
when its function-ending closing brace is reached, it returns program control back to
the main program.
– To call a function, you simply need to pass the required parameters along with
function name, and if function returns a value, then you can store returned value. For
example:
MECHATRONICS STUDY PROGRAM
Functions (6)
• Function Arguments:
– If a function is to use arguments, it must declare variables that accept the values of
the arguments. These variables are called the formal parameters of the function.
– The formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
– While calling a function, there are two ways that arguments can be passed to a
function:
MECHATRONICS STUDY PROGRAM
Functions (7)
• Default Values for Parameters:
– When you define a function, you can specify a default
value for each of the last parameters. This value will be
used if the corresponding argument is left blank when
calling to the function.
– This is done by using the assignment operator and
assigning values for the arguments in the function
definition. If a value for that parameter is not passed
when the function is called, the default given value is
used, but if a value is specified, this default value is
ignored and the passed value is used instead. Consider
the following example:
MECHATRONICS STUDY PROGRAM
Functions (8)
MECHATRONICS STUDY PROGRAM
Functions (9)
• 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 of the function. In this case, changes
made to the parameter inside the function have no effect
on the argument.
Functions (10)
MECHATRONICS STUDY PROGRAM
Functions (11)
• Function call by reference:
– The call by reference method of passing arguments to a
function copies the reference of an argument into the
formal parameter. Inside the function, the reference is
used to access the actual argument used in the call. This
means that changes made to the parameter affect the
passed argument.
– To pass the value by reference, argument reference is
passed to the functions just like any other value. So
accordingly you need to declare the function parameters
as reference types as in the following function swap(),
which exchanges the values of the two integer variables
pointed to by its arguments.
MECHATRONICS STUDY PROGRAM
Functions (12)
MECHATRONICS STUDY PROGRAM
Functions (13)
• Function call by pointer:
– The call by pointer method of passing arguments to a
function copies the address of an argument into the
formal parameter. Inside the function, the address is
used to access the actual argument used in the call. This
means that changes made to the parameter affect the
passed argument.
– To pass the value by pointer, argument pointers are
passed to the functions just like any other value. So
accordingly you need to declare the function parameters
as pointer types as in the following function swap(),
which exchanges the values of the two integer variables
pointed to by its arguments.
MECHATRONICS STUDY PROGRAM
Functions (14)
MECHATRONICS STUDY PROGRAM
OUTLINE
Arrays
Pointers
Functions
Linear Algebra: Inverse of Matrix:
Adjoint Method
Gauss Elimination Method
Gauss-Jordan Elimination Method
Gauss-Siedel Iteration Method
LU Decomposition Method
Matrix Invers
MECHATRONICS STUDY PROGRAM
a 22 a 23 a a 23 a a 22
det(A) a11. a12 . 21 a13 . 21
a 32 a 33 a 31 a 33 a 31 a 32
MECHATRONICS STUDY PROGRAM
Example:
Calculate the value of x1, x2 and x3 from the linear system below :
invers:
x1 + 0,5x2 = 100
2x1 + x2 + x3 = 200 cij = (-1) . M ij
0,5x1 + 0,5x2 + x3 = 100 c11 = 1(1,0 - 0,5) = 0,5
c12 = -1(2,0 - 0,5) = -1,5
1,0 0,5 0 x1 100 c13 = 1(1,0 - 0,5) = 0,5
2,0 1,0 1,0 x 200
2 c21 = -1(0,5 – 0) = -0,5
0,5 0,5 1,0 x3 100 1,0 0,5 0 c22 = 1(1,0 - 0) = 1,0
2,0 1,0 1,0 a c23 = -1(0,5 – 0,25) = -0,25
ij
Determinant: 0,5 0,5 1,0 c31 = 1(0,5 – 0) = 0,5
c32 = -1(1,0 – 0) = -1,0
1,0 1,0 2,0 1,0
aij 1,0 0,5 0 c33 = 1(1,0 – 1,0) =0
0,5 1,0 0,5 1,0
0,5 - 1,5 0,5
= 1,0 (1,0–0,5) – 0,5(2,0–0,5) + 0 cij - 0,5 1,0 - 0,25
= 0,25 0,5 - 1,0 0
MECHATRONICS STUDY PROGRAM
x j aij1 .bi
0,5 - 0,5 0,5
- 1,5 1,0 1,0
2,0 2,0 - 2,0 100
0,5 - 0,25 0
x j 6,0 - 4,0 4,0 200
0,5 - 0,5 0,5 - 2,0 1,0 0 100
1
aij1 - 1,5 1,0 1,0
0,25 x1 0
0,5 - 0,25 0 x 200
2
2,0 2,0 - 2,0 x3 0
6,0 - 4,0 4,0
- 2,0 1,0 0 So : x1 = 0; x2 = 200,
And x3 = 0
Matrix Invers
MECHATRONICS STUDY PROGRAM
a 11 a 12 a 13 x b1
A a 21 a 22 a 23 y b 2
a 31 a 32 a 33 z b3
Matrix Invers
MECHATRONICS STUDY PROGRAM
L1 a 11 a 12 a 13 x b1
L 2' 0 a 22' a 23' y b 2' L3’’ = L3’ – a32’/a22’.L2’
L 3' 0 a 32' a 33' z b 3'
L1 a 11 a 12 a 13 x b1 z = b3’ /a33’
L 2' 0 a 22' a 23' y b 2' y = (b2’ - a23’).b/(a22. a33’)
L 3' 0 0 a 33' z b 3'
x = (b1 + a12.y - a13.z)
MECHATRONICS STUDY PROGRAM
Matrix Invers
MECHATRONICS STUDY PROGRAM
Gauss-JordanElimination (1)
• Gauss-Jordan elimination is an algorithm for solving systems
of linear equations that developed from Gauss elimination
• To perform row reduction on a matrix, one uses a sequence
of elementary row operations to modify the matrix until lower
and upper triangularof the matrix is filled with zeros.
a 11 a 12 a 13 x b1
A a 21 a 22 a 23 y b 2
a 31 a 32 a 33 z b3
Matrix Invers
MECHATRONICS STUDY PROGRAM
L1 a 11 a 12 a 13 x b1
L 2' 0 a 22' a 23' y b 2' L3’’ = L3’ – a32’/a22’.L2’
L 3' 0 a 32' a 33' z b 3'
procedure:
Firstly unknown value are given value zero
The result of calculation are used for the next calculation.
1st iteration
By assuming y and z = zero, then x can be determined:
LU Decomposition (1)
• If Matrix A is a non singulat matrix (matrix that has invers)
then Matrix A can be factorize to become two different
triangular matrix (upper and lower).
• Those changes can be illustrated below:
A
Elementer U
transformation to row L
Matrix Invers
MECHATRONICS STUDY PROGRAM
LU Decomposition (2)
• Pada matriks segitiga bawah, L, semua elemen diagonal utamanya
berharga 1, sedangkan pada matriks segitiga atas, U tidak ada aturan
khusus pada elemen diagonal utamanya. Setelah pemfaktoran matriks A
menjadi matriks L dan matriks U, maka kedua matriks tersebut dapat
digunakan untuk menyelesaikan sistem persamaan linier AX = B, yaitu
sebagai berikut. Tinjau SPL AX = B, kemudian faktorkan A menjadi L dan
U, sehingga A = LU, sehingga LUX = B. Misalkan UX = y, maka Ly = B.
Untuk memperoleh , kita gunakan teknik substitusi maju ( forward
substitution ), sbb,
Matrix Invers
MECHATRONICS STUDY PROGRAM
LU Decomposition (2)
• Pada matriks segitiga bawah, L, semua elemen diagonal utamanya
berharga 1, sedangkan pada matriks segitiga atas, U tidak ada aturan
khusus pada elemen diagonal utamanya. Setelah pemfaktoran matriks A
menjadi matriks L dan matriks U, maka kedua matriks tersebut dapat
digunakan untuk menyelesaikan sistem persamaan linier AX = B, yaitu
sebagai berikut. Tinjau SPL AX = B, kemudian faktorkan A menjadi L dan
U, sehingga A = LU, sehingga LUX = B. Misalkan UX = y, maka Ly = B.
Untuk memperoleh , kita gunakan teknik substitusi maju ( forward
substitution ), sbb,
Matrix Invers
MECHATRONICS STUDY PROGRAM
LU Decomposition (2)
• Example:
Matrix Invers
MECHATRONICS STUDY PROGRAM
LU Decomposition (2)
• Example:
MECHATRONICS STUDY PROGRAM
3 1 2
0 1 4
2 2 1
MECHATRONICS STUDY PROGRAM