1 CS 1430: Programming in C++. 2 Input: 45.5 55 60.5 39.5 -1 Input ends with -1 Sentinel-Controlled Loop Input: 4 45.5 55 60.5 39.5 Input begins with.

Slides:



Advertisements
Similar presentations
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Advertisements

CMPUT 101 Lab # 5 October 22, :00 – 17:00.
Computer Science 1620 Loops.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
Software Engineering 1 (Chap. 1) Object-Centered Design.
1 Programming and Problem Solving — Software Engineering (Read Chap. 2)
Programming and Problem Solving — Software Engineering (Read Chap. 2) 1.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Chapter 6 Looping.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 1 Quiz Questions (CGS-3464) Mahendra Kumar
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Chapter 5 Repetition and Loop Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville,
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
CS 1430: Programming in C++.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
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.
Bill Tucker Austin Community College COSC 1315
Repetitive Structures
CS 1430: Programming in C++ No time to cover HiC.
Chapter 2 Assignment and Interactive Input
CS 1430: Programming in C++.
While Loops.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
TIPS: How to Be Successful
Computing Fundamentals
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Objectives You should be able to describe: The while Statement
CS 1430: Programming in C++ No time to cover HiC.
Presentation transcript:

1 CS 1430: Programming in C++

2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with 4 (count) Count-Controlled Loop

Input: Variables totalCount: number of scores to process loopCount: number of iterations the loop body has been executed (number of scores processed) Pseudo Code Input totalCount Set loopCount to 0 // LCV Initializing other variables While loopCount < totalCount Input the score Process score Increase loopCount by 1 3

How to Initialize? Range is known const float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0; // Initialize max max = MIN_SCORE; // Initialize min min = MAX_SCORE; // Initialize total total = 0; 4 Range is NOT known // Input the first score cin >> score; // Initialize max max = score; // Initialize min min = score; // Initialize total total = score;

// The range is known const float MIN_SCORE = 0.0; const float MAX_SCORE = 60.0; float score, max = MIN_SCORE; int totalCount, loopCount; // Interactive mode cout << "Enter the count of scores: "; cin >> totalCount; loopCount = 0; // No prime read of score! while (loopCount < totalCount) { // Interactive mode cout << "Enter a score: "; cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } loopCount ++; } // Four Parts of Loop 5

// Another way: No loopCount float score, max = MIN_SCORE; int totalCount; // Batch mode: no input prompt // cout << "Enter the count of scores: "; cin >> totalCount; while (totalCount > 0) { // Batch mode: no input prompt // cout << "Enter a score: "; cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { if (score > max) max = score; } totalCount --; // totalCount = totalCount - 1; } // What’s the value of totalCount after the loop? 6

// The range is NOT known float score, max; int totalCount, loopCount; // Batch mode cin >> totalCount; loopCount = 0; while (loopCount < totalCount) { // Batch mode cin >> score; if (loopCount == 0) // first score max = score; else { if (score > max) max = score; } loopCount ++; } 7

// The range is NOT known float score, max; int totalCount, loopCount; // Batch mode cin >> totalCount; loopCount = 0; while (loopCount < totalCount) { loopCount ++; // Batch mode cin >> score; if (loopCount == 1) // first score max = score; else { if (score > max) max = score; } 8

Average and Max of n Scores Range is known Invalid scores are not considered 9 Initialize (total, max, validCount) Input totalCount While totalCount > 0 Input a score If score out of range display a message Else Increase validCount by one add score to total if score > max max = score Decrease totalCount by one Compute average using validCount and total

// Average and Max of n Scores // Range is known and invalid scores are not considered float score, max = MIN_SCORE, total = 0.0, average; int totalCount, validCount = 0; cin >> totalCount; while (totalCount > 0) { cin >> score; if (score MAX_SCORE) cout << "Invalid score: " << score; else { validCount ++; total += score; // total = total + score; if (score > max) max = score; } totalCount --; } average = total / validCount; // Any problems? 10

// validCount could be 0! average = total / validCount; // while loop to compute max, total and validCount while (totalCount > 0) { … } if (validCount > 0) { average = total / validCount; cout << endl << "max = " << max; cout << endl << "average = " << average; // cout << endl << "average = " << total / validCount; } else cout << endl << "No valid scores entered."; 11

More Arithmetic Operators validCount ++; // validCount = validCount + 1; totalCount --; // totalCount = totalCount - 1; total += score; // total = total + score; total -= score; // total = total - score; yValue /= xValue; // yValue = yValue / xValue; yValue %= xValue; // yValue = yValue % xValue; yValue *= xValue; // yValue = yValue * xValue; 12

Example float num, total = 1; // DO_01: // Input a num // If it’s positive // Add it to total // Otherwise // Multiply it to total cin >> num; if (num > 0) total += num; else total *= num; 13

Example int num; cin >> num; // DO_02: // While num is not positive // Increment its value by 2 while (num <= 0) num += 2; 14

Example float num, total; cin >> total; // DO_03: // If total is negative // Input a value to num and add it to total // until total is not negative while (total < 0) { cin >> num; total += num; } 15

Factorial N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 … 16

