Lab7 - Exercise 4 // Program Shell4 averages the values on...

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

View Full Document Right Arrow Icon
/* Exercise 1 // Program Shell3 reads miles and hours and prints miles // per hour. / #include <iostream> #include <iomanip> using namespace std; u void GetData (float &miles, float &hours); v int main () { float miles; float hours; float milesPerHour; cout << fixed << showpoint; GetData (miles, hours); milesPerHour = miles / hours; cout << setw(10) << miles <<" miles" << setw(10) << hours <<" hours" << setw(10) << milesPerHour <<" miles per hour" << endl; return 0; } //***************************************************** / { cout<<"Please enter the number of miles and the number of hours, respectively"<<endl; cin>>miles>>hours; } Exercise 3 The answers in exercise 1 and 2 are different because milesPerHour=miles/hours. In exercise 1, the miles are entered first and hours second, so you get the correct value for milesPerHour. In exercise 2, hours are entered first and miles second, but milesPerHour is still equal to miles/hours, so the number is different.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Exercise 4 // Program Shell4 averages the values on file dataFile. / #include <iostream> #include <fstream> using namespace std; u void ReadAndSum(ifstream&, int&, int&, int&, float&); // Reads, counts, and averages values on a file. / int main () { ifstream dataFile; int number; int sum=0; int numberOfValues; float average; cout << fixed << showpoint; dataFile.open("Shell4.dat"); ReadAndSum(dataFile, number, sum, numberOfValues, average); cout << "The average of " << numberOfValues << " values is " << average << endl; return 0; } //*************************************** / void ReadAndSum (ifstream &dataFile, int &number, int &sum, int &numberOfValues, float &average) // Reads, counts, and averages values on a file, then sends to the main function to print. { dataFile>>number; d while(dataFile) { dataFile>>number; sum=sum+number; numberOfValues++; n } average=sum/numberOfValues; } */...
View Full Document

This note was uploaded on 04/25/2010 for the course CECS CIS 150 taught by Professor L.tsui during the Fall '07 term at University of Michigan-Dearborn.

Page1 / 2

Lab7 - Exercise 4 // Program Shell4 averages the values on...

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

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