Outline. First Quiz Results. Exercise Five Goals. Question Three. Questions One and Two. Exercise five if statements February 28, 2006

Similar documents
Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Programming Language. Functions. Eng. Anis Nazer First Semester

EP578 Computing for Physicists

Expressions, Input, Output and Data Type Conversions

C++ Programming: From Problem Analysis to Program Design, Third Edition

Computer Programming : C++

Introduction to Programming

1st Midterm Exam: Solution COEN 243: Programming Methodology I

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

The C++ Language. Arizona State University 1

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Unit 7. 'while' Loops

Chapter 3 Problem Solving and the Computer

Other operators. Some times a simple comparison is not enough to determine if our criteria has been met.

University of Dublin

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Lab01: C++ Expressions ES036a: Programming Fundamentals Fall 2007

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Objectives. In this chapter, you will:

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Programming. C++ Basics

VARIABLES & ASSIGNMENTS

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 4 - Notes Control Structures I (Selection)

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Increment and the While. Class 15

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Functions Project Core Precalculus Extra Credit Project

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Chapter 2 Basic Elements of C++

From Pseudcode Algorithms directly to C++ programs

Introduction to the C++ Programming Language

Creating a C++ Program

Relational Operators and if. Class 10

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

The Further Mathematics Support Programme

Getting started with C++ (Part 2)

The following is a typical execution run of this program:

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Introduction To Computer Programming C++ Mr. Clausen Program C11A, C11B

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Control Statements. If Statement if statement tests a particular condition

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Chapter 2: Introduction to C++

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

PreCalculus 300. Algebra 2 Review

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Problem Solving: Storyboards for User Interaction

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.7. User Defined Functions II

Introduction to C++ Systems Programming

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Lab Instructor : Jean Lai

In this chapter you will learn:

LECTURE 02 INTRODUCTION TO C++

PIC 10A. Lecture 3: More About Variables, Arithmetic, Casting, Assignment

Ch 6. Functions. Example: function calls function

Floating-point numbers. Phys 420/580 Lecture 6

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Engineering Problem Solving with C++, Etter/Ingber

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

COMP322 - Introduction to C++ Lecture 01 - Introduction

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

Lab # 02. Basic Elements of C++ _ Part1

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 3 - Functions

Using Free Functions

Computer Programming

Local and Global Variables

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

2 nd Week Lecture Notes

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Control Structures. Flowchart Symbols

Solving Equations with Inverse Operations

Computing and Statistical Data Analysis Lecture 3

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

C++ basics Getting started with, and Data Types.

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

SECONDARY MATH TRANSFORMATIONS

Transcription:

Eercise five if statements February 8, 6 Laboratory V Program Control Using if Statements Larry Caretto Computer Science 6 Computing in Engineering and Science February 8, 6 Outline Review first quiz Summarize lecture material on if statements and Boolean variables Eercise five goals Outline tasks for eercise five Provide details for some tasks Note eercises five to eight have two times the credit of eercises one to four Eercise Five Goals As a result of this eercise you should be able to accomplish the following: recognize two common errors in if statements use relational operators, Boolean operators and Boolean variables write a program from pseudocode using if statements understand effect of round-off error and use of algorithms to avoid such error First Quiz Results students 6 maimum possible.9 mean 5.5 median 9.3 standard deviation Grade distribution 5 39 5 6 5 5 55 56 Solutions available on line 3 Questions One and Two Most errors on integer operations in first question Integer division truncates regardless of variable on left hand side Statement int(a) truncates Second question generally okay Remember to use * for multiplication Check parentheses for correctness Remember to use pow(number, power) for eponentiation 5 Question Three #include <iostream> #include <cmath> using namespace std; int main() cout << This program computes << trajectory parameters\n\n << Enter the initial velocity in << meters per second: ; double v; cin >> v; const double g 9.87; 6

