Algorithms, Data Structures, and Problem Solving

Similar documents
COMP s1 Lecture 1

Work relative to other classes

Problem Solving and 'C' Programming

Data Types. Data Types. Integer Types. Signed Integers

Introduction to C Language

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

CSE 303: Concepts and Tools for Software Development

Problem Solving and 'C' Programming

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

LESSON 4. The DATA TYPE char

Topics. CS429: Computer Organization and Architecture. File Inclusion. A Simple C Program. Intro to C

CS240: Programming in C

EL2310 Scientific Programming

High Performance Computing MPI and C-Language Seminars 2009

Procedures, Parameters, Values and Variables. Steven R. Bagley

Welcome to... CS113: Introduction to C

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

EECS2031 Software Tools

CS429: Computer Organization and Architecture

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Room 3P16 Telephone: extension ~irjohnson/uqc146s1.html

CS3157: Advanced Programming. Outline

Programming in C. What is C?... What is C?

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Lecture 03 Bits, Bytes and Data Types

COMP327 Mobile Computing Session:

Lecture 3. More About C

EL2310 Scientific Programming

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

F28HS2 Hardware-Software Interface. Lecture 1: Programming in C 1

Course Outline Introduction to C-Programming

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

PRINCIPLES OF OPERATING SYSTEMS

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Introduction Slide 1/20. Introduction. Fall Semester. Parallel Computing

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Informatica e Sistemi in Tempo Reale

Today s Learning Objectives

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Fundamentals of Programming

Computers Programming Course 5. Iulian Năstac

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Course Information and Introduction

C programming basics T3-1 -

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CS Programming In C

The type of all data used in a C++ program must be specified

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

CS 61c: Great Ideas in Computer Architecture

Programming in C and C++

COMP1917: Computing 1 1. Introduction

Intermediate Programming, Spring 2017*

COMP1917: Computing 1 1. Introduction

Pointers cause EVERYBODY problems at some time or another. char x[10] or char y[8][10] or char z[9][9][9] etc.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Variables and literals

Dynamic Memory Allocation and Command-line Arguments

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

ECEN 449 Microprocessor System Design. Review of C Programming

Lecture 3: C Programm

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CS 64 Week 0 Lecture 1. Kyle Dewey

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

HW1 due Monday by 9:30am Assignment online, submission details to come

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

COSC2031 Software Tools Introduction to C

(T) x. Casts. A cast converts the value held in variable x to type T

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS 241 Data Organization. August 21, 2018

Kurt Schmidt. October 30, 2018

Presented By : Gaurav Juneja

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

Arithmetic type issues

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS 61c: Great Ideas in Computer Architecture

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

EC 413 Computer Organization

COMP322 - Introduction to C++ Lecture 01 - Introduction

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CS240: Programming in C. Lecture 2: Overview

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

C++ for Java Programmers

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

C-programming. Goal To give basic knowledge of the C language. Some previous experiences in programming are assumed. Litterature

Tail recursion. Decision. Assignment. Iteration

Transcription:

Algorithms, Data Structures, and Problem Solving Masoumeh Taromirad Hamlstad University DT4002, Fall 2016

Course Objectives A course on algorithms, data structures, and problem solving Learn about algorithm complexity, algorithm design, and classical data structures Improve programming abilities in a programming language (we use C).

Intended Learning Outcomes Knowledge and Understanding recognize data structures and algorithms for search and sorting, such as quick sort, binary search trees, hash tables recognize techniques for algorithm design such as divide and conquer, recursion, dynamic programming explain how to estimate the execution time of programs Skills and Ability identify the need and use data structures as modules to solve larger problems use techniques for algorithm design in solving larger problems Judgement and Approach judge how suitable a program is given its execution time choose adequate implementations of data structures from program libraries

Teaching Format 1. Lecture Sessions: 2-hour sessions per week main lecture by me guest lecturer in-class activity or quiz 15-minute BREAK! 2. Lab Sessions: 2-hour sessions per week organised by Süleyman Savas computer-based exercises

Assessment 1. Project at the end of the term project result written report 2. Examination written examination of theory

Course Materials Textbooks [K+R] Kernighan, Brian W., Ritchie, Dennis M. The C Programming Language. Prentice Hall, 1989. [L] Loudon, Kyle. Mastering Algorithms with C. OReilly & Associates, 1999. Sedgewick. Algorithms in C, Parts 1-4, Third Edition. ISBN 0-201-31452-5. [recommended]

