Principles of Computer Science Lecture 4 Dr. Horia V. Corcalciuc Horia Hulubei National Institute for R&D in Physics and Nuclear Engineering (IFIN-HH) February 10, 2016
Pointers: Assignment Pointer Assignment in C# Safe Context double[] a = new double[2] { 3.14, 2.71 ; double[] b = new double[2]; b = a; a = null; Pointer Assignment in C# Unsafe Context double *a = stackalloc double[2]; double *b; a[0] = 3.14; a[1] = 2.71; b = &a[0]; In both safe and unsafe contexts, assigning an object to another object actually creates a reference instead of creating a duplicate of the object Lecture 3. This happens under the hood for both Java and C# although the syntax is not explicit as it is in C and C++. In order to truly create a copy of an array you will have to explicitly copy all the elements from one array to the other array or use the API to do it for you.
Pointers Pointers: Copying vs. Assignment Assign and Copy in Java double[] a = new double[2]; double[] b = new double[2]; b = a; a[0] = 3.14; a[1] = 2.71; System.out.println(b[0]); System.out.println(b[1]); By using the equals sign (=) to assign the array a to array b at the line reading b = a we are in fact creating a reference from b to a although the language does not carry an explicit syntax for pointers. If the first array a is modified in any way, then we can access the contents through b via the reference we have created. When we assigned a to b, the contents of the array b are lost and, without a garbage collector, the memory allocated with new would have leaked.
Multi-Dimensional Arrays Java Syntax double[][] f = new double[5][5]; C# Syntax double[,] f = new double[5, 5]; f (i, j) : {y i,j i, j 2 N, i 2 [0..l), j 2 [0..k) 7! 0 1 y 0,0 y 0,1 y 0,k y 1,0 y 1,1 y 1,k B C @ A y l,0 y l,1 y l 1,k 1 l,k y = f (i, j)
Arrays Multi-Dimensional Arrays: Accessing Java Syntax double[][] f = new double[][] { {1, 2, 0, {3, 4, 5 ; System.out.println(f[1][2]); C# Syntax double[,] f = new double[2, 3] { {1, 2, 0, {3, 4, 5 ; Console.WriteLine(f[1, 2]); y i,j = f (i, j) : {y i,j i, j 2 N, i 2 [0..2), j 2 [0, 3) 0 1 y 1,2 = f (1, 2) 7! @ 1 2 0 A 3 4 5 y 1,2 = 5 2,3
Arrays Multi-Dimensional Arrays: Example Listing Elements of Array in Row by Column Order in Java public class Print2Array { public static void main(string[] args) { int[][] a = new int[][] { {1, 2, 3, {4, 5, 6 ; int i = 0; int j = 0; while(i < 2) { while(j < 3) { System.out.println(a[i][j]); ++j; j = 0; ++i;
Introduction Addition Function in C# and Java public int f(int i, int j) { return i + j; (also called methods, subroutines or procedures) take as input zero or more parameters (or function arguments) ofagiventype and may return a value of a given type. The body of function is surrounded by braces and may operate on the parameters passed to the function. A function s parameters are usually a copy of the passed value and their lifetime extends to the end of the function body. In object-oriented programming functions are given a protection level, such as pubic, indicatingbywhomtheycanbeaccessed.
Parameters and Return A function can return nothing - in which case we use void keyword as the return type. Java Function Returning Nothing public void f(int j) { System.out.println(++j); Similarly, a function can take no parameters, in which case we leave a blank expression between the parenthesis. Java and C# Function Taking no Parameters public int f() { return 5;
Memory Disposition Function Call public static int[] f(int j) { int i = 0; int[] k = new int[1]; k[i] = j; return k; public static void main(string[] args) { int[] a = f(10); stack frame f stack frame main... k Heap: int[1] i = 0 j = 10 a local variables return address args[0], args[1],... parameters...
The Main Entry Point C# Pass by Reference public static void main(string[] args) { //... Whenever a program is run in C# or Java the main entry point main is searched and executed. Without a main function, the program would not know where to start executing. Amainentrypointisalwayspublic and static but may sometimes return an integer (int) insteadofnothing(void) inordertoindicateto the system whether the program has termianted successfully or not (conventionally, the main function returns 0 to indicate successful execution). In both C# and Java the main function takes as parameter an array of strings that contain any command line parameters - the first element (args[0]) isthefirstcommandlineparameter,thesecondelement (args[1]) isthesecondcommandlineparameter,etc...
C# ref and out Modifiers In case we want to operate on the variable that was passed to a function we can prefix the ref keyword to the type of the parameter. C# Pass by Reference public void f(ref int i) { ++i; can also store a result in a variable that will be initialised inside the function we can use the out keyword. C# Pass by Reference public void f(out int i) { ++i; When the ref or out keyword is used, the function operates on the variable that was passed to the function instead of a copy - as if we passed a low-level pointer to that variable to the function.
C# ref and out Modifiers: Di erences For ref, the following observations can be made: ref will tell the compiler that the variable passed to the function is already initialised before entering the function. Parameters passed using the ref modifier can be left untouched for the duration of the function. The value passed by ref is set and the function can read and modify it. For out, the following observations can be made: out indicates that the variable will be initialised inside the function. Parameters passed using the out modifier have to be assigned to before the function returns. The value passed by out is not set and you cannot read from it until it is set.
Recursion Definition of Recursion using Set Theory Given a set X and an element e 2 X, a function f : X 7! X there is an unique function F : N 7! X such that: F (0) = e F (n + 1) = f (F (n)) 8 n 2 N A function that calls itself by splitting a problem into smaller and smaller data sets is called a recursive function. A function that uses its return to create more and more of a data set is called a corecursive function.
Recursion: Proof Definition of Recursion using Set Theory Given a set X and an element e 2 X,afunctionf : X 7! X there is an unique function F : N 7! X such that: F (0) = e F (n + 1) = f (F (n)) 8 n 2 N Proof of Uniqueness Suppose we have two functions F : N 7! X and G : N 7! X such that F (0) =e, G (0) =e, F (n + 1) =f (F (n)), G (n + 1) =f (G (n)), then by induction: 1 for n = 0, F (0) =G (0) =e so the equation holds 2 for n 7! n + 1, F (n + 1) =G (n + 1) =f (F (n)) = f (G (n)) so the equations hold thus, inductively F (n) =G (n), 8n 2 N.
Recursion: Example Factorial Recurrence Relation n! = f (n) = ( 1 if n 0 n f (n 1) if n > 0 Recursively Compute Factorial in Java public class RecursiveFactorial { public static int fac(int i) { if(i == 0) return 1; return i * fac(i - 1); public static void main(string[] args) { int f = fac(5); System.out.println(f);
Recursion: Call Stack... stack frame f(0) 1 = f (0) local variables return address Stack pointer C# and Java Array Declaration and Assignment public static int fac(int i) { if(i == 0) return 1; return i * fac(i - 1); //... int f = fac(2); stack frame f(1) 1 = 1 1 stack frame f(2) 2 = 1 2 main parameters i = 0 local variables return address parameters i = 1 local variables return address parameters i = 2 f...
Lecture Material Mirrors Materials, such as the slides, the programs used, the exercises and the solutions to the exercises will be posted at: Personal Website