Structured Programming. Jon Macey

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

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

ANSI C Programming Simple Programs

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Data types, variables, constants

Chapter 1 & 2 Introduction to C Language

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

Fundamental of Programming (C)

Fundamentals of Programming

DEPARTMENT OF MATHS, MJ COLLEGE

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

6.096 Introduction to C++ January (IAP) 2009

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

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

Lecture 3. More About C

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Programming for Engineers Iteration

CS107, Lecture 3 Bits and Bytes; Bitwise Operators

Programming and Data Structures

Flow Control. CSC215 Lecture

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

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

Lecture 3 Memory and Pointers

Introduction to Programming

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

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

Lecture 3 Tao Wang 1

Operators and Expressions:

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

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

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

A Fast Review of C Essentials Part I

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Computer Programming CS F111

Syntax and Variables

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Programming. Elementary Concepts

Chapter 2: Overview of C. Problem Solving & Program Design in C

Chapter 2: Using Data

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Programming. Data Structure

Reserved Words and Identifiers

Introduction to C. Systems Programming Concepts

Review of the C Programming Language for Principles of Operating Systems

3. Java - Language Constructs I

Chapter 2, Part III Arithmetic Operators and Decision Making

Lecture 02 C FUNDAMENTALS

CS Programming In C

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

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

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Programming Lecture 3

A flow chart is a graphical or symbolic representation of a process.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Department of Computer Applications

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

ET156 Introduction to C Programming

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

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

Pointers (2) Applications

Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Basics of Programming

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Chapter 3. Fundamental Data Types

Visual C# Instructor s Manual Table of Contents

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

ISA 563 : Fundamentals of Systems Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Review of the C Programming Language

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

ARG! Language Reference Manual

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Data Types and Variables in C language

Selection Statements. Pseudocode

BLM2031 Structured Programming. Zeyneb KURT

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Lecture 2 Tao Wang 1

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Data Types and Variables in C language

THE FUNDAMENTAL DATA TYPES

C Programming a Q & A Approach

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

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

Computer System and programming in C

Java Notes. 10th ICSE. Saravanan Ganesh

This book is licensed under a Creative Commons Attribution 3.0 License

Transcription:

Structured Programming Jon Macey

Structured Programming Structured programming is an attempt to formalise the process of program development. There are several basis for a theorem of structured programming, some of which go back the mid 1940 s we will concentrate on the following papers :- "Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules" Bohm and Jacopini (1966) Flow chart techniques for structured programming Nassi and Shneiderman 1973

Structured Programming Theorem The structured programming theorem states that any computable function may be built from three building blocks :- 1. sequences of operations. 2. selections ( executing one of two operations based on a boolean value). 3. iteration (repetition) repeating an operation until some sentinel value is reached.

Sequence A sequence is a linear execution of program commands The sequence of the two commands A and B makes sure that after entering the sequence A is always NS Chart A B flowchart A followed by B and the exit can only be reached by doing this sequence B

Selection At it s simplest selection asks a question and will execute a sequence if the answer to the question is true This is know as a selection monadic(1) It is also possible to execute different sequences depending upon the answer (true / false) This is know as a dyadic (2) selection. 1) (Philosophy / Logic) (Mathematics) Logic Maths (of an operator, predicate, etc.) having only a single argument place 2) (Philosophy / Logic) Logic Maths (of a relation, predicate, etc.) relating two terms; binary Compare monadic, polyadic

Monadic Selection condition true true condition false false A A Here we check a condition and if true execute A else continue

dyadic Selection true condition false true condition false A B A B In this version we execute A if the condition is true else we execute B

multiple selection Depending upon the programming language there are different methods for doing multiple selection. Some of these methods are language constructs, whilst some are the nesting of if / else clauses? condition value 1 value 1 value 3 default value A B C D A B C D A

iteration (loop execution) We can break iteration into 3 different categories iterative (head controlled loop) repetitive (foot controlled loop) continuous (never ending loop) We can also use a technique called recursion (which we will look at later in the year)

Head controlled loop condition A A? true false This loop structure will check the condition first and then execute the function To enter this structure the exit condition must be true in the first place. Once this is complete the condition is checked again and repeated until the condition is false This is know as a do-while loop

