Dowemo


s.percision(n) 






Set the precision of to n for future output ( or leaves it unchanged if n is omitted ).


setprecision(n)






Returns a value that, when illustrated on an output s, has the effect of call s. Precidion ( n ). Defined in <.


#include <algorithm>


#include <iomanip>


#include <ios>


#include <iostream>


#include <string>


#include <vector>



int main() 


{


 //ask for and read student's name


 std::cout <<"Please enter your first name:" ;


 std::string name;


 std::cin>> name;


 std::cout <<"Hello," <<name <<"!" <<std::endl;



 //ask for and read the midterm and final grades


 std::cout <<"Please enter your midterm and final exam grades:";


 double midterm, final;


 std::cin>> midterm>> final;



 //ask for and read the homework grades


 std::cout <<"Enter all your homework grades,"


 "followed by end-of-file:";


 std::vector<double> homework;


 double x;



 //invariant: homework contains all the homework grades 


 while(std::cin>> x) {


 homework.push_back(x);


 }



 //check that the student entered some homework grades


 typedef std::vector<double>::size_type vec_sz;


 vec_sz size = homework.size();


 if (size == 0 ) {


 std::cout <<"You must enter your grades." 


 "Please try again." <<std::endl;


 return 1;


 }



 //sort the grades


 std::sort(homework.begin(), homework.end());



 //compute the median homework grade


 vec_sz mid = size/2;


 double median;


 median = size % 2? homework[mid] : (homework[mid - 1] + homework[mid])/2;



 //compute and write the final grade


 std::streamsize prec = std::cout.precision();


 std::cout <<"Your final grade is" <<std::setprecision(3) 


 <<0.2 * midterm + 0.4 * final + 0.4 * median


 <<std::setprecision(prec) <<std::endl;


 return 0;


}









Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs