CS 1430: Programming in C++ No time to cover HiC.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 Lecture 16 Chapter 6 Looping Dale/Weems/Headington.
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.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
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.
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.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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 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++. 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++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
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()
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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?
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
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.
Chapter 3 Selection Statements
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
CS 1430: Programming in C++ No time to cover HiC.
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Some Basics for Problem Analysis and Solutions
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS 1430: Programming in C++.
Some Basics for Problem Analysis
Alternate Version of STARTING OUT WITH C++ 4th Edition
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.
Counting Loops.
Standard Input/Output Stream
Chapter 3 Input output.
CS 1430: Programming in C++.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Formatted Input, Output & File Input, Output
Let’s all Repeat Together
Fundamental Programming
What Actions Do We Have Part 1
C++ Programming Basics
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:

CS 1430: Programming in C++ No time to cover HiC

Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together

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

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

Input -2 Sentinel-Controlled Loop -1: end of section -2: end of class 50.5 54 39 47.5 -1 50 49.5 52 -1 38 49 -1 50 59.5 53 60 -1 -2 Sentinel-Controlled Loop -1: end of section -2: end of class Range is not known All scores are valid

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 Process and display class result

// 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; }

cin >> score; // Prime Read while (score != END_CLASS) { 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) if (score < secMin) secScoreCount ++; secTotal += score; cin >> score; // Display section result if (classScoreCount == 0) { classMax = secMax; classMin = secMin; } else if (secMax > classMax) if (secMin < classMin) classTotal += secTotal; classScoreCount += secScoreCount; cin >> score; // Process and display class result

Different Input Count-Controlled Loop Range is known Checking range 4 4 50.5 54 39 47.5 3 50 49.5 52 2 38 49 4 50 59.5 53 60 Count-Controlled Loop Range is known Checking range Ignore invalid scores Can you do it?

Space Tab Next line … (Replace Tab with 3 spaces in VS) White Spaces Space Tab Next line … (Replace Tab with 3 spaces in VS)

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: -50.5 50 // What is the value of theInput? // Depending on the type of theInput // But the spaces before “-50.0” will be ignored.

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

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

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

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

Tracing aChar C S 1 4 3 X // Input // CS 143 // X char aChar; int count = 0; cin >> aChar; // Prime read before loop while (aChar != ‘X’) { count ++; cout << aChar; } cout << “Count: “ << count; Tracing aChar C S 1 4 3 X Next time : HiC Input.cpp (Quiz3-5 needs more time)

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

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

Function cin.eof() The function returns true or false eof(): end of file end of input From keyboard [CTRL-D] 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

Tracing aChar cin.eof() C False S False - False 1 False 4 False // Input // CS 143 // X char aChar; int count = 0; cin.get(aChar); // Prime read before loop while (!cin.eof()) { count ++; 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

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

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?

#include <iomanip> 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; // 12.00 value = 1 / 5.0; cout << "\nFloat value 1/5.0: " << value; // 0.20

#include <iomanip> 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; // □ □ □ 12.00 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.

Schedule Prog1 Can still fix style Prog2: Loops Start Early! Two points Quiz Monday! Lab 2 Demo by 4pm Quiz3-4 Due 5 pm Monday Test 1: Next Friday Note01 – Note10 Prog2: Loops! Quiz3-5 Now!