| Instructor: | David Goldschmidt, Ph.D. |
|---|---|
| Office Hours: | after class |
| Email: | click here to email me |
// echoname.cpp <== click here for file // ask for a person's name, then generate a framed greeting #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter your first name and last name: "; string name; cin >> name; string lname; cin >> lname; // build the message that we intend to write const string greeting = "Hello, " + name + "!"; // build the second and fourth lines of the output const string spaces( greeting.size(), ' ' ); const string second = "* " + spaces + " *"; // build the first and fifth lines of the output const string first( second.size(), '*' ); // write it all cout << endl; cout << first << endl; cout << second << endl; cout << "* " << greeting << " *" << endl; cout << second << endl; cout << first << endl; cout << endl; cout << "Thanks, " << lname << endl; return 0; }
C++ Strings:
Please enter your first name and last name: David Goldschmidt ***************** * * * Hello, David! * * * ***************** Thanks, Goldschmidt
// echonamediag.cpp <== click here for file // ask for a person's name, then generate a diagonal greeting #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter your first name and last name: "; string name; cin >> name; string lname; cin >> lname; const string star_line( name.size() + 4, '*' ); cout << star_line << '\n'; const string blanks( name.size() + 2, ' ' ); cout << '*' << blanks << "*\n"; for ( int i = 0 ; i < name.size() ; i++ ) { cout << "* "; for ( int j = 0 ; j < i ; j++ ) { cout << ' '; } cout << name[i]; for ( int j = i ; j < name.size() ; j++ ) { cout << ' '; } cout << "*\n"; } cout << '*' << blanks << "*\n"; cout << star_line << '\n'; cout << "Thanks, " << lname << endl; return 0; }
More C++ Strings:
Please enter your first name and last name: David Goldschmidt ********* * * * D * * a * * v * * i * * d * * * ********* Thanks, Goldschmidt
cout << '\n'; cout << endl;
L-Values and R-Values:
string a = "Kim"; string b = "Tom"; b[1] = a[1];
// vec.cpp <== click here for file #include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { // A vector is a "container class" // Unlike an array, a vector can grow/shrink as necessary // Vectors can store any type of data int grade; vector<int> grades; cout << "Enter grades as integers (CTRL-D to end): " << endl; // or CTRL-Z on Windows while ( cin >> grade ) // returns 0 when CTRL-D is input { grades.push_back( grade ); } cout << "Size of vector is: " << grades.size() << endl; sort( grades.begin(), grades.end() ); for ( int i = 0 ; i < grades.size() ; i++ ) { cout << grades[i] << endl; } return 0; }
Vectors:
vector<int> scores;
Initializing a Vector through the use of Constructors:
vector<int> a; // #1 vector<double> b( 100, 3.14 ); // #2 int n = 100; vector<int> c( n * n ); // #3 vector<double> d( b ); // #4 vector<int> e( b ); // #5
Sorting:
double x;
vector<double> a;
while ( cin >> x ) {
a.push_back(x);
}
sort( a.begin(), a.end() );
for ( unsigned int i = 0 ; i < a.size() ; i++ ) {
cout << a[i] << '\n';
}
// mediangrades.cpp <== click here for file // Reads from a file (e.g. <a href="docs/csci1200/grades.txt" target="_blank">grades.txt</a>), then calculates // and displays average, standard deviation, and median. #include <algorithm> #include <cmath> #include <fstream> #include <iomanip> #include <iostream> #include <vector> using namespace std; void read_grades( vector<int> &grades, ifstream &grade_stream ) { int grade; while ( grade_stream >> grade ) { grades.push_back( grade ); } } // (we're passing grades by reference to avoid a COPY) void // const means grades cannot change compute_avg_and_std_dev( const vector<int> &grades, double &avg, double &std_dev ) { // Compute the average int sum=0; for ( unsigned int i = 0; i < grades.size(); i++ ) { sum += grades[i]; } avg = double(sum) / grades.size(); // Compute the standard deviation double sum_sq = 0.0; for ( unsigned int i = 0; i < grades.size(); i++ ) { sum_sq += ( grades[i] - avg ) * ( grades[i] - avg ); } std_dev = sqrt( sum_sq / ( grades.size() - 1 ) ); } double compute_median( const vector<int> &grades ) //compute_median( vector<int> grades ) { // Create a copy of the vector vector<int> grades_to_sort( grades ); // Sort the values in the vector (default is increasing order) sort( grades_to_sort.begin(), grades_to_sort.end() ); // Compute median unsigned int n = grades_to_sort.size(); if ( n % 2 == 0 ) // even number of grades return double( grades_to_sort[ n / 2 ] + grades_to_sort[ n / 2 - 1] ) / 2.0; else return double( grades_to_sort[ n / 2 ] ); } int main( int argc, char* argv[] ) { cout << "argc is " << argc << endl; if ( argc != 2 ) { cerr << "Usage: " << argv[0] << " <grades-file>\n"; return 1; } // Open the grades.txt file (i.e. argv[1]) // (bail if an error occurs) ifstream grades_stream( argv[1] ); if ( !grades_stream ) { cerr << "Cannot open grades file " << argv[1] << endl; return 1; } vector<int> grades; read_grades( grades, grades_stream ); // Quit with an error message if no grades loaded if ( grades.size() == 0 ) { cout << "No grades entered." << endl; return 1; } // Compute average, standard deviation, and median double average, std_dev; compute_avg_and_std_dev( grades, average, std_dev ); double median = compute_median( grades ); cout << "From " << grades.size() << " grades: \n" << " average is " << setprecision(3) << average << '\n' << " std_dev is " << std_dev << '\n' << " median is " << median << endl; return 0; // Everything ok }
Passing Vectors (and Strings) As Parameters:
The following bullet items outline rules for passing vector objects as parameters. The same rules apply to passing string objects.