CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.

Slides:



Advertisements
Similar presentations
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Advertisements

Control Structures Selections Repetitions/iterations
More on Algorithms and Problem Solving
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Chapter 1 - An Introduction to Computers and Problem Solving
 Control structures  Algorithm & flowchart  If statements  While statements.
ITEC113 Algorithms and Programming Techniques
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
Introduction to Computers and Programming Lecture 5 New York University.
Program Design and Development
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Programming Fundamentals (750113) Ch1. Problem Solving
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
Chapter 1 Program Design
Chapter 2: Algorithm Discovery and Design
1 Chapter 2 Problem Solving Techniques INTRODUCTION 2.2 PROBLEM SOLVING 2.3 USING COMPUTERS IN PROBLEM SOLVING : THE SOFTWARE DEVELOPMENT METHOD.
PRE-PROGRAMMING PHASE
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Chapter 2: Problem Solving
Simple Program Design Third Edition A Step-by-Step Approach
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
TMF1013 : Introduction To Computing Lecture 1 : Fundamental of Computer ComputerFoudamentals.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Algorithm Design.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Program design Program Design Process has 2 phases:
ALGORITHMS AND FLOWCHARTS
Algorithms and Flowcharts
Engineering Problem Solving with C++, Etter/Ingber
Lecture 2 Introduction to Programming
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
Programming Fundamentals
Unit# 9: Computer Program Development
Chapter 3: Program Statements
MSIS 655 Advanced Business Applications Programming
Structured Program
` Structured Programming & Flowchart
Flowcharts and Pseudo Code
Structural Program Development: If, If-Else
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow

How to solve problems via programming In this class we are learning how to solve problems by writing computer (in particular C) programs Even for simple problems such as: –“given two numbers, print the larger one” –“given a set of numbers, compute their sum” producing the program that achieves the given task requires some intermediate steps

How to solve problems via programming “given two numbers, print the larger one” To write a program that computes this task we need to answer questions such as: How will the input numbers given? How are we going to print the output? –These are the questions about the specification of the problem (this is called requirements specification) How will we compute the sum? –This is a question about how we can solve the problem using a computer –It is about algorithm design

The process suggested in the Textbook 1.Problem statement –start with clearly defining the problem to solve 2.Input/output description –treat solution like a “black box” – focus on what goes in and what comes out 3.Work the problem by hand on an example –two purposes: (a) verify you know exactly what to do; (b) have known results to check solution later 4.Algorithm development and implementation –includes designing the solution and implementing it 5.Testing –making sure solution works correctly

The process suggested in the Textbook 1.Problem statement 2.Input/output description 3.Work the problem by hand on an example 4.Algorithm development and implementation 5.Testing Steps 1, 2 and 3 are all called requirements analysis. Step 4 consists of software design and implementation. Step 5 is for verification and validation and it is necessary for catching and removing bugs in both our algorithm development and implementation.

Modern software development Software development for large and complex software projects requires more careful thinking about software development process Modern software development practices include –Iterative (agile) software development: A program is developed incrementally by starting from the most basic functionality and then gradually adding new functionality –Test driven software development: Before writing the program a set of test cases are identified and the program development is guided by tests –Object oriented design: Designing software in a way that encapsulates the data and the computation together

What is algorithm? To solve a programming problem we first need to develop an algorithm. –But, what is an algorithm? An algorithm is like a recipe –A recipe is a step by step description of how to convert some ingredients to a dish you can eat –An algorithm is a step by step description of how to convert a given input to a computer to an output This is a very hard question to answer

What is analgorithm? An algorithm is like a recipe –A recipe is a step by step description of how to convert some ingredients to a dish you can eat –An algorithm is a step by step description of how to convert a given input to a computer to the desired output If you write a program that implements the algorithm, then the implementation (the program) will produce the desired output for the given inputs

How do you come up with an algorithm? This is a very hard question to answer Algorithm design is a creative activity and like any creative activity it cannot be converted to a step-by-step description –We do not have an algorithm for algorithm design! Computer scientists developed many algorithms for many general problems, so knowing them helps Computer scientists also developed some general strategies for algorithm design that can be used to solve different problems –One general strategy: Stepwise refinement

Stepwise refinement for software development Stepwise refinement is a divide and conquer approach 1.Clearly state the intended task 2.Divide the task to a set of subtasks and re-express the intended task as an equivalent structure of properly connected subtasks, each solving part of the problem 3.Divide each subtasks far enough until the complexity of each subtask is manageable (i.e., you know how to write a program segment for that subtask) So, if we do algorithm development using stepwise refinement, then the resulting algorithm is a collection of subtasks

How do we write the algorithm we designed? We can do this in two basic ways: –We can write pseudocode It is description of an algorithm that looks like a code but it is not in any programming language It is written for humans and it is not machine readable There is no standard way of writing pseudocode –We can use flowcharts Flowcharts are an almost standard block diagram representation for computational tasks

How are the tasks in an algorithm combined? They can be combined as a sequence: –first execute task1, then execute task2 They can be combined using selection: –check a condition, if the condition is true execute task1, if not execute task 2 They can be combined using repetition (aka iteration): –repeat task1 until a condition becomes true Structured programming languages (like C) have language constructs for composing tasks using sequence, selection and repetition –We call these control structures since they control the execution order of the statements

C has 7 basic control structures 1 st is trivial: sequence structure: statements listed sequentially are executed sequentially a = 4; b = 5; 3 choices of selection structures: –if –if/else –switch 3 choices of repetition structures: –while –for –do/while

Sequence in flowchart

if selection structure as a flowchart

if statement syntax Either if (Boolean expression) statement; Or if (Boolean expression) { statement1; statement2; … } Boolean (aka conditional) expression: –evaluates to 1 (true) or 0 (false) Note the indentation to help readability of the code

Relational Operators

Simple Boolean expressions, relational operators Relational operators:, =, ==, != –e.g., int x=1, y=2, z=3; x > y /* false */ x >= z - y /* true */ z != x + y /* false */ Note precedence: relational operators have lower precedence than arithmetic operators x == z + y /* false */ Note, not same as x = z + y /* makes x be 5 */

Boolean (aka logical) operators: &&, ||, ! Boolean (aka logical) operators are used for combining simple Boolean expressions into more complex expressions –&& means logical and –|| means logical or –! means logical negation –Operands are Boolean expressions e.g., grade == ‘ A ’ && weight > 10 –Note precedence: relational operators have higher precedence than Boolean operators Rules (see Table 3.1, p. 89, next page) –op1 && op2 - true if both operands are true –op1 || op2 - true if either operand is true –!op - true if operand is false Note precedence: ! then && then ||

Boolean (logical) operators

Updated precedence summary

if/else Selection Structure

if/else if (Boolean expression) { statements; } else { statements; } Example: if (grade >= 60) printf("%s", "Pass"); else printf("%s", "Fail"); –See …/demos/richter.c …/demos/richter.c

Conditional operator Conditional operator can be used in place of simple if then else statements Example: if (a < b) count++; else c = a+b; equivalent code with conditional operator a<b ? count++ : c = a + b; Conditional operator does not allow block or nesting but returns a value printf("%s",(grade>=60 ? "Pass" : "Fail"));