Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.

Slides:



Advertisements
Similar presentations
CS 240 Computer Programming 1
Advertisements

1 1 Eng. Mohamed Eltaher Eng.Ahmed Ibrahim Programming & Flowchart.
CS1010 Programming Methodology
ITEC113 Algorithms and Programming Techniques
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
The Program Design Phases
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Original Source : and Problem and Problem Solving.ppt.
Introduction to Algorithm – part one Jennifer Elmer Form 3 Computing.
Flow Charting. Goals Create Algorithms using Flow Charting procedures. Distinguish between Flow Charting and Pseudocode. Top-Down Design Bottom-up Design.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
A step-by-step procedure for solving a problem in a finite number of steps.
Visual Basic Programming
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
COSC 235: Programming and Problem Solving Ch. 2: Your first programs!!! Instructor: Dr. X.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CS1010 Programming Methodology
Chapter 4 – C Program Control
More on the Selection Structure
REPETITION CONTROL STRUCTURE
Input and Output Upsorn Praphamontripong CS 1110
for Repetition Structures
GC211Data Structure Lecture2 Sara Alhajjam.
Introduction to Programming
Lecture 2 Introduction to Programming
Introduction To Flowcharting
Introduction to Algorithm – part 1
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Microsoft Visual Basic 2005 BASICS
Problem Solving (design of programs) CS140: Introduction to Computing 1 8/26/13.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
TOPIC 4: REPETITION CONTROL STRUCTURE
Unit# 9: Computer Program Development
CS1100 Computational Engineering
Program Design Introduction to Computer Programming By:
How to develop a program?
PROBLEM SOLVING CSC 111.
Introduction to Programming
CS1100 Computational Engineering
T. Jumana Abu Shmais – AOU - Riyadh
Loops CIS 40 – Introduction to Programming in Python
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Introduction to Algorithms and Programming
Algorithm and Ambiguity
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Flowcharts and Pseudo Code
Life is Full of Alternatives
Introduction to Programming
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Tic Tac Toe application
Basic Concepts of Algorithm
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Tic-Tac-Toe Game Engine
JavaScript 101 Lesson 8: Loops.
REPETITION Why Repetition?
Presentation transcript:

Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13

Last Time Computing is more than programming Programming is more than coding If you can’t solve the problem, you can’t write the code Design before code Computers are stupid, you are not Practice, practice, practice Document 2

Going from problem to solution Problems are expressed in terms of values The specific problem is the set of input values The result is the final values computed 3

Steps in programming Conceptual  Computational  Coded problem problem problem design phase implementation phase The better the design, the easier the implementation. 4

Values are stored as “variables” A variable is – a name – for a place – to store a value Called a “Variable” because the value “varies” Value of a variable is sometimes referred to as the “state” of the variable Changing a variable’s value is changing its state 5

Specific problem(s) / General solution Specific Problem 2, 10 5, 9 42, 42 Input values 6 Specific Solution Resulting values General Problem Sum 2 values one general solution

From Initial State to Final State Programs determine the state changes Sequences of statements Selection (branching) Repetition (looping) Subprogram 7

Job 1: Design developing a solution to the problem If you can’t solve the problem, you can’t write the code 8

Procedural Design Representations Flowcharts Activity Diagrams Pseudo code 9 hand-written solutions are good!

Activity Diagram src : 10

Activity Diagram ElementSymbol Start Stop Statements Order of activities 11 start end

What is a statement? Some example statements: – Input, read, get a value – Print, display – Assignment Statement algebra with variables, storing the result in a variable 12

Hello 13 Print “Hello” Hello start end

Sum 2 values 14 Print “enter two values” Input num1, num2 sum = num1 + num2 Print sum enter two values enter two values end start

Activity Diagram ElementSymbol Start Stop Statements Branching Order of activities 15 start end

Branch structure 16 TRUE FALSE condition either or !!

Branching example 17 num1 > num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end the 1st value is larger the 1st value is larger 2 10 the 2nd value is larger 2 10 the 2nd value is larger

Activity Diagram ElementSymbol Start Stop Statements Branching Repetition Order of activities 18 start end

Branching / Selection If (conditional_expression) { ; …; } else { ; …; } 19 if (conditional_expression) { ; …; } switch (control_expression) { case expression 1: ; case expression 2: ; case expression n: ; default: ; } break; break label; continue; return; return expression;

Repetition (looping) while (expression) { ; …; } 20 do { ; …; } while (expression); for (initialization; condition; increment or decrement) { ; …; }

Repetition structure 21 condition FALSE TRUE

Repetition example 22 FALSE TRUE branching repetition

Activity Diagram ElementSymbol Start Stop Statements Branching Repetition Subprograms Order of activities 23 start end

Subprogram 24 We haven’t mastered programs yet, so let’s leave subprograms till later.

Pseudo code read num1, num2 if num1 > num2 then print “the 1st value is larger” else print “the 2nd value is larger” 25 num1 > num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end

Tracing / Desk-checking Find (and fix) design problems before they become code problems Play the computer – Simulate the program you have designed by following the design you wrote – Follow the design you wrote, not the the one in your head 26

Branching example 27 num1 < num2 TRUE FALSE Print “the 1st value is larger” Print “the 2nd value is larger” start Input num1, num2 end 200 the 2nd value is larger 200 the 2nd value is larger

Exercise problem - Barista 28 take order place cup make coffee milk Y prep milk pour in cup N syrup Y pour in cup N give customer more customers Y N take 5 loop take order place cup make coffee if milk then begin prep milk pour milk end if syrup then add syrup give customer if more orders then goto loop else break loop endloop take 5

Summary (not the end of class) Programs transform the initial input values (problem statement) to the final output values (solution) Solutions are composed of – Sequence of statements – Branching – Repetition Design before code – Flowcharts – Activity Diagrams – Pseudo Code 29

30

Now, a game Elements of Tic Tac Toe: Space – empty, X, O Board – 9 Spaces Game – the board – moves – win/loss/tie src: 31

Tic Tac Toe – structure code design 32

A move – activity code design Decision diagram Input: by player Output: was move made T/F 33

The game – activity code design Input: by two players Output: Win/Loss/Tie 34

Next Time Work on your homework Ask questions 35