CSCI 171 Chapter Outlines

Similar documents
C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS


Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and


Advanced C Programming and Introduction to Data Structures

Model Viva Questions for Programming in C lab

C programming basics T3-1 -

Review of the C Programming Language

Review of the C Programming Language for Principles of Operating Systems

Computers Programming Course 5. Iulian Năstac

Introduction to Programming Using Java (98-388)

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

CS201 Some Important Definitions

C-LANGUAGE CURRICULAM

edunepal_info

The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

Short Notes of CS201

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

Computers Programming Course 6. Iulian Năstac

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

CS201 - Introduction to Programming Glossary By

Problem Solving and 'C' Programming

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

6.096 Introduction to C++ January (IAP) 2009

Dynamic memory allocation (malloc)

Course Title: C Programming Full Marks: Course no: CSC110 Pass Marks: Nature of course: Theory + Lab Credit hours: 3

M1-R4: Programing and Problem Solving using C (JULY 2018)

High Performance Programming Programming in C part 1

EL6483: Brief Overview of C Programming Language

CS 11 C track: lecture 5

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

by Pearson Education, Inc. All Rights Reserved.

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Lectures 5-6: Introduction to C

Compiling and Running a C Program in Unix

Introduction to C Language

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

UNIT IV-2. The I/O library functions can be classified into two broad categories:

C Programming Multiple. Choice

Procedural programming with C

A Fast Review of C Essentials Part I

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Kurt Schmidt. October 30, 2018

Continued from previous lecture

UNIT- 3 Introduction to C++

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

Lectures 5-6: Introduction to C

Pointers and File Handling

C mini reference. 5 Binary numbers 12

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Appendix G C/C++ Notes. C/C++ Coding Style Guidelines Ray Mitchell 475

ANSI C Programming Simple Programs

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Lecture 9: File Processing. Quazi Rahman

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

COMPUTER APPLICATION

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Variation of Pointers

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

SAURASHTRA UNIVERSITY

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

Standard File Pointers

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

A Crash Course in C. Steven Reeves

BLM2031 Structured Programming. Zeyneb KURT

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Chapter 2: Basic Elements of C++

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

ET156 Introduction to C Programming

ECE264 Spring 2014 Exam 2, March 11, 2014

Have examined process Creating program Have developed program Written in C Source code

C & Data Structures syllabus

Standard C Library Functions

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

ESC101N: Fundamentals of Computing End-sem st semester

Fundamental of C programming. - Ompal Singh

Contents. A Review of C language. Visual C Visual C++ 6.0

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

PESIT-BSC Department of Science & Humanities

MISRA-C:2012 Standards Model Summary for C / C++

C Language, Token, Keywords, Constant, variable

Transcription:

Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures (for loops)... 6 CSCI 171 Chapter 4 (Sections 1 8) Functions... 7 CSCI 171 Chapter 4 (Section 10) Recursion... 8 CSCI 171 Chapter 4 Recursion (Recurrence relations and Linear Homogeneity)... 9 CSCI 171 Preprocessor (not in book) and macros... 10 CSCI 171 Chapter 5 Arrays and Matrices... 11 CSCI 171 Chapter 6 (Sections 1 6) Pointer Basics... 13 CSCI 171 Chapter 6 (Section 8) Dynamic Memory Allocation... 14 CSCI 171 Chapter 3 (Section 7) Files... 16 CSCI 171 Chapter 7 Basic Structures... 18 CSCI 171 Chapter 7 Structures and Pointers... 19 1

CSCI 171 Chapter 1 Overview Generic Concepts Application software vs. System software Language generations Other languages Problem solving technique (define, devise, implement, test) Programming Language Words and symbols Rules of the language Syntax and semantics Necessary Information Is computer program required? Inputs/Outputs Formulas Algorithm C History o Bell Telephone Laboratories (1972) o Dennis Richie (also created Unix) o A B C ANSI standard Advantages o Powerful, flexible, stable o Popular o Portable Program cycle o Develop algorithm o Write source code (.c and.h files) o Link and compile (.exe file) o Errors (compile, run-time, logic) 2

CSCI 171 Chapter 2 Programming Components main function drives program flow first and last statements executed are in main #include preprocessor directive includes additional files in program program statements I/O execute functions perform calculations other functions block of code that perform specific tasks discussed in detail later comments documentative stripped out by preprocessor variables declaration o reserve memory o supply name and type naming rules types o integer char, short, int, long o floating-point float, double sample program 2_1 constants #define preprocessor directive const reserved word 3

basic I/O printf o C s basic output function scanf o C s basic input function conversion specifiers o %c, %d, %f, %lf, %s escape sequences o \a, \n, \t, \\, \ sample program 2_2 operators assignment mathematical o unary pre and post increment and decrement o binary +,, *, /, % precedence follows standard mathematical order of operations o compound assignment (operator equals) sample program 2_3 conditional o only C ternary operator o sample program 2_4 4

