{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lab09_sample

# Lab09_sample - CMPSC 101 Introduction to Algorithmic...

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

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;

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern