






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: DATA STRUCTURES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Spring 2006;
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







4.3 Example: Reading Numbers and Computing the Average
// Program: average.cpp // Author: Chuck Stewart // Purpose: Compute the average of an input set of grades. This // demonstrates input of a sequence of integers, computing the // average, and manipulating the output precision.
#include
int main( int argc, char* argv[] ) {
if ( argc != 2 ) { std::cerr << "Usage: " << argv[0] << " grades-file\n"; return 1; }
std::ifstream grades_str( argv[1] ); if ( !grades_str ) { std::cerr << "Can not open the grades file " << argv[1] << "\n"; return 1; }
// Counting and summation variables. int count = 0; int sum = 0; // Input variable int x;
// Read in the scores one at a time. Add each score to the sum and // increment the count. // // The value of the expression grades_str >> x is a reference to the // input stream grades_str. The while condition uses this to test // grades_str to see if it is ok. If it is not, the test is false // and the loop ends. The most common cause of this is finding the // end of the input, but it would be false is something other than // an integer (or whitespace), such as a letter, is in the input // stream. while ( grades_str >> x ) { ++ count ; sum += x; }
// Output the result. Set the precision to 3. std::cout << "The average of " << count << " grades is " << std::setprecision(3) << double(sum) / count << std::endl;
return 0; }
4.6 Example: Using Vectors to Compute Standard Deviation
// Program: average_and_deviation.cpp // Author: Chuck Stewart // Purpose: Compute the average and standard deviation of an input // set of grades. This introduces the use of a vector to store // the grades upon input.
#include
int main( int argc, char* argv[] ) {
if ( argc != 2 ) { std::cerr << "Usage: " << argv[0] << " grades-file\n"; return 1; } std::ifstream grades_str( argv[1] ); if ( !grades_str ) { std::cerr << "Can not open the grades file " << argv[1] << "\n"; return 1; }
std::vector
// Read the scores, appending each to the end of the vector while ( grades_str >> x ) { scores.push_back(x); }
// Quit with an error message if too few scores. if ( scores.size() == 0 ) { std::cout << "No scores entered. Please try again!" << std::endl; return 1; }
// Compute and output the average value. int sum=0; // Accumulation of the values for ( unsigned int i = 0; i < scores.size(); ++ i ) { sum += scores[i]; }
double average = double(sum) / scores.size(); std::cout << "The average of " << scores.size() << " grades is " << std::setprecision(3) << average << std::endl;
// Exercise: compute and output the standard deviation.
return 0; }
4.7 Median
an/ 2 − 1 + an/ 2
4.8 Standard Library Sort Function
std::vector
// Read the scores, as before read_scores( scores, grades_str );
// Quit with an error message if too few scores. if ( scores.size() == 0 ) { std::cout << "No scores entered. Please try again!" << std::endl; return 1; }
// Compute the average, standard deviation and median double average, std_dev; compute_avg_and_std_dev( scores, average, std_dev ); double median = compute_median( scores );
// Output std::cout << "Among " << scores.size() << " grades: \n" << " average = " << std::setprecision(3) << average << ’\n’ << " std_dev = " << std_dev << ’\n’ << " median = " << median << std::endl;
return 0; }
4.10 Passing Vectors (and Strings) As Parameters
4.11 Initializing a Vector — The Use of Constructors
4.12 Exercises
std::cout << ’[’ << std::setw(3) << lower << ".." << std::setw(3) << upper << "): " << std::setw(3) << histogram[b] << ’\n’; }
return 0; // Everything ok }
4.14 Example: Alphabetize Strings
// Program: alphabetize.cpp // Author: Chuck Stewart // Purpose: Demonstrate using a vector of strings and sorting.
#include
int main( int argc, char* argv[] ) {
if ( argc != 3 ) { std::cerr << "Usage: " << argv[0] << " names-in names-out\n"; return 1; } std::ifstream names_in_str( argv[1] ); if ( !names_in_str ) { std::cerr << "Can not open the names file " << argv[1] << "\n"; return 1; } std::ofstream names_out_str( argv[2] ); if ( !names_out_str ) { std::cerr << "Can not open the output names file " << argv[2] << "\n"; return 1; }
std::vector<std::string> names; std::string one_name;
// Read the strings one at a time and add them to the back of the // vector. The reading loop ends when the end of file has been reached. while ( names_in_str >> one_name ) { names.push_back( one_name ); }
// Sort the vector of strings in the same manner that we sorted the // vector of doubles. The sort function uses (automatically) the < // operator which is defined on strings. This operator compares // strings "lexicographically". std::sort( names.begin(), names.end() );
names_out_str << "\n" << "Here are the names in alphabetical order." << std::endl; for ( unsigned int i=0; i<names.size(); ++i ) { names_out_str << names[i] << std::endl; }
return 0; }