Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS

Slides:



Advertisements
Similar presentations
An introduction to pointers in c
Advertisements

Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Bowen Yu Programming Practice Midterm, 7/30/2013.
Kernighan/Ritchie: Kelley/Pohl:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 5. Program Structure IV - 1
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
ACSE th Conference The Iconic Programmer Stephen Chen.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Art 315 Lecture 6 Dr. J. Parker. Variables Variables are one of a few key concepts in programming that must be understood. Many engineering/cs students.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Loops and Iteration for Statements, while Statements and do-while Statements.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Looping and Counting Lecture 3 Hartmut Kaiser
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 5: Structured Programming
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
CPS120: Introduction to Computer Science Decision Making in Programs.
Controlling Program Flow with Decision Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
How to Quickly Read a Program? A/Prof Anthony Tung (邓锦浩) Department of Computer Science School of Computing NUS.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Chapter 4 – C Program Control
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4 - Program Control
Lecture 2 Introduction to Programming
User-Defined Functions
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 4 - Program Control
Functions Pass By Value Pass by Reference
Chapter 7: User-Defined Functions II
Presentation transcript:

Program Design (or Why C is really not difficult?) A/Prof Anthony Tung Department of Computer Science School of Computing NUS

Gist of the Problem Anything written in any languages involve two components: – Grammar( 语法) – Content (内容) Many are learning the grammar of programming languages without knowing how to come out with the content. That’s like trying to write poetry without knowing what you want to write about. So what if you know all the grammatical rules? The slides here aim to give some explanation on how the “content” are generated or more correctly designed in computing

Components of Programming Languages Operators/Input/Output: These are single statements that either modify the values stored in the variables or output them Memory allocation: These are statements that allocate spaces for storing variables and declare how these variables should be interpreted (eg int, char[], malloc) Control Statements: Control the flow of programs. Two main ones: if-then-else, loop Functions: We can do without functions but things will be much more tidy and manageable with them. We talk about functions later.

Plan Your Program ! To program effectively, it make sense to have a feel or overview of your program. Typically, this involve the following three steps i) Plan your control flow statements and their scope – How many loops? Are they nested within each other? Are they within or outside the scope of if-then-else statements? – How many if-then-else should there be? What scope should they be within ? What should be within their scopes? ii) What variables should there be? This includes – (a) control variables for control statements, – (b) variables for storing input/output/intermediate result iii) What operators should be used to manipulate the variables and more importantly where should they be within the scopes of the control statements

Scope(I) The scope of a control statement refers to the regions which are control by the statement: if () then { } else { } for () { } while () { } Scope_if Scope_else Scope_forScope_while Disclaimer: We won’t be faithfully following the syntax of C here. Most programming languages apply here anyway.

Scope(II) By arranging the scopes of these control statements, you form the skeleton (initial design) of your program. main() { if () then { } else { } for () { } if () { } Scope_for Scope_if main() { for () { } for () { } if () { } Scope_for Scope_if Example 1Example 2

Scope(III) Once the skeleton is correct, it is a matter of filling up the “meat” i.e. the variables declaration, operators, input, output statements at the correct place within the skeleton main() { for () { } for () { } if () { } Scope_for Scope_if variables declaration and initialization usually here can be here and there as well (variables declared within a scope won’t be recognized outside it) input statements output statements usually here but can be at any place as well condition tests

Scope(IV) 1.Test: Since the skeleton of the program is so important, it make to test the skeleton thoroughly (using printf statement )BEFORE filling in the actual “meat” 2.Indent: It is a good habit to indent the brackets properly so that the scopes (and thus the skeleton) of your control statements are very clear to you 3.Comments: If need be, first add in comments near the control statements to remind yourself why you put these control statements there! 4.Self-contain: Try to make the scopes as self-contained as possible i.e. statements within a scope should NOT affect anything outside the scope unless necessary (eg. for sending out result to outside the scope)

Example Write a program that determine whether an input number N is a prime Step 1: write/think in human/mathematical language how you intend to do it – I will take N and divide it by all those integer starting from 2 that is smaller than it. If I found a number that can divide N exactly, then N is not a prime, else N is a prime Step 2: from the above, try to guess how many loops and if-then-else statements are needed. Here, assume we guess: 1 loop and 1 if-then-else based on previous step

Example(II) Step 3: Try to arrange the 1 loop and 1 if-then- else statement to form the skeleton main() { for () { } if () { } else { } main() { if () { } else { } for () { } Choice 1 Which choice is correct? Choice 2

Example(III) Step 4: Assuming you know choice 1 is correct either through testing or by common sense! Then we need to fill in the “meat” main() { scanf(“%i”,N); for (int i=2;i<N;i++) { } if (N mod i==0) { IsPrime==0 } else { % actually nothing need to be here! } Choice 1 Input N here, need to declare int N input statements output statements condition tests Need to output result, probably need to get result from the if-then-else scope through a variable. Oh, maybe need an additional if-then-else! this loop is to go through 2 to N- 1, need to declare int i for the counter in the loop this “if” is to test whether N is divisible by i, must pass result to output statement, need to declare a boolean variable IsPrime

Example (IV) Step 5: Finally, declare the variables that are needed and add in the if-then-else statement that we never foresee variables declaration and initialization main() { int N; int IsPrime=1; scanf(“%i”,N); for (int i=2;i<N;i++) { } if (IsPrime==0) then printf(“Not prime”) else printf(“prime”) } if (N mod i==0) { IsPrime==0 } Notice how the variable i exists only within the scope of the for-loop since it is not needed outside the scope. Notice how we easily figure out that we need an additional if-then-else statement and place it correctly due to the fact that we are very clear about the skeleton of the program and know where the output logic are.

Equivalent Statements There are many equivalent statements in C that are useful to have but not necessary Example: do-while(), while(), for() can replaced each other Example: if-then-else can replace the switch- case statements Example: break can be avoided altogether Feel free to use them but do not get confuse with the program logic because of them!

Due to lack of time, we will stop here, when I have the time, I will add in slides for Functions and how they can help you to design you program better. Good luck with your checkpoint!