Course Web Page Lecture handouts and slides are available at http://ceres.hh.se/mediawiki/da 4002 2016 Check the page for updates and announcements! Any Question?

Algorithms recurring solutions to recurring problems recipes for solving new problems understanding trade-offs to make informed choices in specific situations

Objectives Study algorithms procedure for solving a problem finite number of steps involves repetition of an operation Objectives lay foundations reusable software development in any language (we ll use C) master daily programming handle trickier tasks Let s start! C Tutorial ( slides form previous years)

C Tutorial [K+R] chapter 1 (skip section 1.10)

Hello, World #include <stdio.h> int main (int argc, char **argv) { printf ( hello, world\n ); return 0;

Hello, World import standard I/O functions #include <stdio.h> every program has a main function int main (int argc, char **argv) { printf ( hello, world\n ); return 0; call a function to print a string return an exit code

C Programming Workflow design, edit eliminate source file errors compile test, debug, optimize executable run output input

C Programming Workflow gedit hello.c & design, edit gcc -Wall -o hello hello.c hello.c compile./hello hello run hello, world (none)

Variables, Operators, Flow /* print Fahrenheit to Celsius table */ int main (int argc, char **argv) { int fahr, cels; fahr = 0; while (fahr <= 300) { cels = 5 * (fahr - 32) / 9; printf ( %d\t%d\n, fahr, cels); fahr = fahr + 20; return 0;

Variables, Operators, Flow variables comment /* print Fahrenheit to Celsius table */ int main (int argc, char **argv) { int fahr, cels; fahr = 0; while (fahr <= 300) { cels = 5 * (fahr - 32) / 9; printf ( %d\t%d\n, fahr, cels); fahr = fahr + 20; return 0; while loop

Variables, Operators, Flow /* print Fahrenheit to Celsius table */ int main (int argc, char **argv) { type name int fahr, cels; fahr = 0; loop condition while (fahr <= 300) { cels = 5 * (fahr - 32) / 9; printf ( %d\t%d\n, fahr, cels); fahr = fahr + 20; return 0; loop body

Variables, Operators, Flow /* print Fahrenheit to Celsius table */ int main (int argc, char **argv) { assign int fahr, cels; fahr = 0; while (fahr <= 300) { cels = 5 * (fahr - 32) / 9; printf ( %d\t%d\n, fahr, cels); multiply fahr = fahr + 20; return 0; compare add subtract divide

Types int an_integer; short a_small_int; long a_big_int; char a_single_character; float almost_a_real_number; double more_precise; unsigned int a_positive_int; unsigned short a_small_positive_int; unsigned long a_big_positive_int; unsigned char an_unsigned_char; /* no unsigned float or double */

More precise arithmetic int main (int argc, char **argv) { int fahr, cels; fahr = 0; while (fahr <= 300) { cels = 5 * (fahr - 32) / 9; printf ( %d\t%d\n, fahr, cels); fahr = fahr + 20; return 0; truncated integer division

More precise arithmetic int main (int argc, char **argv) { double fahr, cels; fahr = 0.0; while (fahr <= 300.0) { precise floating-point division cels = 5.0 * (fahr - 32.0) / 9.0; printf ( %f\t%f\n, fahr, cels); fahr = fahr + 20.0; with int with double return 0; 60 15 60.00 15.555 80 26 80.00 26.667 100 37 100.00 37.778

There s a more compact way... int main (int argc, char **argv) { double fahr, cels; fahr = 0.0; while (fahr <= 300.0) { cels = 5.0 * (fahr - 32.0) / 9.0; printf ( %f\t%f\n, fahr, cels); fahr = fahr + 20.0; return 0;

There s a more compact way... int main (int argc, char **argv) { double fahr; for (fahr = 0; fahr <= 300; fahr += 20) printf ( %f\t%f\n, fahr, 5*(fahr-32)/9); return 0;

There s a more compact way... int main (int argc, char **argv) { also: -= *= /=... double fahr; for (fahr = 0; fahr <= 300; fahr += 20) printf ( %f\t%f\n, fahr, 5*(fahr-32)/9); return 0; convenient for-loop syntax converted to floating-point convenient update operators no need for explicit cels variable single-statement bodies don t need { automatic type conversion (but be careful...)

Character Input & Output int c; unsigned long n; n = 0; c = getchar(); while (EOF!= c) { putchar (c); ++n; c = getchar(); printf ( \ntotal: %lu\n, n);

More Compact Character I/O int c; unsigned long n = 0; combined initialization and declaration while (EOF!= (c = getchar())) { putchar (c); ++n; printf ( \ntotal: %lu\n, n); the value of an assignment is the value of its left-hand side

More Compact Character I/O int c; unsigned long n = 0; while (EOF!= (c = getchar())) { putchar (c); ++n; printf ( \ntotal: %lu\n, n);

Counting Lines int c; unsigned long n = 0; while (EOF!= (c = getchar())) { putchar (c); if ('\n' == c) ++n; printf ("\n%lu lines\n", n);

Counting Words #define IN 1 #define OUT 0 int c, state = OUT; unsigned long n = 0; while (EOF!= (c = getchar())) { putchar (c); if (ispunct(c) isspace(c)) state = OUT; else if (state == OUT) { state = IN; ++n; printf ("\n%lu words\n", n);

Counting Words #define IN 1 #define OUT 0 compile-time constants int c, state = OUT; unsigned long n = 0; while (EOF!= (c = getchar())) { putchar (c); if (ispunct(c) isspace(c)) state = OUT; else if (state == OUT) { state = IN; ++n; printf ("\n%lu words\n", n); from <ctype.h>

Counting Words Finite State Machine START transitions at beginnings and endings of words c = getchar() yes no ispunct(c) isspace(c) ++n EOF == c yes no c = getchar() no yes print n OUT state yes EOF == c ispunct(c) isspace(c) no END IN state

Arrays idx num[idx] 0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 int idx; int num[10]; for (idx = 0; idx < 10; ++idx) num[idx] = idx * idx; block of N identically typed values in declarations: [] reserves memory for N values in expressions: [] accesses individual values the first element is at index zero the last element is at index N-1

Counting Digits int c; unsigned long n[10]; memset (n, 0, sizeof(n)); while (EOF!= (c = getchar())) { putchar (c); if ('0' <= c && '9' >= c) ++n[c - '0']; printf ("\ndigits:\n"); for (c = 0; c < 10; ++c) printf (" %d:\t%lu\n", c, n[c]);

digit 0 1 2 3 4 5 6 7 8 9 ASCII code 48 49 50 51 52 53 54 55 56 57 from <string.h> int c; unsigned long n[10]; memset (n, 0, sizeof(n)); while (EOF!= (c = getchar())) { putchar (c); if ('0' <= c && '9' >= c) ++n[c - '0']; printf ("\ndigits:\n"); for (c = 0; c < 10; ++c) array length in bytes this trick works only for ASCII printf (" %d:\t%lu\n", c, n[c]);

Functions make it possible to focus on what is done, without worrying about how it is done two steps to create a C function 1. declaration: give it a name describe what it depends on describe what it produces 2. definition: write down the sequence of statements that implement it these two steps can be combined into one

Functions int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0;

Functions int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; combined declaration + definition function calls int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0;

Functions int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; signature body int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0;

Functions function name argument list type name return type local variables return statement int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p;

p = p * base; return p; Function Calls int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0; after call: place return value from function into caller before call: copy arguments from caller into function int power (int base, int n) {

Call by Value int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0;

Call by Value int power (int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; the function gets its own fresh copy of each argument! int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0;

Call by Value int power (int base, int n) { int p; for (p = 1; n > 0 ; --n) p = p * base; return p; modifying an argument in here has no outside effect! int main (int argc, char ** argv) { int i; for (i = 0; i < 10; ++i) printf( %d %d, power(2, i), power(-3, i)); return 0;

Call by Value int power (int base, int n) { int p; for (p = 1; n > 0 ; --n) p = p * base; return p; function arguments are local variables that get initialized by the caller int main (int argc, char ** argv) { int i; we ve for been (i = using 0; i functions < 10; ++i) all this time already! printf( %d %d, power(2, i), power(-3, i)); return 0;

...almost done... strings and a first look at pointers

Strings remember arrays? block of N identically typed values strings are arrays of characters strings always end with a zero so we can use them without knowing their size beforehand

Strings idx char[idx] ASCII for example: itads2013 0 i 105 remember arrays? block of N identically typed values strings are arrays of characters strings always end with a zero so we can use them without knowing their size beforehand 1 t 116 2 a 97 3 d 100 4 s 115 5 2 50 6 0 48 7 1 49 8 3 51 9 \0 0

Strings #include <stdio.h> int main (int argc, char **argv) { printf ( hello, world\n ); return 0; h e l l o, w o r l d \n \0 104 101 108 108 111 44 32 119 111 114 108 100 10 0

Strings #include <stdio.h> int main (int argc, char **argv) { printf ( hello, world\n ); return 0; but how does C pass this into a function? h e l l o, w o r l d \n \0 104 101 108 108 111 44 32 119 111 114 108 100 10 0

a first look at Pointers data live in RAM at various addresses variables usually hide that detail but it can be necessary to use addresses for now just show the principle: * in declarations * and & in statements later in this course we will use pointers for links between data elements customizing data structure functions mutable function arguments

a first look at Pointers int foo = 42117039; char bar = -17; memory (hexadecimal) address value 7028 02 7029 82 702a a7 702b af 702c ef 702d 702e 702f 7030 7031 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef 702d 702e 702f 7030 7031 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; * in declaration means: fp stores an address where an int is stored memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 702f 7030 7031 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; fp = &foo; memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 702f 7030 7031 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; fp = &foo; & means: get the address of the variable memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 702f 7030 7031 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; fp = &foo; = 0x7028 = means: store it in the variable fp memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 70 702f 28 7030 7031 7032 7033

a first look at Pointers fp = &foo; memory (hexadecimal) address value foo: 7028 02 7029 82 int foo = 42117039; char bar = -17; 702a 702b a7 af bar: 702c ef int *fp; 702d fp: 702e 70 pointers are variables that store an address 702f 28 7030 7031 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; the same principle applies to any type memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; *fp = -17; memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; *fp = -17; * in statement means: (the int value) stored at (the address stored at fp) memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; *fp = -17; * in statement means: (the int value) stored at (the address stored 0x7028 at fp) memory (hexadecimal) address value foo: 7028 02 7029 82 702a a7 702b af bar: 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; *fp = -17; = means: write -17 * in statement means: (the int value) stored at (the address stored 0x7028 at fp) memory (hexadecimal) address value foo: 7028 bar: 7029 702a 702b 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; *fp = -17; = means: write -17 * in statement means: (the int value) stored at (the address stored 0x7028 at fp) memory (hexadecimal) address value foo: 7028 ff 7029 ff 702a ff 702b ef bar: 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

a first look at Pointers int foo = 42117039; char bar = -17; int *fp; char *bp = &bar; fp = &foo; *fp = -17; when dealing with pointers, a table like this can help a lot memory (hexadecimal) address value foo: 7028 ff 7029 ff 702a ff 702b ef bar: 702c ef fp: 702d 702e 70 702f 28 bp: 7030 70 7031 2c 7032 7033

Back to the Question #include <stdio.h> int main (int argc, char **argv) { printf ( hello, world\n ); return 0; but how does C pass this into a function? h e l l o, w o r l d \n \0 104 101 108 108 111 44 32 119 111 114 108 100 10 0

Back to the Question #include <stdio.h> int main (int argc, char **argv) { printf ( hello, world\n ); return 0; 0x7028 just pass the address of the first letter! 7028 7029 702a 702b 702c 702d 702e 702f 7030 7031 7032 7033 7034 7035 h e l l o, w o r l d \n \0 104 101 108 108 111 44 32 119 111 114 108 100 10 0

Back to the Question #include <stdio.h> int main (int argc, char **argv) { printf ( hello, world\n ); return 0; thankfully, this is usually completely hidden from the programmer 7028 7029 702a 702b 702c 702d 702e 702f 7030 7031 7032 7033 7034 7035 h e l l o, w o r l d \n \0 104 101 108 108 111 44 32 119 111 114 108 100 10 0

Pointers? Enough for Now! pointers seem to be the most confusing part of C for many people but once you understand them, everything becomes so easy: strings are just pointers arrays are just pointer even function are just pointers...exercises: practice makes perfect...

Take-Home Message values: types and variables arrays, strings, pointers flow: if and else for and while loops functions: argument list return type pass by value

Reading List for this week (for exercises 1 + 2) [K+R] 2.1-10, 3.1-5, 3.7, 5.1, 5. 3, 5. 4, 7.1-2 for next week (lecture 2, exercises 3 + 4) [K+R] 1.1-10, 4.1-2, 4.8, 4.10, 5.2, 6.1-4, 6.7