Fundamental of Programming (C)

Similar documents
Fundamentals of Programming

Introduction to Flowcharting

Control Structure: Loop

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting

Structured Program Development

Fundamental of Programming (C)

CS 199 Computer Programming. Spring 2018 Lecture 2 Problem Solving

Introduction. C provides two styles of flow control:

Fundamentals of Programming. Lecture 6: Structured Development (part one)

Chapter 3 Structured Program Development

Introduction to Programming

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Theory of control structures

Chapter 2 - Control Structures

Introduction to C Programming

BIL101E: Introduction to Computers and Information systems Lecture 8

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Concept of algorithms Understand and use three tools to represent algorithms: Flowchart Pseudocode Programs

BRANCHING if-else statements

Fundamentals of Programming

LECTURE 5 Control Structures Part 2

Decision Making and Loops

Chapter 2 - Control Structures

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Chapter 2 - Control Structures

Fundamental of Programming (C)

Module 4: Decision-making and forming loops

Structured Program Development in C

Outline. Program development cycle. Algorithms development and representation. Examples.

Decision Making -Branching. Class Incharge: S. Sasirekha

Computer System and programming in C

FLOW CHART AND PSEUDO CODE

Lecture 6. Statements

Computers Programming Course 7. Iulian Năstac

Chapter 5: Control Structures

Chapter Two: Program Design Process and Logic

Chapter 13 Control Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

REPETITION CONTROL STRUCTURE LOGO

3 The L oop Control Structure

A control structure refers to the way in which the Programmer specifies the order of executing the statements

CS1100 Introduction to Programming

UIC. C Programming Primer. Bharathidasan University

C: How to Program. Week /Mar/05

Flow Chart & Algorithms

Chapter 13. Control Structures

Week 1 Introduction to Computer and Algorithm (Part1) UniMAP Sem II 11/12 DKT121: Basic Computer Programming 1

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

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

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

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Chapter 2 - Introduction to C Programming

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Name Roll No. Section

Comments. Comments: /* This is a comment */

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

Fundamental of Programming (C)

Lab Session # 3 Conditional Statements. ALQUDS University Department of Computer Engineering

SELECTION STATEMENTS:

Score score < score < score < 65 Score < 50

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

IT 1033: Fundamentals of Programming Loops

Course Outline Introduction to C-Programming

Unit 7. Functions. Need of User Defined Functions

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Chapter 13 Control Structures

Question Bank (SPA SEM II)

Lecture 10. Daily Puzzle

Lecture 5 Tao Wang 1

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

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Repetition Algorithms

DECISION CONTROL AND LOOPING STATEMENTS

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Repetition Structures II

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Programming for Engineers Iteration

Control Statements. If Statement if statement tests a particular condition

EECE.2160: ECE Application Programming Spring 2018

The four laws of programs

PSEUDOCODE AND FLOWCHARTS. Introduction to Programming

1 EEE1008 C Programming

Lecture 5: C programming

4*4*4 2. What is the output of the following flowchart for the values given below: (25 p)

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

CpSc 1111 Lab 4 Formatting and Flow Control

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

ALGORITHMS AND FLOWCHARTS

Chapter 1: Problem Solving Skills Introduction to Programming GENG 200

Transcription:

Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 5 Structured Program Development Department of Computer Engineering

How to develop a program? Requirements Problem Analysis Refinement Design Designing algorithm Pseudo code Flow chart Implementation Implementing algorithm in c/c++ Validation Maintenance Test Software Development Life-Cycle Department of Computer Engineering 2/65

Department of Computer Engineering 3/65

Requirements Discovery Problem Analysis Problem Identification Abstraction Inputs and outputs determination Defining their relation Write a program to compute an employee's weekly pay? How many hours did you work? How much do you get paid per hour? employee's weekly pay equal to multiplication of Hours by Pay Rate Department of Computer Engineering 4/65

Problem vs. Solution A problem is something that causes trouble The solution is how to solve or fixe the problem Department of Computer Engineering 5/65

Algorithm A procedure for solving a problem in terms of actions and the order in which these actions are to be executed in a computer program (program control) Action Order Department of Computer Engineering 6/65

Execution order Sequential execution, normally, statements in a program are executed one after the other in the order in which they re written Various C statements enable you to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control Department of Computer Engineering 7/65

Structured Programming goto statement that allows programmers to specify a transfer of control to one of many possible destinations in a program Research had demonstrated that programs could be written without any goto statements Programs produced with structured techniques were clearer, easier to debug and modify and more likely to be bug free in the first place. Research had demonstrated that all programs could be written in terms of only three control structures, namely the sequence structure, the selection structure and the repetition structure Department of Computer Engineering 8/65

Pseudo code An artificial and informal language that helps you develop algorithms similar to everyday English are not executed on computers consists only of action statements pay-calculation program: 1. Read Hours and Pay Rate 2. weekly pay = Read Hours * Pay Rate Department of Computer Engineering 9/65

Flowchart A flowchart is a diagram that depicts the flow of an algorithm pay-calculation program START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Each symbol represents a different type of operation Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 10/65

