Working with Batches of Data

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Chapter 5: Loops and Files.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Introduction to C Programming
Program Input and the Software Design Process ROBERT REAVES.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Instructor - C. BoyleFall Semester
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
File I/O ifstreams and ofstreams Sections 11.1 &
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Working with Batches of Data Lecture 4 Hartmut Kaiser
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
1 Lecture 3 Control Structures else/if and while.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Loops and Files. 5.1 The Increment and Decrement Operators.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
CPS120 Introduction to Computer Science Exam Review Lecture 18.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 1.2 Introduction to C++ Programming
Review 1.
C++ Basic Input and Output (I/O)
ifstreams and ofstreams
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Lecture 07 More Repetition Richard Gesick.
User-Defined Functions
Quiz Next Monday.
Lecture 4B More Repetition Richard Gesick
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
One-Dimensional Array Introduction Lesson xx
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Introduction to C++ Programming
Chapter 3: Input/Output
Chapter 3 Input output.
Introduction to C++ Programming
Computing Fundamentals
Let’s all Repeat Together
File I/O in C++ I.
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
Chapter 2 part #3 C++ Input / Output
ifstreams and ofstreams
COMS 261 Computer Science I
Chapter 1 c++ structure C++ Input / Output
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Reading from and Writing to Files
Presentation transcript:

Working with Batches of Data Chapter 3 Working with Batches of Data

Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar with the implementation of sentinel loops. Investigate the <ios> and <iomanip> headers from the standard library. Introduce file input and output. Explore the use of vector objects. Become familiar with the ternary operator.

C++ “C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement.” – Drew Olbrich “I invented the term Object-Oriented, and I can tell you I did not have C++ in mind.” – Alan Kay

Computing Course Grade Suppose you are taking a course where the final is 40% of the grade, the midterm is 20% and homework is 40%. Suppose we do not know the number of homework assignments, we will just read until the user enters and null string. Then we will compute the homework average.

Beginning the Grade Program Our program begins with include and using commands. #include <iomanip> #include <ios> #include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::setprecision; using std::string; using std::streamsize;

ios and iomanip <ios> defines streamsize, which is the type the input-output library uses to define sizes. <iomanip> defines setprecision which we will use to format our output.

Getting a Name int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; Notice how we use one statement but two >> to read in multiple values. double is a floating point number but uses twice as much space for more accuracy.

Sentinel Loop // ask for the homework grades cout << "Enter all your homework grades, "; cout << "followed by end-of-file (ctrl-Z (PC) or ctrl-D (Mac)): "; // the number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read count grades so far, and // num is the sum of the first count grades while (cin >> x){ ++count; sum += x; }

