Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review."— Presentation transcript:

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

2 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

3 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

4 C’s 7 control structures as flowcharts Iteration Selection Sequence

5 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)

6 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

7 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

8 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

9 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

10 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

11 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, …

12 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

13 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

14 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 */

15 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: +8.000 y = x / z; printf(“%-+10.3f”, y); prints: +8.333

16 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

17 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

18 Practice problem Determine if the given expressions evaluate to true or false float a = 5.5; float b = 1.5; float k = -3; a < 10.0 + 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 */

19 Practice problem Fully parenthesize the expressions based on the evaluation order a < 10.0 + 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

20 Practice problem Parenthesized version a < (10.0 + 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))

21 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 */

22 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

23 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


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

Similar presentations


Ads by Google