Terminals represented by rounded rectangles indicate a starting or ending point START Terminal START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate END Multiply Hours by Pay Rate. Store result in Gross Pay. Flow line Display Gross Pay Terminal END Department of Computer Engineering 11/65

Input/Output Operations indicate an input or output operation START Display message How many hours did you work? Display message How many hours did you work? Input/Output Operation Read Hours Display message How much do you get paid per hour? Read Pay Rate Read Hours Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 12/65

Processes indicates a process such as a mathematical computation or variable assignment Multiply Hours by Pay Rate. Store result in Gross Pay. Process START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 13/65

Execution START Display message How many hours did you work? Output Operation How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours:? Pay Rate:? Gross Pay:? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 14/65

Execution How many hours did you work? 40 Input Operation (User types 40) START Display message How many hours did you work? Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours: 40 Pay Rate:? Gross Pay:? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 15/65

Execution START Display message How many hours did you work? How much do you get paid per hour? Read Hours Display message How much do you get paid per hour? Output Operation Read Pay Rate Variable Contents: Hours: 40 Pay Rate:? Gross Pay:? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 16/65

Execution START Display message How many hours did you work? How many hours did you work? 20 Read Hours Display message How much do you get paid per hour? Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay:? Input Operation (User types 20) Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 17/65

Execution START Display message How many hours did you work? How many hours did you work? 20 Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Process (Gross Pay = Hours * Pay Rate) Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 18/65

Execution START Display message How many hours did you work? Your gross pay is 800 Read Hours Display message How much do you get paid per hour? Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 20 Gross Pay: 800 Output Operation Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END Department of Computer Engineering 19/65

Connectors Sometimes a flowchart will not fit on one page A connector (represented by a small circle) allows you to connect two flowchart segments START A END A Department of Computer Engineering 20/65

Modules A program module (such as a function in C) is represented by a special symbol START The position of the module symbol indicates the point the module is executed module Read Input Call calc_pay function A separate flowchart can be constructed for the module Department of Computer Engineering 21/65 Display results END

Control Structures Sequence Decision selection statement The if statement is called a single-selection statement because it selects or ignores a single action. The if else statement is called a double-selection statement because it selects between two different actions. The switch statement is called a multiple-selection statement because it selects among many different actions Repetition while do while for Department of Computer Engineering 22/65

Sequence Structure a series of actions are performed in sequence The pay-calculating example Department of Computer Engineering 23/65

Compound Statements A statement is a specification of an action to be taken by the computer as the program executes Compound Statements is a list of statements enclosed in braces, { } Department of Computer Engineering 24/65

Decision Structure One of two possible actions is taken, depending on a condition Selection structures are used to choose among alternative courses of action NO YES NO x < y? YES Process A Process B Department of Computer Engineering 25/65

Decision Structure The flowchart segment below shows how a decision structure is expressed in C as an if/else statement Flowchart C programming language NO x < y? YES if (x < y) a = x * 2; Calculate a as x plus y. Calculate a as x times 2. else a = x + y; Department of Computer Engineering 26/65

Example Compound statement is way to group many statements together so they are treated as one if (grade >= 60) printf( "Passed.\n" ); // { printf( "Passed.\n" ); } else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } Department of Computer Engineering 27/65

Decision Structure The flowchart segment below shows a decision structure with only one action to perform Flowchart C programming language NO x < y? YES if (x < y) a = x * 2; Calculate a as x times 2. Department of Computer Engineering 28/65

Combining Structures NO x > min? YES if (x > min) { if (x < max) printf("x is within the limits"); else printf("x is outside the limits"); } else printf("x is outside the limits"); Display x is outside the limits. NO Display x is outside the limits. x < max? YES Display x is within limits. Department of Computer Engineering 29/65

Example int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k; printf("%d", m); } else { k = 1; printf("%d", k); } int k = 1, m = 4; if (k < 2 m == 3) { m = 2 + k, printf("%d", m); } else { k = 1, printf("%d", k); } Which is more readable? Department of Computer Engineering 30/65

Example if(x) if(y) printf("yes"); else printf("no"); if (x < 0) sign = -1; else if (x == 0) sign = 0; else sign = 1; if(x) { if(y) printf("yes"); else printf("no"); } if (x < 0.25) count1++; else if (x >= 0.25 && x < 0.5) count2++; else if (x >= 0.5 && x < 0.75) count3++; else count4++; if(x) { if(y) printf("yes"); } else printf("no"); Department of Computer Engineering 31/65

Case Structure One of several possible actions is taken, depending on the contents of a variable Department of Computer Engineering 32/65

Case Structure indicates actions to perform depending on the value in years_employed If years_employed = 1, bonus is set to 100 If years_employed = 2, bonus is set to 200 CASE years_employed If years_employed = 3, bonus is set to 400 If years_employed is any other value, bonus is set to 800 1 2 3 Other bonus = 100 bonus = 200 bonus = 400 bonus = 800 Department of Computer Engineering 33/65

switch A switch statement allows a single variable (integer or char) to be compared with several possible constants A constant can not appear more than once, and there can only be one default expression Department of Computer Engineering 34/65