foot controlled loop A A condition? false true In this loop construct the function A is executed at least once Then depending upon the condition may be executed again This is know as a do-until loop

continuous loop A A A continuous loop has an entrance but no exit condition or check. These kinds of loops can be useful for systems that need to run forever without interruption (embedded systems) However they may also be problematic and can be caused by bugs in code.

continuous with exit(s) It is possible to construct loops with multiple exit conditions Whilst these are common, there are sometimes issues with maintenance / debugging using page A exit? B exit? C these constructs.

Structured Programming We will return to these constructs soon, and see how we can build them in different programming languages. But first we are going to introduce notational technique to help describe the syntax of programming languages. This will aid us in explaining the programming constructs in a more formal way before jumping into specific programming language syntax

BNF (Backus Naur Form) In the 1950 s both John Backus and Noam Chomsky invented the same notation which has become widely used to describe programming language syntax. Chomsky is a linguist and never intended to apply this work to computers Backus however was involved in the specification for a programming language called ALGO and used this context free grammar to describe syntax which he called BNF. It was only later that the connection between the two systems were made

Meta-languages A meta-language is a language to describe another language. BNF uses abstractions for syntactic structures. A BNF is a set of rules written like this <symbol> ::= expression symbol is a syntactic variable (or non-terminal) meaning a variable that can be replaced The expression is what the symbol is representing and is usually a finite combination of other symbols

alternatives In BNF alternatives may be separated by the vertical bar <digit>:== "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" In this case we state that a digit can be any of the numbers 0 through 9

(E)xtended- BNF E-BNF has a different syntax from the previous BNF, notation but has a number of advantages of basic BNF. BNF < digit >:== "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" <> no longer needed E-BNF digit excluding zero = "1" "2" "3" "4" "5" "6" "7" "8" "9"; digit = "0" digit excluding zero; Note ; to end statement

E-BNF, rule A comma indicates a concatenation for example eleven = "1", "1"; one hundred and eleven = "1", eleven; or perhaps? one hundred and eleven = eleven, "1";

E-BNF {} Expressions that may be omitted or repeated can be represented through curly braces digit excluding zero = "1" "2" "3" "4" "5" "6" "7" "8" "9"; digit = "0" digit excluding zero; natural number = digit excluding zero, {digit};

Usage Notation definition = concatenation, termination ; alternation option [...] repetition {...} grouping () terminal string... comment (*...*) exception - integer = "0" [" "], natural number;

Integers In computing the integer data type is used to represent the whole numbers (including zero) We can further refine the definition by stating if they are signed or unsigned digit excluding zero = "1" "2" "3" "4" "5" "6" "7" "8" "9"; digit = "0" digit excluding zero; unsigned integer = digit, {digit}; signed integer = unsigned integer + unsigned integer - unsigned integer;

variable declarations in C In C we can declare variables using the syntax <variable type> <variable identifier>; <variable type> <var1>,<var2>... <var n>; Where variable type indicates one of the C data types identifier is a valid name for a variable

valid variable names The following rules must be applied to C variable names must not begin with a number spaces are not allowed in names Only letters digits and _ are valid characters C keywords are not allowed digit = "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"; letter = "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"; start char = "letter" "_"; variable name = start char, {digit} {letter};

C reserved words auto default float register struct volatile break do for return switch while case double goto short typedef char else if signed union const enum int sizeof unsigned continue extern long static void

integer data type In C / C++ we can specify an integer using the int keyword. The range of an integer is dependant upon the machine architecture but is usually a whole 16, 32 or 64-bit (2, 4 or 8 bytes, respectively) addressable word. By default the int data type is signed (can be positive or negative) Typical range is 2147483648 to +2147483647

example #include <stdio.h> #include <stdlib.h> int main() { int a=10; int b=20; printf("a+b = %d \n",a+b); int abignumber = 4294967295; int one= 1; printf("abignumber+one = %d \n",abignumber+one); } return EXIT_SUCCESS;

ordinal data types The ordinal data types in C can be either signed or unsigned. C gives the programmer the following ordinal data types char, short int, long int each can be pre-fixed with the keyword unsigned

