




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
A part of the lecture notes for computer science ii, fall 2006. It covers the topics of vectors, sorting, and computing statistics using vectors. Problem-solving examples and code snippets in c++.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Reading: Koenig & Moo, Chapter 3
#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; }
int count = 0; // Counting and summation variables. int sum = 0; int x; // Input variable
// Read in the scores one at a time, updating the sum & count. The // value of (grades_str >> x) is a reference to the input stream // grades_str. When we reach the end-of-file OR find something that // can’t be read into an integer variable, this condition fails. 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(6) << double(sum) / count << std::endl; return 0; }
vector
a(n−1)/ 2 if n is odd
an/ 2 − 1 + an/ 2 2 if^ n^ is even
double x; std::vector
Note the use of functions and parameter passing in this example:
// Compute the median value of an input set of grades.
#include
void read_scores(std::vector
void compute_avg_and_std_dev(const std::vector
// Compute the standard deviation double sum_sq = 0.0; for (unsigned int i=0; i < s.size(); ++i) { sum_sq += (s[i]-avg) * (s[i]-avg); } std_dev = sqrt(sum_sq / (s.size()-1)); }
double compute_median(const std::vector
// Sort the values in the vector. By default this is increasing order. std::sort(scores_to_sort.begin(), scores_to_sort.end());
// Now, compute and output the median. unsigned int n = scores_to_sort.size();
if (n%2 == 0) // even number of scores return double(scores_to_sort[n/2] + scores_to_sort[n/2-1]) / 2.0; else return double(scores_to_sort[ n/2 ]); // same as (n-1)/2 because n is odd }
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
// 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; }
cout << a.size() << endl << b.size() << endl << c.size() << endl;
5, etc. Write it two ways, one that uses push_back and one that does not use push_back.
#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. while (names_in_str >> one_name) { names.push_back(one_name); }
// 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; }
#include
const int BIN_SIZE = 10;
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, as before 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; }
// Find the maximum value int max_value = scores[0]; for (unsigned int i=1; i<scores.size(); ++i) { if (scores[i] > max_value) max_value = scores[i]; }
// Establish the number of histogram bins unsigned int num_bins = max_value / BIN_SIZE + 1;
// Initialize the vector called histogram to have size num_bins and // to have a 0 at each entry of the vector. std::vector< int > histogram(num_bins, 0);
// Now fill in the histogram. Each score maps to a location in the histogram. for (unsigned int i=0; i<scores.size(); ++i) { int bin = scores[i] / BIN_SIZE; histogram[ bin ] ++ ; }
// Output the histogram for (unsigned int b=0; b<num_bins; ++b) { int lower = b * BIN_SIZE; int upper = lower + BIN_SIZE - 1; std::cout << ’[’ << std::setw(3) << lower << ".." << std::setw(3) << upper << "): " << std::setw(3) << histogram[b] << ’\n’; }
return 0; // Everything ok }