Advertisement
J2112O

Sort not working

Mar 30th, 2015
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1.  1 // compute mean and median temperature                                                                        
  2.   2 #include <iostream>                                                                                          
  3.   3 #include <string>                                                                                            
  4.   4 #include <vector>                                                                                            
  5.   5 #include <algorithm>                                                                                          
  6.   6                                                                                                              
  7.   7 using std::cout;                                                                                              
  8.   8 using std::cin;                                                                                              
  9.   9 using std::string;                                                                                            
  10.  10 using std::vector;                                                                                            
  11.  11                                                                                                              
  12.  12 int main()                                                                                                    
  13.  13 {                                                                                                            
  14.  14     vector<double> temps;                                                                                    
  15.  15     for (double temp; cin>>temp; )                                                                            
  16.  16         temps.push_back(temp);                                                                                
  17.  17                                                                                                              
  18.  18     // compute mean temperature                                                                              
  19.  19     double sum = 0;                                                                                          
  20.  20     for (int x : temps) sum += x;                                                                            
  21.  21     cout << "Average temperature: "  << sum/temps.size() << '\n';                                            
  22.  22                                                                                                              
  23.  23     // compute median temperature                                                                            
  24.  24     sort(temps);                                                                                              
  25.  25     cout << "Median temperature: " << temps[temps.size()/2] << '\n';                                          
  26.  26                                                                                                              
  27.  27     return 0;                                                                                                
  28.  28 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement