Lecture 4 - Working with Batches of Data

Lecture 4 - Working with Batches of Data - Working with...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Working with Batches of Data Lecture 4 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/˜hkaiser/fall_2011/csc1254.html
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Abstract So far we looked at simple ‘read a string – print a string’ problems. Now we will look at more complex problems involving multiple pieces of similar data. 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 2
Background image of page 2
Computing Student Grades Calculate the overall grade of a student (20% midterm, 40% final exam, 40% homework) ▫ Read values interactively ▫ Arbitrary number of homework grades 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Computing Student Grades // #include directives // using directives int main() { // ask for and read the student's name // ask and read the midterm and final grades // ask for the homework grades // write the result return 0; } 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 4
Background image of page 4
#include and using directives // #include directives #include <iomanip> // std::setprecision #include <ios> // std::streamsize #include <iostream> #include <string> // using directives using std::cin; using std::setprecision; using std::cout; using std::string; using std::endl; using std::streamsize; int main() { // ask for and read the student's name cout << "Please enter your first name: " ; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // . .. return 0; } 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Ask and Read the Grades int main() { // . .. // ask and read the midterm and final grades cout << "Please enter your midterm and final grades: " ; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: " ; int count = 0; // the number and sum of grades read so far double sum = 0, x; // a variable to read the grades into // invariant: // we have read 'count' number of grades so far // 'sum' is the sum of the first 'count' grades while (cin >> x) { ++count; sum += x; } 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 6
Background image of page 6
Computing Student Grades Calculate and output overall grade //. .. // write the result streamsize prec = cout.precision(); cout << name << ", your final grade is: " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << setprecision(prec) << endl; return 0; } 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Testing for end of Input We have seen: while (cin >> x) { /*…*/ } Generally istreams can be used as conditions: if (cin >> x) { /*…*/ } Which is equivalent to: cin >> x ; if (cin) { /*…*/ } Detects: ▫ Operation reached end of input ▫ Input characters are not compatible with expected type ▫ System has detected hardware failure 9/1/2011, Lecture 4 CSC 1254, Fall 2011, Working with Batches of Data 8
Background image of page 8
Calculating the Median Value So far we throw away the values right after
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/29/2011 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.

Page1 / 25

Lecture 4 - Working with Batches of Data - Working with...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online