ordinal data types Data type Description char short int int long int Small data type only needs 1 byte / 8 bits of memory to store. Integer data type half the size of an integer Integer data type, size dependent upon platform using it Integer data type twice the size of the int data type

sizeof() In C/C++ sizeof is a unary operator that must be implemented by the developer of the compiler it appears as a C/C++ function when we use it but will return the size in bytes of the data type passed to it. The following program demonstrates sizeof

#include <stdio.h> #include <stdlib.h> int main() { printf("sizeof(char)= %ld \n",sizeof(char)); printf("sizeof(short int)= %ld \n",sizeof(short int)); printf("sizeof(int)= %ld \n",sizeof(int)); printf("sizeof(long int)= %ld \n",sizeof(long int)); printf("unsigned versions\n"); printf("sizeof(unsigned char)= %ld \n",sizeof(unsigned char)); printf("sizeof(unsigned short int)= %ld \n",sizeof(unsigned short int)); printf("sizeof(unsigned int)= %ld \n",sizeof(unsigned int)); printf("sizeof(unsigned long int)= %ld \n",sizeof(unsigned long int)); } return EXIT_SUCCESS; sizeof(char)= 1 sizeof(short int)= 2 sizeof(int)= 4 sizeof(long int)= 8 unsigned versions sizeof(unsigned char)= 1 sizeof(unsigned short int)= 2 sizeof(unsigned int)= 4 sizeof(unsigned long int)= 8

char The char data type is useful for representing ASCII characters It usually takes up 1 byte and can represent either 0 to +255 (unsigned) or -128 to +128 (signed) Whilst this is used to store numeric values we can use the convenience single quote method to assign a char from a character as shown in the next program

#include <stdio.h> #include <stdlib.h> int main() { char a='z'; printf("%c \n",a); a=42; printf("%c \n",a); } return EXIT_SUCCESS; http://upload.wikimedia.org/wikipedia/commons/4/4f/ascii_code_chart.svg

real numbers In computing we use floating point data types to represent real numbers (numbers with a fractional part) These numbers are always approximations as we have to move the decimal. Numbers are, in general, represented approximately to a fixed number of significant digits and scaled using an exponent. Significant digits base exponent

real numbers in C C has two real data types float and double the long prefix may be used with double to increase the precision Type Specifiers Precision (decimal digits) Exponent range Minimum IEEE 754 Minimum IEEE 754 float 6 7.2(24 bits) ±37 ±38 (8 bits) double 10 15.9(53 bits) ±37 ±307(11 bits) long double 10 34.0(113 bits) ±37 ±4931 (15 bits)

#include <stdio.h> #include <stdlib.h> int main() { } float a=2.5; double b=0.00000034; long double c=123213213.343433434320; printf("%f \n",a); printf("%lf \n",b); printf("%lf \n",c); return EXIT_SUCCESS; 2.500000 0.000000 123213213.343433 Note truncated printf output need to use %n.nf and specify decimal places to print e.g. %.8lf

Arithmetic expressions Most programs are algorithmic in nature which means we have to do some maths The table below shows the available arithmetic operators Operator Meaning Examples + addition - subtraction * multiplication / division 5 + 2 is 7 5.0 + 2.0 is 7.0 5-2 is 3 5.0-2.0 is 3.0 5*2 is 10 5.0*2.0=10.0 5/2 is 2 5.0/2.0 is 2.5 % remainder (modulus) 5%2 is 1

The / Operator When applied to two positive integers the division operator computes the integral part of the result dividing its first operand by its second For example 7.0 / 2.0 is 3.5 7 / 2 is 3 299.0 / 100.0 is 2.99 (double value) 299 / 100 is 2 (integer value) If the / Operator is used with a negative and positive integer, the results vary from one C implementation to another For this reason you should avoid division by -ve integers

More on / It is also important not to do division by 0 as the program may crash, some modern compilers will try to warn of #include <stdio.h> #include <stdlib.h> int main() { printf("3/15 %d\n",3/15); printf("15/3 %d\n",15/3); printf("16/3 %d\n",16/3); printf("17/3 %d\n",17/3); printf("18/3 %d\n",18/3); printf("16/-3 %d\n",16/-3); printf("0/4 %d\n",0/4); printf("4/0 %d\n",4/0); } return EXIT_SUCCESS; this as seen with the program opposite

