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.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

Computer Science 1620 Loops.
1 Lecture 15 Chapter 6 Looping Dale/Weems/Headington.
1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 6 Looping Dale/Weems. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 6 Looping.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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
Instructor - C. BoyleFall Semester
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 6 Looping. 2 Chapter 6 Topics l While Statement Syntax l Count-Controlled Loops l Event-Controlled Loops l Using the End-of-File Condition to.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
1 Looping. 2 Chapter 6 Topics  While Statement Syntax  Phases of Loop Execution  Two Types of Loops: Count-Controlled Loops &Event-Controlled Loops.
Organizing Heterogeneous Data Arrays allow a programmer to organize lists of values that are all of the same type (homogeneous). But we are often faced.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
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
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
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
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
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++.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
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?
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
CS 1430: Programming in C++ No time to cover HiC.
Counting Loops.
Standard Input/Output Stream
CS 1430: Programming in C++.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Class StudentList class StudentList { private: int numStudents;
CS 1430: Programming in C++.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
CS 1430: Programming in C++ No time to cover HiC.
Presentation transcript:

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

3 Pseudo Code While not end of class Process one section While not end of class While not end of section Process one score

4 Pseudo Code Initialize class variables (LCV) While not end of class Initialize section variables (LCV) While not end of section Process one score Update section LCV Process and display section result Process class variables Update class LCV Process and display class result

5 Input Sentinel-Controlled Loop -1: end of section -2: end of class Range is not known All scores are valid

6 Pseudo Code classTotal = 0, classCount = 0 Input a score (Prime Read) While score not -2 secTotal = 0 secCoumt = 0 While score not -1 Update secCoumt, secTotal, secMax, secMin Input next score Process and display section result Update classMax, classMin, classCount, classTotal Input next score Process and display class result

7 // Constants // Before main() const int END_CLASS = -2; const int END_SECTION = -1; int main() { // Declare variables // Inside main() float score; float classMax, classMin, classTotal; float secMax, secMin, secTotal; int classScoreCount, secScoreCount; … return 0; }

8 classTotal = 0.0; classScoreCount = 0; cin >> score; // Prime Read while (score != END_CLASS) { secTotal = 0; secScoreCount = 0; while (score != END_SECTION) { if (secScoreCount == 0) { secMax = score; secMin = score; } else { if (score > secMax) secMax = score; if (score < secMin) secMin = score; } secScoreCount ++; secTotal += score; cin >> score; } // Display section result if (classScoreCount == 0) { classMax = secMax; classMin = secMin; } else { if (secMax > classMax) classMax = secMax; if (secMin < classMin) classMin = secMin; } classTotal += secTotal; classScoreCount += secScoreCount; cin >> score; } // Process and display class result

9 Different Input Count-Controlled Loop Range is known Checking range Ignore invalid scores Can you do it? Program2!

10 White Spaces Space Tab Next line … (Replace Tab with 3 spaces in VS) Tools/Options/Text Editor/C/C++ Tab Size/Ident Size: 3 Check “Insert Spaces”

11 Input Operator >> cout << "What is your input: "; cin >> theInput; // Operator >> will ignore white spaces // before reading // Operator >> will stop at white spaces // after have read any value // Input: // What is the value of theInput? // Depending on the type of theInput // But the spaces before “-50.0” will be ignored.

12 Type char char theInput; cout << "What is your input: "; cin >> theInput; // Input: // Q: What is the value of theInput? // A: _____ // ‘-’ // Not space before ‘-’

13 Type integer int theInput; cout << "What is your input: "; cin >> theInput; // Input: // Q: What is the value of theInput? // A: _____ // -50 // stop at ‘.’, which cannot be part of int

14 Type float float theInput; cout << "What is your input: "; cin >> theInput; // Input: // Q: What is the value of theInput? // A: _____ // -50.5

15 Type string string theInput; cout << "What is your input: "; cin >> theInput; // Input: // Q: What is the value of theInput? // A: _____ // -50.5

16 // Input // CS 143 // X char aChar; int count = 0; cin >> aChar; // Prime read before loop while (aChar != ‘X’) { count ++; cout << aChar; cin >> aChar; } cout << aChar; cout << “Count: “ << count; Tracing aChar C S X

17 Function cin.get() The function will read one char at a time Including white spaces!

18 // Input // CS 143 // X char aChar; int count = 0; cin.get(aChar); // Prime read before loop while (aChar != ‘X’) { count ++; cout << aChar; cin.get(aChar); } cout << aChar; cout << “Count: “ << count; Tracing aChar C S \n X

19 Function cin.eof() The function returns true or false eof(): end of file end of input From keyboard [CTRL-Z] From File Not True after reading the last item in the file True when trying to read after reading the last item in the file

20 // Input // CS 143 // X char aChar; int count = 0; cin.get(aChar); // Prime read before loop while (!cin.eof()) { count ++; cout << aChar; cin.get(aChar); } cout << aChar; cout << “Count: “ << count; Tracing aChar cin.eof() C False S False - False 1 False 4 False 3 False \n False X False ? True

21 int theInput; int count = 0; cout << "What is your input: "; cin >> theInput; while (!cin.eof()) { count ++; cout << "What is your input: "; cin >> theInput; } cout << theInput; cout << “Count: “ << count; // Input: // What is the value of theInput? // Trouble when reading ‘.’!

22 Formatting Output cout << endl << "My output."; cout << "\nMy output."; float average; int count; // Compute count and average cout << endl << count; cout << endl << average; // How many digits displayed for average?

23 #include cout << fixed << showpoint << setprecision(2); // Always display two decimal digits. // No need to remember for quiz/test float value; value = 1 / 3.0; cout << "\nFloat value 1/3.0: " << value; // 0.33 value = 36 / 3.0; cout << "\nFloat value 36/3.0: " << value; // value = 1 / 5.0; cout << "\nFloat value 1/5.0: " << value; // 0.20

24 #include cout << fixed << showpoint << setprecision(2); // The setting is applied to all values displayed // until the setting changes. float value; value = 1 / 3.0; cout << "\nFloat value 1/3.0: " << setw(8) << value; // □ □ □ □ 0.33 value = 36 / 3.0; cout << "\nFloat value 36/3.0: " << setw(8) << value; // □ □ □ value = 1 / 5.0; cout << "\nFloat value 1/5.0: " << value; // 0.20 (no extra spaces) // setw() is only applied to the next value // It has to be used for every value need the format.

25 Schedule Prog1 Grace Time: 10 PM Today Prog2: Loops Start Early! Quiz3-4 Due 10 pm Monday Quiz 1 Monday (10 points) Test 1: Next Friday Note01 – Note10 Quizzes, Labs, Progs