Principles of Computer Science

Similar documents
Principles of Computer Science

Contents. 8-1 Copyright (c) N. Afshartous

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

CSE 307: Principles of Programming Languages

CS558 Programming Languages

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Dynamic memory allocation (malloc)

CS 251 Intermediate Programming Java Basics

Programming Languages

Algorithms & Data Structures

CE221 Programming in C++ Part 1 Introduction

COE318 Lecture Notes Week 4 (Sept 26, 2011)

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

In Java we have the keyword null, which is the value of an uninitialized reference type

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

G Programming Languages - Fall 2012

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Creating Classes and Objects

NOTE: Answer ANY FOUR of the following 6 sections:

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CA31-1K DIS. Pointers. TA: You Lu

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CSE 230 Intermediate Programming in C and C++ Functions

CS 415 Midterm Exam Spring 2002

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

CS558 Programming Languages

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

Object Oriented Software Design II

Lecture 7: Binding Time and Storage

CSC 1600 Memory Layout for Unix Processes"

Memory management COSC346

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

What goes inside when you declare a variable?

Nested Loops. A loop can be nested inside another loop.

Object Oriented Programming. Java-Lecture 6 - Arrays

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Introduction to Programming (Java) 4/12

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS558 Programming Languages

Run-time Environment

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Run-time Environments - 3

Pointers, Arrays and Parameters

C11: Garbage Collection and Constructors

Perl Library Functions

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

4: Initialization & Cleanup. Guaranteed Initialization with the Constructor. Nuance

Full file at

Introduction to the Java Basics: Control Flow Statements

Variables and Java vs C++

APCS Semester #1 Final Exam Practice Problems

Variables, Memory and Pointers

AMCAT Procedure functions and scope Sample Questions

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Compiler Construction

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

CSCI 171 Chapter Outlines

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Pace University. Fundamental Concepts of CS121 1

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Fundamentals of Programming

Programming Languages

Dynamic Data Structures. CSCI 112: Programming in C

Chapter 6 Single-Dimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Object Oriented Modeling

CA341 - Comparative Programming Languages

CS 231 Data Structures and Algorithms, Fall 2016

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Lesson 13 - Vectors Dynamic Data Storage

COP4020 Programming Assignment 1 - Spring 2011

CS558 Programming Languages

Object-Oriented Programming for Scientific Computing

CS 161 Exam II Winter 2018 FORM 1

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

C++ Programming. Pointers and Memory Management. M1 Math Michail Lampis

Object-Oriented Principles and Practice / C++

G Programming Languages - Fall 2012

Introduction to Programming. Lecture 6: Functions & Program Structure

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Memory and C++ Pointers

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Week 3 Lecture 2. Types Constants and Variables

Transcription:

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