Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Jensen Vu
- CSCI 127 - HWK 7
- File: NASA.cpp
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- int main()
- {
- fstream file; //reads file
- file.open("data.txt"); //opens file
- int count = 0; //keeps track of # lines read
- vector<double> distances; //holds the distances
- while(!file.eof())
- {
- string distance;
- getline(file, distance); // grabs line from file as a string
- double distanceNum = atof(distance.c_str()); // converts the distance string to float
- distances.push_back(distanceNum); //stores the converted number in the vector
- count ++; //increments the count of the number of read lines
- }
- file.close();
- for(int i = 0; i < distances.size(); i++)
- cout << distances[i] << "\n\n";
- cout << "Lines Read: " << count << "\n\n";
- double totalDist = 0.00;
- for(double i=0;i<distances.size(); i++)
- {
- totalDist += distances[i]; //adds up elements in vector
- }
- cout << "Average distance = " << totalDist/count << "\n\n";
- double largeDist = 0.00;
- for(int i = 0; i < distances.size(); i++) // if current distance is larger than the largest distance seen so far
- if(distances[i] > largeDist)
- largeDist = distances[i];
- cout << "Largest distance traveled: " << largeDist << "\n\n";
- double smallDist = distances[0];
- for(int i = 0; i < distances.size(); i++)
- if(distances[i] < smallDist)
- smallDist = distances[i];
- cout << "Smallest distance traveled: " << smallDist << "\n\n";
- int lastDist = 238857; // asteroids last distance from the moon (miles)
- cout << "Estimated time of impact is approximately " << (lastDist / (totalDist/count))/ 3600 << " hours.";
- return 1;
- }
Add Comment
Please, Sign In to add comment