Sentinel Loop Notice the loop while (cin >> x){ //loop stuff } This reads until there are no more numbers to read. The stream is empty. The next input to read cannot be converted to a double. The user indicates the stream is empty using the end-of-file character. Control z (windows) Control d (Linux, Unix, Macintosh)

>> Notice that the >> operator is doing two things here. If there is valid data to be read in the cin stream then it has the side-effect of modifying the value of the variable x. In this case the actual value of the expression is true, so the body of the loop executes. If the read fails there is no side-effect and the return value is false, so the loop terminates.

End-of-File The end-of-file character is a standard way of marking the end of either a data file or input from the keyboard. Once the end-of-file character is encountered, that input stream can not longer be used. All attempts to read from it will fail (return false and have no effect on the variables involved).

Printing Output // write the result int prec = int(cout.precision()); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << setprecision(prec) << endl; return 0; }

Setting Precision We want to print the output with 3 significant digits. To do this we use setprecision. Like endl this is a manipulator. After it is used, all the output from the stream will appear with the specified number of significant digits. Since this is a permanent change it is a good idea to change it back to what it was before when we are done. This is why precision was used to store the previous precision in an int.

int prec = int(cout.precision()); Type Casting Notice the line int prec = int(cout.precision()); This line calls a function, cout.precision(), that has a return type of streamsize. Instead of dealing with this type, we used type casting to turn convert it to an int.

ifstream infile(“in”); Reading From a File Our program is designed so that the input could come from a file with little modification. To do file input and output the first step is to include <fstream>. We create an input stream associated with our file by creating an ifstream object. ifstream infile(“in”); Here infile is our variable name for the stream and “in” is the name of the file on the hard drive.

Reading From a File We read from the stream in the same manner as before. infile >> x; The only modification is that cin is replaced with infile. When we are done with the file we should close it. infile.close();

ofstream outfile(“out”); Writing to a File As with input, output is similar if we want to output to a file. We create an output stream associated with our file by creating an ofstream object. ofstream outfile(“out”); If the file does not already exist it will be created. If it does exist it will be overwritten. Here outfile is our variable name for the stream and “out” is the name of the file on the hard drive.

Writing to a File We write to the stream in the same manner as before. outfile << x; The only modification is that cout is replaced with outfile. Again, when we are done writing we should close the file. outfile.close();

Stream I/O We often try to set up our input and output so that it will not be hard to change from manual to file input/output. This is why we are using end-of-file as the sentinel for our loop. We will also often pass the appropriate stream to functions that do input or output. That way we can switch without rewriting the functions.

Medians Suppose we want to calculated the median of the homework grades instead of the average. We will need to remember and sort the homework grades. To store the grades we will use a vector. This is similar to a sequence in Python.

vector<double> homework; Vectors Unlike in Python, vectors in C++ are homogeneous. They can only contain entries of a single type. For our code this might look like this. vector<double> homework; This creates a vector that is a container (can contain) values of type double. vector is a template class. We specify what it will contain inside angle brackets.

Reading Entries into the Vector vector<double> homework; double x; while (cin >> x) homework.push_back(x); This is similar to sentinel loop from before but we use the push_back method of the vector class. This appends the new value to the end of the vector, increasing its size by 1.

Getting the Size of the Vector The size of the vector has its own type (you may have noticed C++ does a lot of that). vector<double>::size_type Since we don’t want to type this too often let us create our own type using typedef. typedef vector<double>::size_type vec_sz We can now create a variable of this type and initialize it using the size method. vec_sz size = homework.size();

Checking for Empty Vector Next, we check to see if the vector is empty. if (size == 0) { cout << endl << "You must enter your grades. " "Please try again." << endl; return 1; } Notice that returning 1 indicates a problem occurred.

sort(homework.begin(), homework.end()); Sorting the Vector Next we need to sort the entries in the vector into increasing order. sort(homework.begin(), homework.end()); Sort is defined in the <algorithm> header. The vector class has accessor methods begin and end. The arguments of sort specify the list to be sorted.

Computing the Median Computing the median involves using indexing to access elements in the list and a new operator. vec_sz mid = size/2; double median; median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid]; The indexes of elements of a vector begin at 0. Notice that if both operands are integer types then the / operator performs integer division. If one is a floating point type then / performs floating point division. The remainder of integer division is calculated using the % operator.

condition ? statement1 : statement2 Ternary Operator Probably the strangest operator in C++ is the ternary operator ? :. This operator takes three operands in the following syntax. condition ? statement1 : statement2 If the condition is true then statement1 is used to replace the entire expression, including all side-effects and return values. If the condition is false then statement2 is executed.

Ternary Operator The line could be replaced with the following. median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid]; The line could be replaced with the following. if (size % 2 == 0) median = (homework[mid] + homework[mid-1]) / 2; else median = homework[mid]; It is a matter of opinion if the compactness of the ternary operator is worth the penalty in clarity. Some programmers find it clear, easy to use, and more compact. Some programmers avoid it completely.

Complete Program #include <algorithm> #include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> using std::cin; using std::sort; using std::cout; using std::streamsize; using std::endl; using std::string; using std::setprecision; using std::vector; int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final;

Complete Program // ask for and read the homework grades cout << "Enter all your homework grades, " "followed by end-of-file (ctrl-Z (PC) or ctrl-D (Mac)): "; vector<double> homework; double x; // invariant: `homework' contains all the homework grades read so far while (cin >> x) homework.push_back(x); // check that the student entered some homework grades typedef vector<double>::size_type vec_sz; vec_sz size = homework.size(); if (size == 0) { cout << endl << "You must enter your grades. " "Please try again." << endl; return 1; }

Complete Program // sort the grades sort(homework.begin(), homework.end()); // compute the median homework grade vec_sz mid = size/2; double median; median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid]; // compute and write the final grade int prec = int(cout.precision()); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl; return 0; }

Reading Past the End of a Vector It is important that we not try to access undefined elements of a vector. The following would produce unpredictable results. vector<double> homework; cout << homework[0]; The vector has been created but not initialized. In particular we have not assigned a value to homework[0]. We will get whatever happens to be in that memory location. Vectors do not check whether the index is in range.

Unsigned Integer Types All standard-library size types (like vector<double>::size_type) are unsigned integers and cannot be negative. Consider homework.size()-100 This will produce an unsigned integer as well. It cannot be negative even if homework.size() < 100.

Homework Chapter 3 (page 49) The lecture quiz questions are worth 10 pts. Do the following assignments for a total of 45 pts total possible. 3-0 3-2 (email. 10 pts) 3-4 (email, 10 pts) 3-6 (paper and email, 15 pts) Modify the grade program so that the grade input and output is done to files. Create the input file necessary to test your program. (15 pts)