Factorial Pseudo Code Input Num (assume Num >= 0) Fact = 1 loopCount = 0 While loopCount < Num loopCount ++ Fact = Fact * loopCount Output Fact 17 N! = 1 * 2 * 3 *…* (N - 1) * N 0! = 1 1! = 1 2! = 1 * 2 = 2 … 5! = 1 * 2 * 3 * 4 * 5 = 120 … How to compute n! for any n?

Factorial Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 Fact = Fact * loopCount loopCount -- Output Fact Correct! 18 Pseudo Code Input Num Fact = 1 loopCount = Num While loopCount > 1 loopCount -- Fact = Fact * loopCount Output Fact Correct? NO!

Schedule  Program 1 Due Time : Tuesday by 10 pm Grace time: Friday by 10pm  Quiz3-3 Due 10 PM Wednesday  Lab2 Tuesday by 5 pm  Lab3 Tuesday evening 19

20 Lab 3 If no differences by 5 pm Thursday Five (5) points Else if no differences by 5 pm the following Tuesday Three (3) points Else No Points! Same for other Labs Unless specified otherwise.

21 Program 1  Differences: NONE  Follow the Requirements! (At least one Nested IF statement)  Style: Could lose up to 7 points! Save your log file Print log file for me to check style or Drop your UserName_Prog1.cpp file to K:\Courses\CSSE\yangq\DropBox

22 Program 1 If submit by the due time and no differences Two bonus points on style (total cannot be more than 15 points) Else if submit by the grace time and no differences May still get 15 points Else if submit after grace time and pass test 1 Zero points Could pass CS143 Else Retake CS143 next semester

23 Program 1: One Nested IF statement! if (...) {... else... } else if (...) {... } else if (...)... else { if (...)... }

24 Two IF statements if (...) {... } else if (...) {... } else if (...)... else { } if (...)... else...

25 Style File with line numbers added: Prog1.cpp : 2: #include 3: #include 4: using namespace std; 5: 6: int main() 7: { 8:... Miss Comment Block! Could lose 3 points!

26 Style File with line numbers added: Prog1.cpp : // : // Name: Qi Yang 3: // 4: // Course: CS 143, Section 9, Fall : // 6: // Purpose: This program converts between kilometers and 7: // miles or between Fahrenheit temperature and Celsius. 8: // 9: // Input : This program accepts the following prompted input 10: // from the keyboard: 13: // 14: // Output: This program provides the following output prompts to 15: // standard output (the monitor): 16: // "Input a type: kilometers, miles, fahrenheit, or celsius; 17: // followed by a space and then an amount: " 24: // : 26: #include 27: #include 28: using namespace std;

27 Style 36: 37: int main() 38: { 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount; Constants should be before main() 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41:

28 Style 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount; Do not indent constants! 30: const float KM_PER_MILE = 1.61; 31: const float FREEZING_F = 32.0; 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount;

29 Style 76: if (amount > 120) 77: { 78: conversion = (amount – 32.0) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else Magic Number! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else

30 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else Brace Alignment! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else

31 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in “ << "Fahrenheit is " << conversion << " degrees in Celsius."; 80: } 81: else Line too long! Each line can have at most 74 columns! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else

32 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in “ 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else Alignment! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else

33 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in “ 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else 84: cout << “Invalid unit!!”; Indentation! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else 84: cout << “Invalid unit!!”;

34 Style 76: if (amount > MAX_FAHR_TEMP) 77: { cout << “Invalid unit!!”;} 78: else 79: { 80: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 81: cout << “The result is good."; 82: } 83: 84: Braces on separate lines! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: cout << “Invalid unit!!”; 79: } 80: else 81: { 82: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 83: cout << “The result is good."; 84: }

35 Style 76: if (amount > MAX_FAHR_TEMP) { 77: cout << “Invalid unit!!”;} 78: else { 79: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 80: cout << “The result is good."; 81: } 82: 83: Braces on separate lines! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: cout << “Invalid unit!!”; 79: } 80: else 81: { 82: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 83: cout << “The result is good."; 84: }

36 Style 76: if (amount>MAX_FAHR_TEMP) 77: { 78: conversion=(amount - FREEZING_F)/FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount<<" degrees in “ 80: << "Fahrenheit is "<<conversion 81: << " degrees in Celsius."; 82: } 83: else Space before and after operator! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79: cout << endl << "Temperature " << amount << " degrees in " 80: << "Fahrenheit is " << conversion 81: << " degrees in Celsius."; 82: } 83: else

37 Style 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 82: } 83: 84: else if 85: 86: { 87: 88: 89: } 90: 91: else 92: 93: { 94: No blank line before/after else! 76: if (amount > MAX_FAHR_TEMP) 77: { 78: conversion = (amount - FREEZING_F) / FAHR_PER_CELSIUS; 79:... 80: } 81: else if 82: { 83:... 84: } 85: else 86: {

38 Style 33: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 34: 35: int main() 36: 37: { 38: 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount; No blank line before/after brace! 32: const float FAHR_PER_CELSIUS = 9.0 / 5.0; 36: 37: int main() 38: { 39: string units; 40: float amount, conversion; 41: 42: cout << "Input a type: kilometers, miles, fahrenheit, or celsius;" 43: << endl << "followed by a space and then an amount: " << endl; 44: cin >> units >> amount;