CSCI 171 Chapter 3 (Sections 1 4) Selection Structures Structure theorem More operators Relational operators and Boolean values o ==,!, >, >=, <, <= Evaluation of relational expressions (0 or 1) Evaluation of integers Logical o!, &&, Selection structures family of if statements o if o if else o nested if linear non-linear o relationship to conditional operator o sample program 3_1 switch o sample program 3_2 5

CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures (for loops) Structure theorem Iteration structures repeated execution of a block of code for loop basics o used when the number of iterations is known PRIOR TO loop execution o syntax loop control variable initialization condition incrementation code blocks arrays o Declaration o Initialization o Referencing elements sample program 4_1 sample program 4_2 additional for loop concepts o variable scope o additional functionality break continue omission of initialization omission of incrementation (intentional or otherwise ) o logical operators in a for loop o commas in a for loop o nested for loops sample program 4_3 6

CSCI 171 Chapter 4 (Sections 1 8) Functions function uniquely named independent may receive or send information function anatomy prototype definition o header o body return type parameter list sample program 7_1 functionality only used if called o information may be sent in via the parameter list may have local variables after code is executed, control returned to calling function o information may be sent back via a return statement variable scope global variables o generally considered poor structure local variables reinitialized with each call o sample program 7_2 static variables o maintain value o default initialization of 0 function calls resolve to the returned value can be used wherever the returned datatype is allowed o printf o in calculation o part of a condition o as parameter to another function other 7

CSCI 171 Chapter 4 (Section 10) Recursion Review of mathematical sequences: Explicitly defined Example: a n nn ( 1) 3 2 Recursively defined Example: bn bn 1 n where b1 4 Recursion in computer science definition calls itself must have at least one base case 4 questions for recursion what is the base case(s)? how can problem be defined in terms of a smaller case? will the base case be reached? how are the smaller-case solutions used to find the original solution? Recursive sequence sample program 9_1 (recursively written) sample program 9_2 (explicitly written) Implementation run-time stack o duplicate variables 8

CSCI 171 Chapter 4 Recursion (Recurrence relations and Linear Homogeneity) Examples a n nn ( 1) 3 2 b b n where b 4 n n1 1 Recurrence relation definition solving Linear Homogeneity identifying linearly homogenous sequence characteristic function theorem to solve Theorem to find an explicit formula which generates the same sequence as a recursive formula which is linearly homogenous of degree 2: If the characteristic equation 2 x r1x r2 0 of the recurrence relation n n an r1 an 1 r2 an2 has 2 distinct roots and, then an us1 vs2 (where u and v depend on initial conditions) is the explicit formula for the sequence. If the characteristic equation n 1 n1 2 n2 s 1 2 x r1x r2 0 a r a r a has a single root s, then s 2 of the recurrence relation n n an us vns (where u and v depend on initial conditions) is the explicit formula for the sequence. Example: Use linear homogeneity to find an explicit sequence that is equivalent to: an an 1 an2 a1 a2 2 15 where 3 and 8 9

CSCI 171 Preprocessor (not in book) and macros Preprocessor part of all C compilers produces modified code based on directives Directives #include o <> for built-in libraries o for user-defined libraries additional directives used #ifndef #endif see directions linked from CSCI 171 Links and Downloads page #define o substitution macro #define PI 3.14159 o function macro example: #define HALFOF(value) ((value)/2) sample program 10_1 common errors forgetting parenthesis o example: #define AREA(x, y) (x*y) placing space after function name o becomes a substitution macro o example: #define AREA (x, y) ((x)*(y)) compared to regular functions no overhead to find the function disadvantages limited functionality expanded in code for every call o Sometimes used to simply indicate something has been defined: #define _USE_MATH_DEFINES #include <math.h> #undef o opposite effect of #define 10

CSCI 171 Chapter 5 Arrays and Matrices matrices addition multiplication multiplication by scalar transposition Boolean matrices o join o meet o Boolean product arrays declaration and initialization memory usage o sizeof function arrays as arguments to functions function prototype/definition/call o sample program 11_1 passing by value o sample program 11_2 passing by reference o sample program 11_3 multi dimensional arrays number of dimensions = number of subscripts o no set limit o increased complexity / decreased performance declaration and initialization geometric objects o numbers stored adjacently multi dimensional arrays as parameters to functions function prototype/definition/call o sample program 11_4 all dimensions other than the first MUST be provided in prototype and header o Why? in an array declared as: int x[5][7] which element is: x[3][4]? 11

strings arrays of characters output o printf(%s conversion specifier) o puts input o scanf (no & is used) %s conversion specifier leave a space before the %s to clear buffer dangerous because user can enter a string larger than the array reads up to whitespace example: scanf( %s, name); o gets will read a string, but has no length safeguard either reads up to carriage return example: gets(name); o see sample program 11.5 for a solution use this function anytime a string is needed from input 12

