{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lab09_sample - CMPSC 101 Introduction to Algorithmic...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSC 101 Introduction to Algorithmic Processes Summer 2007 LAB 9 – I/O Streams 1. Write a program that will count total number of integers in a file. The file contains nothing but numbers of type int separated by blanks or line breaks. // File name: Lab09_01.cpp // Description: count number of integers in a file // Lab 09-1 // Author: Jaehyun Lim // Date: Friday, August 3, 2007 #include <iostream> #include <cstdlib> #include <fstream> using namespace std; int main() { ifstream in_stream; in_stream.open( "input_p1.txt" ); if (in_stream.fail()) { cout << "Input file opening failed." << endl; exit(1); } // read in numbers from file input_p1.txt // and count total number of integers int counter = 0, num; while (in_stream >> num) { counter++; } // display output on the screen cout << "There are total " << counter << " number of integers " << "in the file input_p1.txt" << endl;
Image of page 1

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

View Full Document Right Arrow Icon
in_stream.close(); return 0; } 2. Write a program that takes its input from a file of double type numbers and outputs the sum and average of the numbers to the screen. You can use your code from problem 1 to determine how many numbers the input file contains.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern