CSCI 2132 Software Development - Dalhousie Universityvlado/csci2132/slides/sd35-slides.pdf · How...

44
CSCI 2132 Software Development Lecture 35: Dynamically Allocated Arrays, Course Review Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 5-Dec-2017 (35) CSCI 2132 1

Transcript of CSCI 2132 Software Development - Dalhousie Universityvlado/csci2132/slides/sd35-slides.pdf · How...

CSCI 2132Software Development

Lecture 35:

Dynamically Allocated Arrays, Course Review

Instructor: Vlado Keselj

Faculty of Computer Science

Dalhousie University

5-Dec-2017 (35) CSCI 2132 1

Previous Lecture

• Shell Scripting (continued)• conditional expressions on files• exit code, example• Dynamic Arrays

– String examples– Example reversewords

5-Dec-2017 (35) CSCI 2132 2

Reversing the Words, Revisited

• Another idea:– Reverse the complete string– Reverse each word within string once more

• Fill-in-the-blanks code available in

˜prof2132/public/reversewords2.c-blanks

(in one line)

5-Dec-2017 (35) CSCI 2132 3

Dynamically Allocated Arrays

• Similarly to strings we can allocate arbitrary arrays; for example:

int *array, i;array = (int*) malloc(n * sizeof(int));if (array == NULL) {...

}for (i = 0; i < n; i++)array[i] = 0;

...free(array);

• or simpler

int *array;array = (int*) calloc(n, sizeof(int));if (array == NULL) { ... /* error */ }

5-Dec-2017 (35) CSCI 2132 4

Dynamically Allocated Arrays andVLAs

• Using dynamic memory vs VLA• VLAs are allocated and deallocated more

efficiently (on stack)• Heap more appropriate for large arrays• Heap appropriate for arbitrary lifespan• Heap more appropriate for portability (C99)

5-Dec-2017 (35) CSCI 2132 5

Mergesort Using Dynamic Arrays

• Fill-in-the-blanks code available at:

˜prof2132/public/mergesort3.c-blanks

5-Dec-2017 (35) CSCI 2132 6

Dynamic Arrays: Resizable Arrays

• How to implement something like ArrayList in Java, or vector class inC++?

• Pseudocode for adding elements:

If array is fullResize the array to twice its current capacity

using reallocStore the new element

• The main structure:

struct vector {int *array;int capacity;int size;

}

5-Dec-2017 (35) CSCI 2132 7

Resizable Array Implementation:dynamicarray.c-blanks

• Fill-in-the-blanks implementation can be foundat:

˜prof2132/public/dynamicarray.c-blanks

(one line)

5-Dec-2017 (35) CSCI 2132 8

Dynamic Array: Time Complexity

• What can happend when push_back is calledregarding execution time?

• Why do we consider this a reasonably efficientsolution?

• Why we must be careful when determiningwhen to shrink the array?

• This provides a better explanation of behaviourof the ArrayList in Java and the vector class inC++

5-Dec-2017 (35) CSCI 2132 9

Final Exam Studying

• Lecture notes and Lab notes• Textbook reading (according to the list)• Assignments and exams (midterms and

samples)• Textbook questions• Textbook exercises

5-Dec-2017 (35) CSCI 2132 10

Course Review

• Lecture 1: Course Introduction– Course Introduction∗ logistics and administrivia∗ important dates, course description∗ evaluation scheme and criteria∗ textbooks∗ lectures, exams, assignments, lab work

– Academic integrity policy– Culture of respect– Main learning objectives

5-Dec-2017 (35) CSCI 2132 11

• Part I: Unix Operating System• Lecture 2: Introduction to UNIX and Unix-like Operating

Systems– Motivation: Why UNIX, Why C– Tentative list of course topics– Introduction to UNIX– Reading: Chapter 1 of the UNIX book– Onion skin model of OS– OS functionalities– UNIX history

5-Dec-2017 (35) CSCI 2132 12

• Lecture 3: Files and Directories– Main UNIX concepts– Unix architecture– Shells– Logging in– PuTTY– Some basic utilities and commands∗ date, clear, passwd, man

– Shell metacharacters– ‘cat’ example, file redirection– Logging out– Files and Directories

5-Dec-2017 (35) CSCI 2132 13

• Lecture 4: File Permissions– Pathnames– Commands for managing and navigating directory

structure– Commands: cat, logout, exit, ls, dirname, basename,

pwd, cd, mkdir, rmdir, mv, rm, tree– File manipulation, commands– File permissions:∗ users, groups∗ checking permissions

5-Dec-2017 (35) CSCI 2132 14

• Lecture 5: File Permissions, I/O Redirection– Changing file permissions– Changing user and group owners– Redirection∗ standard input, output, and error

• Lecture 6: Links and Inodes– Pipes– Inodes– Hard links– Soft links

5-Dec-2017 (35) CSCI 2132 15

• Lecture 7: Wildcards and Regular Expressions– Filename substitution (wildcards)– Regular expressions∗ basic regular expressions∗ grep, filters

5-Dec-2017 (35) CSCI 2132 16

• Lecture 8: Shells, Processes, and Job Control– Extended regular expressions (ERE)– Shells and Job Control– Shells∗ functionality∗ popular shells

– Bash shell∗ commands∗ variables

– Processes and programs∗ process memory composition

5-Dec-2017 (35) CSCI 2132 17

• Part II: C programming Language and SoftwareDevelopment

• Lecture 9: Job Control, Introduction to C– Processes and programs∗ process memory composition∗ threads∗ process Control Block (PCB)

– Process creation∗ fork and exec system calls

– Job control and process control∗ foreground and background processes∗ commands for managing jobs and processes

– Introduction to C

5-Dec-2017 (35) CSCI 2132 18

• Lecture 10: Formatted Input and Output– Introduction to C: writing a simple program– Compilation process– C Language Basics:∗ program structure, preprocessor directives, functions∗ statements∗ string literals and printing strings

5-Dec-2017 (35) CSCI 2132 19

• Lecture 11: Formatted Input and Output– C Language Basics (continued):∗ input and output∗ constants∗ simple example

– Formatted input and output∗ printf∗ scanf

5-Dec-2017 (35) CSCI 2132 20

• Lecture 12: Examples with scanf– Conversion specifications for scanf, examples– Operators, expressions: Java vs. C

• Lecture 13: Expression and Control Structures– C control structures; comparison with Java– Implementation-defined and unspecified behaviour– Logic expressions– Differences between C99 and earlier standards– goto statement, null statement

5-Dec-2017 (35) CSCI 2132 21

• Lecture 14: Software Development Life Cycle– Software Development Life Cycle (SDLC)– Waterfall model– Rapid prototyping model– Software testing

• Lecture 15: C Basic Types– Software testing and debugging, gdb– C basic types– Integer types– Integer type specifiers– Integer representation– Floating-point number types

5-Dec-2017 (35) CSCI 2132 22

• Lecture 16: C Arrays– Character types– Type conversion– Implicit type conversion of operands and assignments– Explicit type conversion (type casting)– typedef and sizeof keywords– C Arrays: one-dimensional arrays

• Lecture 17: Array Example, Multidimensional Arrays– Example: binary search– Multidimensional arrays– Variable-length arrays

5-Dec-2017 (35) CSCI 2132 23

• Lecture 18: Functions– Example: Latin square– Function definitions– Function declarations or prototypes– Arguments and parameters– Arguments passed by value– Call stack– Simple recursion example: power

• Lecture 19: Recursion Examples: Mergesort– Mergesort implementation and discussion

5-Dec-2017 (35) CSCI 2132 24

• Lecture 20: Recursion Examples: Quicksort andPermutations– Mergesort complexity calculation– Quicksort vs Mergesort– Example: Generating permutations

• Part III: Program Organization and Dynamic MemoryAllocation

• Lecture 21: Program Organization– Generating permutations (finished)– Multidimensional arrays as arguments– Program Organization:– Local variables– External (global) variables

5-Dec-2017 (35) CSCI 2132 25

• Lecture 22: Pointers– Organizing a C program in a single file– Example: Decimal to binary– Blocks and compound statements; Scope– Pointers: Reading: Ch12 Pointers– Introduction and history– Pointer variables, reference type– Address operator (&), indirection operator (*)– Pointer Assignment– Pointer Examples

5-Dec-2017 (35) CSCI 2132 26

• Lecture 23: Pointers and Arrays– Pointer assignment– Pointer arguments– Example: statistics.c– Pointers and arrays– Pointer arithmetic: pointer + integer

• Lecture 24: Pointer Examples– Pointer arithmetic: subtraction and comparison– Pointer examples– Mergesort revisited, with pointers

5-Dec-2017 (35) CSCI 2132 27

• Lecture 25: Strings– Midterm 2 review– Finished mergesort2 “fill-in the blanks”– Strings:– String literals– String variables

• Lecture 26: String Library– Assignment discussion– Reading and writing strings– String library functions

5-Dec-2017 (35) CSCI 2132 28