Eercise five if statements February 8, 6 Question Three, Part Two double hma v * v / g; double tma v / g; double tfinal * v / g; cout << \n\nthe maimum height is << hma << meters. << \nthe maimum height occurs << at << tma << seconds. << \nthe final time is << tfinal << seconds. ; return EXIT_SUCCESS; 7 Tasks for Eercise Five One copy and paste code with errors in if statements; run without changes, correct errors and run corrected code Two copy, paste and run code using Boolean variables for data validation Three write a program with if statements to find roots of quadratic equations handling eceptional conditions Four modify task three code to reduce round-off error 8 Task : Errors in if Statements Semicolon after condition parentheses if ( <condition> ); // error Assignment operator () instead of relational equality operator () if ( 3 ) //usually an error When you use ( a b ) as a condition the value of a is set to the value of b the resulting value of a is converted to bool zero is false; nonzero is true Task : Boolean Variables Declared as type bool Used for complicated conditional tests Have values false () or true (not zero) Some possible statements int, Ma, Min ; cout << Enter : ; cin >> ; bool baddata < Min > Ma; bool gooddata!baddata; 9 The boolalpha Manipulator Output of Boolean variables normally prints for true and for false boolalpha manipulator prints the tet true or false bool good true; bad!good; cout << good << << bad; // prints cout << boolalpha << good << << bad; // prints true false Use noboolalpha to return to default Tasks 3 and : Quadratic Roots + b + a b a Possible problems with these solutions to a + b + c in a computer a b and a, b comple roots two roots the same roundoff errors

Eercise five if statements February 8, 6 When is b equal to c? Numbers are not represented eactly in the computer (roundoff error) Smallest double such that +! is.69533e 6 Test for b c should be written as follows const double eps e-5; if ( fabs( b * b * a * c ) < eps * (b * b + fabs( * a * c) ); 3 Initial Pseudocode for Quadratics If a b Print out that there is no solution Otherwise, if a and b, Print message that this is a linear equation with only one solution; print solution of c/b Otherwise if b c (see previous chart for this test) Print message that the two roots are the same and print the common root, b/(a) Initial Quadratic Pseudocode II Otherwise, if b c < Print message that roots are comple and print two comple roots Otherwise Compute roots from conventional formulas shown below Print message that there are two distinct roots and print values of roots b + a + b a 5 Printing Comple Roots Comple roots for b < c (i -) Real part b + i a a b i a a Imaginary part Take absolute value (fabs in C++) of b c before taking square root Print roots one of two ways label real part and imaginary part or print roots as 3 + i and 3 i Use cout << + i or i in latter option 6 Task Three Cases Seven different cases shown below and in instructions Run all cases and print case VII results with significant figures for comparison with task four results case a b c I II III.5.5 IV V VI VII -6-6 7 More on Roundoff Error Arises because numbers are not represented eactly in a computer Main problem is the subtraction of two nearly equal numbers 356789 356788 356789 + ( 356788) Reduce significant figures from nine to one Algorithm for quadratic equation solutions can avoid this 8 3

Eercise five if statements February 8, 6 Eample, b - c 356788 -b ± b - c If b 356789 use of + part of ± sign will give roundoff but sign is okay If b -356789 use of part of ± sign will give roundoff but + sign is okay We know that the product of the two roots of a quadratic equals c/a Choose formula for root with + or to avoid roundoff then compute second from c/(a ) 9 How do we know c/a? b + b a a Use ( + y)( y) y and z ( b) ( ) ( ) b c c a ( ) z Final Pseudocode for Quadratics If a b (changes in red) Print out that there is no solution Otherwise, if a and b, Print message that there is only one solution and print solution of c/b Otherwise if b c Print message that the two roots are the same and print the common root, b/(a) Final Quadratic Pseudocode II Otherwise, if b c < Print message that roots are comple and print two comple roots Otherwise If b is positive Compute first root [ b (b c) / ]/(a) otherwise Compute first root, [ b + (b c) / ]/(a) End of If b is positive choices Compute second root c/(a ) Print message and two distinct roots Compare Results Run case VII input with both task three and task four code Get output with significant figures Compare results from the two codes with the eact answer from series epansion in notes. 3 3 b ac a c a c + + + + L 6 a b b b 3 3 c ac a c 5a c + + + + L 6 b b b b 3 Compare Results II Task three has seven data sets Use data set VII only for task four For this data set, use significant figures of accuracy to compare Results from task three Results from task four Results from series epansion You should find that the task four code is more accurate for one of the roots

Eercise five if statements February 8, 6 Good Style: Indent Structures if ( hours > ) pay rate * ( +.5 * ( hours ) ); else Bad pay rate * hours; Structure if(hours>)payrate*(+.5*(hours ));elsepayrate*hours; 5 5