CSCI 171 Chapter 6 (Sections 1 6) Pointer Basics variables have a data type have specific location in RAM o address & is address operator &x is read as the address of x o can be printed with %p segment and offset o can be printed with %d easier to read pointers variables that hold an address declared using the indirection operator default initialization should be NULL using pointers contents accessed the same way as any other variable if indirection operator precedes the pointer, it refers to the contents of the pointed to variable o indirect access sample program 12_1 arrays (sometimes called pointer constants ) array name refers to address of first byte of first element o location cannot be changed pointer can reference an array o array can be traversed via pointer o pointer arithmetic adding 1 to a pointer adds the size of the data type in bytes only adding or subtracting integers is valid no other arithmetic operations allowed no floating point data types o sample Program 12_2 pointers and functions like arrays, pointers can be parameters to functions o sample program 12_3 unlike arrays, pointers can be returned from functions o more complicated than returning simple data types and many pitfalls 13

CSCI 171 Chapter 6 (Section 8) Dynamic Memory Allocation static memory allocation memory allocation can be determined at compile time memory is allocated immediately upon program execution o does not change o example: int x = 3; dynamic memory allocation memory allocated during program execution o cannot be determined at runtime functions in stdlib.h library that implement this o malloc o calloc o realloc o free deallocation function malloc function prototype: void * malloc(int); o return: address of allocated space (NULL if unable to allocate) o parameter number of bytes to allocate issues with allocation functions returned values must always be cast to appropriate data type returned value must always be checked for NULL sizeof function must be used for portability o sample program 12_4 memory leaks o memory that is not de-allocated is lost until program termination sample program 12_5 free function prototype: void free(void *); o parameter pointer to de-allocate o function DOES NOT set pointer to NULL (you have to do this manually) calloc function prototype: void * calloc(int, int); o return: address of allocated space (NULL if unable to allocate) o parameters number of objects to allocate space for size of each object o calloc clears the allocated memory to 0 o sample program 12_6 14

realloc function prototype: void * realloc (void *, int); o return: address of allocated space (NULL if unable to allocate) o parameters pointer which is to be changed current values associated with pointer are maintained any de-allocation will be handled if pointer is NULL, function behaves like malloc new size in bytes o sample program 12_7 if int is 0, function behaves like free can capture the returned NULL value in the pointer to avoid having to set it to NULL manually 15

CSCI 171 Chapter 3 (Section 7) Files CSCI 171 Chapter Outlines Files and File Streams types o text 0 255 characters ending in carriage return can be viewed in text editor o binary machine readable (cannot be displayed) data dumped in unchanged format from RAM rules o naming conventions dictated by operating system o file path and name stored in a string char * file_name = c:\\temp\\myfile.txt ; char * file_name = c:/temp/myfile.txt ; functions associated with files reside within stdio.h functions to open and close files o fopen parameters pointer to the file pointer file name (String) return file mode (String) o r open for reading ( rb for binary file) returns NULL if file cannot be opened o w open for writing ( wb for binary file) creates file if it does not exist data is erased if file does exist o a open for appending ( ab for binary file) creates file if it does not exist data is not erased if file does exist NULL if not successful always check to see if return is NULL, and handle this appropriately o fclose closes a file parameter FILE pointer 16

functions associated with output and input to and from text files o fprintf text file output same as printf_s, except first parameter is the FILE pointer sample program 13_1 o fputs text file output same as puts, except second parameter is the FILE pointer o fscanf text file input same as scanf_s, except first parameter is the FILE pointer sample program 13_2 o fgets text file input same as gets, except second parameter is max number of characters and third parameter is the FILE pointer detecting end-of-file o feof determines if the end-of-file has been reached parameter the file in question sample program 13_5 functions associated with output and input to and from binary files o fwrite binary file output parameters pointer to data to be written size of each object to be written number of objects to be written FILE pointer returns the number of objects written sample program 13_3 o fread binary file input parameters pointer to store the read data size of each object to be read number of objects to be read FILE pointer returns the number of objects read sample program 13_4 17

CSCI 171 Chapter 7 Basic Structures CSCI 171 Chapter Outlines structure collection of one or more variables grouped under a single name o each variable is called a member structure members may be: o simple data type (int, double, etc.) o array o pointer o another structure when an actual structure has been declared, it is called an instance defining a structure 3 ways o instances immediately declared no more declaration possible sample program 14_01 o structure defined and given a name declarations can be done at anytime struct reserved word must be used with declaration sample program 14_02 o typedef compiler token used most structured approach declarations done in the same way as other data types struct reserved word is not necessary sample program 14_03 accessing structure members the dot member operator is used between the instance and member sample program 14_04 other concepts array members sample program 14_05 structure members sample program 14_06 arrays of structures sample program 14_07 functions that use structures o as parameters sample program 14_08 o as return type sample program 14_09 18

CSCI 171 Chapter 7 Structures and Pointers pointers to structures declared like any other pointer accessing members o dereferencing the pointer and using the dot member operator the dot member operator has precedence over dereferencing o indirect membership operator ( >) o sample program 14_10 like all pointers, memory can be allocated to point to multiple structures o sample program 14_11 structures with pointer members members declared within the structure definition like any other pointer o memory for pointer members will be dynamically allocated: after structure is instantiated before structure is used sample program 14_12 pointers to structures with pointer members assimilation of the ideas above o memory allocation for structure must occur prior to memory allocation for members sample program 14_13 dynamic data structures linked lists stacks queues trees 19