Input Based Programs.

Slides:



Advertisements
Similar presentations
The Software Lifecycle. Example Problem: Update a Checkbook Write a program that allows the user to enter a starting balance, a transaction type, D or.
Advertisements

Problem You require an algorithm that will receive the length of the hypotenuse and one of the sides of a right triangle, and calculate the length of the.
Hand Crafting your own program By Eric Davis for CS103.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Java Programming Practice.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Writing and Testing Programs Drivers and Stubs Supplement to text.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
CH Programming An introduction to programming concepts.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CMSC 104, Version 9/011 Incremental Programming Topics Review of Incremental Programming Example of Incremental Programming Reading None.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Working with Batches of Data
triangle.
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Functions, Part 2 of 2 Topics Functions That Return a Value
Chapter 2 Assignment and Interactive Input
CS 1428 Exam I Review.
Implementing Functions from a Detailed Design Quick Tips
Lesson 3 - Repetition.
Quiz Next Monday.
Some Basics for Problem Analysis and Solutions
Repetition Statements
One-Dimensional Array Introduction Lesson xx
Starting Out with C++: From Control Structures through Objects
Alternate Version of STARTING OUT WITH C++ 4th Edition
Value returning Functions
Counting Loops.
Programming Funamental slides
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
Wednesday 09/23/13.
Chapter 3 Input output.
If Statements.
Elementary Programming (C++)
Programs written in C and C++ can run on many different computers
Fundamental Programming
Reading from and Writing to Files
CS 101 First Exam Review.
Using string type variables
(Dreaded) Quiz 2 Next Monday.
CS 1428 Exam I Review.
Reading from and Writing to Files
Programming Fundamental
Programming Fundamental-1
Presentation transcript:

Input Based Programs

Generic Algorithm Algorithm for every program ever written:

Generic Algorithm Algorithm for every program ever written: Get some input Do some stuff to it Output a value

Getting Input cin : console input "Read from the console into variable1" >> : stream extraction operator

Prompt Lines Prompt for input with cout before doing cin: cout << "Please enter a number between 1 and 10 and " << "press the return key" << endl; int num; cin >> num;

Bad Input Bad input = silent failure!!! Variable maintains previous value int x; cin >> x; Input Result 12 X is set to 12 Q X is ??? program continues .5 12.5 X is set to 12 ".5" is saved for next needed input

Bad Input Bad input = silent failure!!! No more input accepted int x, y; cin >> x; cin >> y; Input Result 12 2 X is set to 12, y to 2 12 2 12 Q X is 12 y is ???? Q 12 X and Y are ????

Bad Input Bad input = silent failure!!! No more input accepted int x, y; cin >> x; cin >> y; Input Result 12 Q X is 12 y is ???? Q 12 X and Y are ????

Don't worry about bad input For now… Don't worry about bad input Don't attempt to "catch" bad input on assignments No "try again" input loops, no "are you sure", no "run again?", etc… DO make sure you accept the input a problem specifies

Software Development Lifecycle The grand process:

Software Development Lifecycle Small scale Understand problem Decide on inputs and outputs How many, what types, what order Start program Read in inputs into variables Do math on variables, store results into new variables Output variables that contain answers

Software Development Lifecycle Testing: Test early and often Read in values – confirm you got them correctly Partway through calculations Work in small steps NO: double answer = (-b + pow( pow(b,2) – 4 * a * c), 0.5) / (2.0 * a); YES: double bSquared = pow (b, 2); double discriminant = bSquared – 4 * a * c double twoA = 2.0 * a; double answer = (-b + pow(discriminant, 0.5)) / twoA;

Sample 1 Given the lengths of the two legs of a triangle, print the length of the hypotenuse. Sample run: Input side A: 3 Input side B: 4 Hypotenuse is 5.0

Sample 1 Given the lengths of the two legs of a triangle, print the length of the hypotenuse. Input: side lengths a and b Output: hypotenuse

Sample 2 Ask the ctime library for number of seconds since Midnight, Jan 1st 1970. Print the time of day like hour:minute:second.

Sample 2 Ask the ctime library for number of seconds since Midnight, Jan 1st 1970. Print the time of day Input: None from user. (Get time from library) Output: hours, minutes, seconds