Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.

Slides:



Advertisements
Similar presentations
 Control structures  Algorithm & flowchart  If statements  While statements.
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
CS150 Introduction to Computer Science 1
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Computer Science 1620 Programming & Problem Solving.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
Control Structures in C++ while, do/while, for switch, break, continue.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
Structured Program Development in C
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
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.
Previously Repetition Structures While, Do-While, For.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline -Introduction -Algorithms -Pseudocode -Control Structures -if Selection Structure.
Unit 2 – Algorithms & Pseudocode. Algorithms Computer problems solved by executing series of action in order Procedure –The Actions to execute –The Order.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
1 Lecture 3 Control Structures else/if and while.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering Problem Solving and Logic.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Objective: Students will be able to: Declare and use variables Input integers.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Algorithm: procedure in terms of
while Repetition Structure
for Repetition Structures
Controlling execution - iteration
Chapter 2 Assignment and Interactive Input
Control Statements Kingdom of Saudi Arabia
Chapter 4- part 2 Control Statements: Loops 1
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Chapter 4 Control Statements: Loops 1
Structured Program
Programming in Pseudocode
Counting Loops.
Starting Out with C++: From Control Structures through Objects
Chapter 3 - Structured Program Development
Chapter 3 - Structured Program Development
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
EPSII 59:006 Spring 2004.
Presentation transcript:

Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement

Understand the problem A class of students took a quiz. The grades (integers ranging from 0 to 100) for this quiz are available. Determine the class average on the quiz.

First level design Determine the class average for the quiz. Complete representation of what the program needs to do. Not detailed enough to code yet.

Next level of refinement Initialize variables Input the quiz grades. Total them and count them. Calculate and print the class average Complete representation of the problem but still not detailed enough to code.

Refine “Initialize variables” Initialize total to zero Initialize counter to zero Initialize average to zero

Refine “ Input the quiz grades. Sum them and count them..” Think first. How many grades do we have? How will we know when we have the last one?

Refine “ Input the quiz grades. Sum them and count them..” Input the first grade While the user has not yet entered the special end of data value Add this grade into the total Add one to the grade counter Input the next grade (possibly the special end of data value)

Calculate and print the class average Think first. What kind of errors could occur? What could go wrong? Division by 0. User doesn’t enter any grades.

Calculate and print the class average If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “ No grades were entered”

Grade averaging pseudo-code Initialize total to zero Initialize counter to zero Initialize average to zero. Input the first grade While the user has not yet entered the special end of data value Add this grade into the running total Add one to the grade counter Input the next grade (possibly the special end of data value) If the counter is not equal to zero Set the average to the total divided by the counter Print the average else Print “ No grades were entered”

#include using namespace std; main () { float average = 0; int counter = 0, total = 0, grade; cout << “Enter grade, enter -1 to end: “; cin >> grade; while (grade != -1) { total = total + grade; counter++; cout << “Enter grade, enter -1 to end: “; cin >> grade; } if (counter != 0) { average = (float) total/counter; cout << “Class average is “ << setprecision (2) << average << endl; else cout << “No grades were entered” << endl; return 0; }