switch switch (variable) { case const: statements...; default: statements...; } switch (c = toupper(getch())) { case R : printf("red"); break; case G : printf("green"); break; default: printf("other"); } Department of Computer Engineering 35/65

Example switch(betty) { case 1: printf("betty = 1\n"); case 2: printf("betty=2\n"); break; case 3: printf("betty=3\n"); break; default: printf("not sure\n"); } CASE betty? 1 2 3 Other betty = 1 betty = 2 betty = 3 Not sure Department of Computer Engineering 36/65

Repetition Structure A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists x < y? YES Process A Department of Computer Engineering 37/65

Repetition Structure The flowchart segment below shows a repetition structure expressed in C as a while loop Flowchart C programming language while (x < y) x < y? YES Add 1 to x x++; Department of Computer Engineering 38/65

While while (loop_repetition_condition) statement; OR //Compound statement while (loop_repetition_condition) { statement1; statement2; // } Department of Computer Engineering 39/65

Controlling a Repetition Structure The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created In this flowchart segment, x is never changed. Once the loop starts, it will never end How can this flowchart be modified so it is no longer an infinite loop? x < y? YES Display x Department of Computer Engineering 40/65

Controlling a Repetition Structure Adding an action within the repetition that changes the value of x YES x < y? Display x Add 1 to x Department of Computer Engineering 41/65

A Pre-Test Repetition Structure This type of structure is known as a pre-test repetition structure. The condition is tested BEFORE any actions are performed if the condition does not exist, the loop will never begin YES x < y? Display x Add 1 to x Department of Computer Engineering 42/65

Example while (1); int counter = 0; while (counter < 1000) ; int counter = 0; while (counter < 9) printf("%d\n", counter ++); int counter = 9; while (counter > 0) printf("%d\n", counter --); int counter = 0; while (counter < 9) { printf("%d\n", counter); counter++; } int counter = 0; while (counter < 9) { printf("%d\n", counter ++); } Department of Computer Engineering 43/65

A Post-Test Repetition Structure The condition is tested AFTER the actions are performed A post-test repetition structure always performs its actions at least once C programming language Display x do { printf( ); x++; } while (x < y); Add 1 to x x < y? YES Department of Computer Engineering 44/65

do-while do statement; while (loop_repetition_condition) OR do //Compound statement { statement1; statement2; // } while (loop_repetition_condition) Department of Computer Engineering 45/65

Example Draw a flowchart for the following problem: Read 5 integer and display the value of their summation. Can you identify the input and output? Input : 5 integer n1, n2, n3, n4, n5 Output: The summation of n1, n2,.., n5 Input example: 2 3 4 5 6 Output example: 20 Department of Computer Engineering 46/65

Assume input example: 2 3 4 5 6 start Input n1 n1 2 This flowchart does not use loop, hence we need to use 6 different variables Input n2 Input n3 input n4 n2 n3 3 4 input n5 n4 5 sum n1+n2+n3+n4+n5 n5 6 output sum sum 20 end Department of Computer Engineering 47/65

Assume input example: 2 3 4 5 6 counter 1, sum 0 counter 12 34 56 sum 02 59 14 20 14 02 59 + 23 45 6 counter < 6 true input n sum sum + n counter++ output sum false 61 23 54 < < 6 6 false true n 23 45 6 The This Uses counter loop only is counter-controlled Increases 3 variables by 1 Department of Computer Engineering 48/65

Decreasing Counter-Controlled Loop counter 5, sum 0 false counter > 0 true input x sum sum+ x counter-- output sum Department of Computer Engineering 49/65

For for (initial_value ; condition; update_counter) statement; OR // Compound statement for (initial_value ; condition; update_counter) { statement; statement; // } Department of Computer Engineering 50/65

counter 1, sum 0 counter < 6 true input n sum sum + n counter++ output sum false int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf( %d,&x); sum = sum + x; } printf( %d,sum); Department of Computer Engineering 51/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n );??? num _ Department of Computer Engineering 52/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num _ Department of Computer Engineering 53/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num _ Department of Computer Engineering 54/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 1 num 1 _ Department of Computer Engineering 55/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 _ Department of Computer Engineering 56/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 _ Department of Computer Engineering 57/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 2 num 1 2 _ Department of Computer Engineering 58/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 _ Department of Computer Engineering 59/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 _ Department of Computer Engineering 60/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 3 num 1 2 3 _ Department of Computer Engineering 61/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num 1 2 3 _ Department of Computer Engineering 62/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num 1 2 3 _ Department of Computer Engineering 63/65

Example: for (num = 1; num <= 3; num++ ) printf( %d\t, num); printf( have come to exit\n ); 4 num 1 2 3 have come to exit_ Department of Computer Engineering 64/65

Example * ** *** **** ***** ****** #include <stdio.h> int main() { int icounter = 0; int jcounter = 0; while (icounter < 7) { jcounter = 0; while (jcounter < icounter) { printf("*"); jcounter++; } printf("\n"); icounter++; } getch(); return 0; } Department of Computer Engineering 65/65