Guest User

Untitled

a guest
Nov 24th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /* Author: Jensen Vu
  2. CSCI 127 - HWK 7
  3. File: NASA.cpp
  4. */
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <fstream>
  9. #include <vector>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16. fstream file; //reads file
  17. file.open("data.txt"); //opens file
  18.  
  19. int count = 0; //keeps track of # lines read
  20.  
  21. vector<double> distances; //holds the distances
  22.  
  23. while(!file.eof())
  24. {
  25. string distance;
  26. getline(file, distance); // grabs line from file as a string
  27. double distanceNum = atof(distance.c_str()); // converts the distance string to float
  28. distances.push_back(distanceNum); //stores the converted number in the vector
  29. count ++; //increments the count of the number of read lines
  30. }
  31.  
  32. file.close();
  33.  
  34. for(int i = 0; i < distances.size(); i++)
  35. cout << distances[i] << "\n\n";
  36. cout << "Lines Read: " << count << "\n\n";
  37.  
  38. double totalDist = 0.00;
  39.  
  40.  
  41. for(double i=0;i<distances.size(); i++)
  42. {
  43. totalDist += distances[i]; //adds up elements in vector
  44. }
  45. cout << "Average distance = " << totalDist/count << "\n\n";
  46.  
  47. double largeDist = 0.00;
  48. for(int i = 0; i < distances.size(); i++) // if current distance is larger than the largest distance seen so far
  49. if(distances[i] > largeDist)
  50. largeDist = distances[i];
  51. cout << "Largest distance traveled: " << largeDist << "\n\n";
  52.  
  53.  
  54. double smallDist = distances[0];
  55. for(int i = 0; i < distances.size(); i++)
  56. if(distances[i] < smallDist)
  57. smallDist = distances[i];
  58. cout << "Smallest distance traveled: " << smallDist << "\n\n";
  59.  
  60.  
  61.  
  62.  
  63.  
  64. int lastDist = 238857; // asteroid’s last distance from the moon (miles)
  65.  
  66.  
  67. cout << "Estimated time of impact is approximately " << (lastDist / (totalDist/count))/ 3600 << " hours.";
  68.  
  69.  
  70.  
  71.  
  72.  
  73. return 1;
  74. }
Add Comment
Please, Sign In to add comment