The % (modulus) Operator The remainder operator (%) returns the integer remainder of the result of dividing the first operand with the second For example the value of 7 % 2 is 1 The magnitude of m % n must always be lest than the division n 7/2 299/100 # # 7 2 = 3 299 100 = 2 3 2=6 2 100 = 200 6 7 6 7%2=1 200 299 200 = 299 % 100 = 99

#include <stdio.h> #include <stdlib.h> int main() { printf("3 %% 5=%d\n",3%5); printf("5 %% 3=%d\n",5%3); printf("4 %% 5=%d\n",4%5); printf("5 %% 4=%d\n",5%4); printf("5 %% 5=%d\n",5%5); printf("15 %% 5=%d\n",15%5); printf("6 %% 5=%d\n",6%5); printf("15 %% 6=%d\n",15%6); printf("7 %% 5=%d\n",7%5); printf("15 %% -7=%d\n",15%-7); printf("8 %% 5=%d\n",8%5); printf("15 %% 0=%d\n",15%0); } return EXIT_SUCCESS;

Data type of an expression There are certain rules to define the results of mixing data types For example int a=10; int b=23; int c; c=a+b; // will result in a integer value However if we mix the types we will get different results depending upon the receiving variables data type double x; int n; x = 9 * 0.5; // will result in x = 4.5 n = 9 * 0.5; // will result in n = 4

Expressions with Multiple Operators There are rules as to how expressions are evaluated Parentheses Rule : All expressions in parentheses must be evaluated separately. Nested parenthesised expressions must be evaluated from the inside out, with the innermost expression evaluated first. Operator precedence rule : Operators in the same expression are evaluated in the following order. unary +, - first *, /, % next binary +,- last

Expressions with Multiple Operators Associativity Rule : Unary operators in the same subexpression and at the same precedence levels (such as + and -) are evaluated right to left. Binary operators in the same sub-expression and the same precedence level (such as + and -) are evaluated left to right. To help avoid problems with the order of evaluation it is best to use parenthesis x * y * z + a / b -c * d; can be written (x * y * z) + (a / b) - (c * d);

Mathematical Formulas as C/C++ expressions Mathematical Formula C Expression b 2 4ac b*b-4*a*c a + b c a+b-c a+b c+d (a + b) / (c + d) 1 1+x 2 1 / (1 + x * x ) a (b + c) a * -(b + c) Notice that C has no equivalent to x 2 so we have to evaluate it as x * x If any other power is required the pow(double x, double y) function must be used to evaluate x y

References Cornelia M. Yoder and Marilyn L. Schrag. 1978. Nassi-Shneiderman charts an alternative to flowcharts for design. SIGSOFT Softw. Eng. Notes 3, 5 (January 1978), 79-86. I. Nassi and B. Shneiderman. 1973. Flowchart techniques for structured programming. SIGPLAN Not. 8, 8 (August 1973), 12-26. Anderson Eike F. 2002-2005 Computer Programming in ANCI C Lecture notes NCCA Sebesta R. W. 2002, Concepts of Programming Languages, 5th edition, Addison Wesley, International Ed Daniel D. McCracken and Edwin D. Reilly. 2003. Backus-Naur form (BNF). In Encyclopedia of Computer Science (4th ed.), Anthony Ralston, Edwin D. Reilly, and David Hemmendinger (Eds.). John Wiley and Sons Ltd., Chichester, UK 129-131. Hanly. J. R Koffman E. B. 1999, Problem Solving & Program Design in C, 3rd Edition, Addison Wesley, International Ed

Further reading http://en.wikipedia.org/wiki/noam_chomsky http://en.wikipedia.org/wiki/backus%e2%80%93naur_form http://en.wikipedia.org/wiki/metalanguage http://en.wikipedia.org/wiki/extended_backus%e2%80%93naur_form http://en.wikipedia.org/wiki/floating_point http://en.wikipedia.org/wiki/c_syntax