CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

More on Algorithms and Problem Solving
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 3 - Structured Program Development
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Program Design and Development
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Chapter 8 . Sequence Control
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
Structured Program Development in C
Lecture 3 Structured Program Development in C
Lecture 4 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CIS Computer Programming Logic
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
C Lecture Notes 1 Structured Program Development.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control.
C Programming 2002 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection.
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.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 3 Structured Program Development Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Decision Making and Branching (cont.)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
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.
Chapter 4 – C Program Control
CprE 185: Intro to Problem Solving (using C)
Engineering Problem Solving with C++, Etter/Ingber
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Programming Fundamentals
Outline Altering flow of control Boolean expressions
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
Chapter 3 - Structured Program Development
Computer Science Core Concepts
EPSII 59:006 Spring 2004.
Chapter 4 - Program Control
Structural Program Development: If, If-Else
Presentation transcript:

CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review

How are the subtasks 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 Sequence structure: statements listed sequentially are executed sequentially a = 4; b = 5; 3 selection structures: –if –if/else –switch 3 repetition (loop) structures: –while –for –do/while

C’s 7 control structures as flowcharts Iteration Selection Sequence

Recall: Stepwise Refinement 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)

Rule 1: start with the simplest flowchart One rectangle A good (and widely applicable) example: get some data, calculate and show some results Really just a way to start; clarifies the “big picture” Very general; top-level algorithm

Rule #2: Sequential decomposition Replace a rectangle with two rectangles in sequence This “stacking rule” can apply repeatedly: one  two, two  three, … For example: 1.Get data 2.Process 3.Show results Rule 2

Rule #3: Nesting This “nesting rule” also applies repeatedly, as each control structure has rectangles e.g., nest a while loop in an if structure: if (n > 0) while (i < n) printf("%d ", i++); Rule 3 Any one of 7 choices in C

Rule #4: apply #2 or #3 repeatedly Stack, nest, stack, nest, nest, stack, … gets more and more detailed as one proceeds –Think of control structures as building blocks that can be combined in two ways only. –Captures the essence of stepwise refinement: keep adding details as they arise Means add control structures as long as they are needed Top-down design: start with forest, do trees later

What did we learn so far? We covered the first 3 chapters of the book: –General concepts about computers –Basic concepts about C programs: basic types, assignment, input-output, etc. –C control structures, stepwise refinement Let’s do a review and some practice problems

Chapter 1: General concepts about computers Basic architecture Basic concepts about computers: –what is: hardware, software, operating system, compiler, assembly language, programming language, source code, object code, software life cycle, …

Practice problem Match the following words and definitions Compiler, ALU, operating system kernel, processor, machine language 1.The part of a computer that controls all the other parts 2.A software component that manages the interface between the hardware and the software applications 3.The part of a computer that performs mathematical computations 4.A program that translates programs written in high level language to a low level language

Chapter 2: Basic C concepts Basic C concepts: –representation of numeric values, different base representations –representation of characters, ASCII code –basic types, type-casting –variable declarations, identifiers –overflow, underflow –evaluation of numeric expressions (precedence, associativity) –assignment statement –input/output with printf and scanf

Practice problem Is there a syntax error in the following declarations, and what is it? int x_5;/* OK */ int _y;/* OK */ int 2y;/* syntax error */ int y2; /* OK */ float/* something */ z,y = 4 ;/* OK */ fl/* something */oat x; /* syntax error */ float int1;/* OK */

Practice problem What will be printed? int x = 25; float y; float z = 3.0; printf(“%x”,x); prints: 19 printf(“%o”,x); prints: 31 printf(“%d”,x); prints: 25 y = x / 3; printf(“%+10.3f”, y); prints: y = x / z; printf(“%-+10.3f”, y); prints:

Practice problem What will be printed? int x; x = 12; printf(“%d”, x); prints: 12 x = 012; printf(“%d”, x); prints: 10 x = 0x12; printf(“%d”, x); prints: 18

Chapter 3: Structured programming, control flow Relational operators, relational expressions, logical operators, logical expressions Control flow structures in C –if, if/else, switch, while, for, do/while –Use of { and } to group statements into a block Stepwise refinement –Top-down approach for solving a programming problem

Practice problem Determine if the given expressions evaluate to true or false float a = 5.5; float b = 1.5; float k = -3; a < k/* true */ a + b >= 6.5/* true */ k != a – b/* true */ b – k > a/* false */ ! ( a == 3 * b)/* true */ -k <= k + 6;/* true */ a 5/* true */ fabs(k) > 3 || k < b – a/* false */

Practice problem Fully parenthesize the expressions based on the evaluation order a < k a + b >= 6.5 k != a – b b – k > a ! ( a == 3 * b) -k <= k + 6; a 5 fabs(k) > 3 || k < b - a

Practice problem Parenthesized version a < ( k) (a + b) >= 6.5 k != (a – b) (b – k) > a ! (a == (3 * b)) (-k) <= (k + 6); (a 5) (fabs(k) > 3) || (k < (b – a))

Practice problem Determine the number of times the following for loops are executed for (k = 3; k <= 10; k++) { … }/* 8 times */ for (k = 3; k <= 10; ++k) { … }/* 8 times */ for (k = -4; k <= 14; k += 2) { … } /* 10 times */

Practice problems What is the value of count after the nested for loops are executed? int k, j, count = 0; for (k = -1; k <= 3; k++) for (j = 3; j >= 1; j--) count++; Outer loop executes 5 times Inner loops executes 3 times for each execution of the outer loop So, count++ is executed 5×3 = 15 times At the end the value of count is 15

Practice problems Write program segments which do the following: Compute the sum from 1 to n for a given n Compute the maximum value among 3 numbers Compute the maximum value among n numbers Sort three numbers