• Lecture 27: Writing Large Programs– String library functions (continued)– Command-line arguments– sortwords.c example with insertion sort– Writing large programs: header files and “.c” files– modules and header files

• Lecture 28: Compilation of Large Programs– Protecting header files from double-inclusion– prj-dec2bin and stack example– Compilation of large programs– make utility (started)

5-Dec-2017 (35) CSCI 2132 29

• Lecture 29: Dynamic Memory Allocation– make utility– Using gdb with multi-file programs– Structures– Arrow operator– Dynamic memory allocation: malloc and free

5-Dec-2017 (35) CSCI 2132 30

• Lecture 30: Linked Lists– Linked lists– Linked list example: student database– Analysis of the program list.c

• Lecture 31: Mergesort with Linked Lists– Mergesort with linked lists∗ Algorithm∗ Example with students database: sortlist.c

• Lecture 32: File Operations in C– Heap (Free Store)– File Manipulation in C:– Opening a file, closing a file– Formatted I/O, character I/O

5-Dec-2017 (35) CSCI 2132 31

• Lecture 33: Shell Scripting– Block reading and writing– File positioning– Examples– Shell Scripting: introduction, a basic example– variables– arithmetic operations, conditional expressions– ‘if’ statement, ‘for’ loop– ‘case’ statement

5-Dec-2017 (35) CSCI 2132 32

• Lecture 34: Dynamically Allocated Arrays– Shell Scripting (continued)– conditional expressions on files– exit code, example– Dynamic Arrays∗ String examples∗ Example reversewords

• Lecture 35: Dynamically Allocated Arrays, CourseReview

5-Dec-2017 (35) CSCI 2132 33

• Lab 1: UNIX: Getting Started– Logging into server: putty, ssh– pwd– mkdir, ls, chmod– lab1 directory– Preparing Java file in Emacs– Compiling and running Java program– Using emacs for search-and-replace– Using submit command– Command head

5-Dec-2017 (35) CSCI 2132 34

• Lab 2: Unix Utilities; Emacs– Autocompletion in Bash– Emacs: beginning and end of a line– Emacs: copy and paste, cut and paste– Emacs: undo– Filters: uniq, sort, sort with CSV– cut, a pipe example

5-Dec-2017 (35) CSCI 2132 35

• Lab 3: SVN Source Version Control Tutorial– Learning about SVN Source Version Control system– Concept of repository and working copy– Checkout and Adding a Directory– Commit Command– Adding Files– Deleting Files– Checking out second copy– Changing files, update– Accidental Delete– Parallel Changes, merging changes, conflicting

changes

5-Dec-2017 (35) CSCI 2132 36

– Retrieving copies of previous work

5-Dec-2017 (35) CSCI 2132 37

• Lab 4: Exploring bash and C Compilation– echo, /etc/shells, .bashrc, .profile– diff– Writing a C program, compiling, using Emacs to

compile– Suspending Emacs– Examining exit code: $?– Email client: pine

5-Dec-2017 (35) CSCI 2132 38

• Lab 5: Dr. Tig’s Git Tutorial– What is Git? What is GitHub?– Cloning– History, git log– Branches– Diff, Branch Diff– Creating a repository– git config, editor– Commit– Merge and merge conflicts– Optional sharing

5-Dec-2017 (35) CSCI 2132 39

• Lab 6: Formatted Input and Output– man command, reading about printf and scanf– Experiments with printf– C Compilation in Emacs, setting hot keys– Multiple windows in Emacs– Precision in printf, exploring scanf– An exercise: phone numbers

5-Dec-2017 (35) CSCI 2132 40

• Lab 7: gcc and gdb– gcc options: -Wall, -g– gdb: running, breakpoints– Using gdb in emacs– gcc option -std

– Debugging an incorrect program:– Compiling binary.c (binary search)– Running gdb to find a bug– Finding another bug: setting break points, print, display– Testing program

5-Dec-2017 (35) CSCI 2132 41

• Lab 8: Practicum 1• Lab 9: Practicum 2• Lab 10: Practicum 3

5-Dec-2017 (35) CSCI 2132 42

• Lab 11: Make Utility– Getting a copy of directory lab-make– Running make commands– Using make variables– Preparing make for gdb– Dividing a program into multiple files

5-Dec-2017 (35) CSCI 2132 43

• Lab 12: Shell Scripting• Starting with Shell Scripting• Simple “Add” Script• Using For Loop• For Loop, another form• Using the Case Statement• “Save” Example

5-Dec-2017 (35) CSCI 2132 44