Use test_algorithms.cpp as an example. Write a C++ program using STL vector and algorithm to do the following: Create a vector of five items of type double with the values: 4.0, 1.5, 2.0, 3.0, 1.0 Print out the vector. Sort the vector. Print out the vector. Reverse the order of the vector. Print out the vector. Make the name of the file submitted: my_algorithm.C or my_algorithm.cc or my_algorithm.cpp submit cs291 HW7 my_algorithm.C (or whatever extension)
Use test_list.cpp as an example. Write a C++ program using STL list to do the following: Create a list, L1, of five items of type double with the values: 4.0, 1.5, 2.0, 3.0, 1.0 Print out the list L1. Sort the list L1. Print out the list L1. Reverse the order of the list L1. Print out the list L1. Create a list, L2, of three items of type double with the following values: 7.5, 7.6, 7.7 Merge L2 onto the back of L1. Print out the list L1. ( should be L1 followed by L2 ) Print out the list L2. ( should be empty list ) Make the name of the file submitted, my_list.C or my_list.cc or my_list.cpp submit cs291 HW8 my_list.C (or whatever extension)
Open book. Open note. Bring copies of the homework you submitted. Bring class handouts and your textbook. Be organized, the classroom is crowded and desktops are small. Multiple choice questions based on: lectures, reading assignments and homework. Exam covers book: 462-465 474-478 633 661-663 815-818 825-829 Exam covers homework: HW5-HW8 Exam covers lectures: 12-21
It turns out that utility template functions can operate on many, but not all STL container classes. You are to write a file named my_merge.h and write one template function called my_merge that merges alternate items from lists into a single list. It must also work for vectors. Your "my_merge" template function will look a lot like the #include <algorithm> merge function, see page 843 of text book. You may use the algorithm merge inside your my_merge or do your own code. More details are given below after the test program. Given list L1 with items 1, 3, 5 and list L2 with items 2, 4, 6, 7 build a list with the first item from list 1, the first item from list 2, the second item form list 1, the second item from list 2, etc. If the lists are not the same length, put the left over items on the end of the list being built. See the test program and output for details. The test program is: // my_merge.cc main function to test students my_merge.h // // merge two STL lists (of the same type) to create a third list // that has L1(1), L2(1), L1(2), L2(2), L1(3), L2(3), ... leftovers // #include <iostream> #include <list> #include <vector> using namespace std; #include "my_merge.h" // written by CMSC 291 student and submitted static void put_list(char *name, list<int> L) // local for printing lists { cout << name << " "; list<int>::iterator p = L.begin(); while( p != L.end() ) { cout << *p << " "; // cout operator << must be visible for type *p p++; } cout << endl; } static void put_vector(char *name, vector<double> L) // local printing vectors { cout << name << " "; vector<double>::iterator p = L.begin(); while( p != L.end() ) { cout << *p << " "; // cout operator << must be visible for type *p p++; } cout << endl; } int main() // test program for my_merge.h { list<int> L1; list<int> L2; list<int> L3; // test data L1.push_back(1); L1.push_back(3); L1.push_back(5); L2.push_back(2); L2.push_back(4); L2.push_back(6); L2.push_back(7); put_list("L1", L1); put_list("L2", L2); // test template in my_merge.h on list my_merge(L1.begin(), L1.end(), L2.begin(), L2.end(), L3); put_list("L3", L3); cout << endl; // more test data vector<double> V1; vector<double> V2; vector<double> V3; V1.push_back(1.1); V1.push_back(3.3); V1.push_back(5.5); V1.push_back(6.6); V2.push_back(2.2); V2.push_back(4.4); put_vector("V1", V1); put_vector("V2", V2); // test template in my_merge.h on vector my_merge(V1.begin(), V1.end(), V2.begin(), V2.end(), V3); put_vector("V3", V3); return 0; } test your file my_merge.h using my_merge.cc and you should get the output: L1 1 3 5 L2 2 4 6 7 L3 1 2 3 4 5 6 7 V1 1.1 3.3 5.5 6.6 V2 2.2 4.4 V3 1.1 2.2 3.3 4.4 5.5 6.6 submit cs291 HW9 my_merge.h Notes: It seems interesting, and a bit confusing, that the template profile needs two distinct type names for the two begin() end() pairs. The reason for this may be that a vector and a list can be merged within a single call to my_merge. template <class InIt1, class InIt2, class T> void my_merge(InIt1 begin1, InIt1 end1, InIt2 begin2, InIt2 end2, T &L) could correspond to the function call: my_merge(L1.begin(), L1.end(), V2.begin(), V2.end(), L3); Hint: Since L3 is passed by reference, L3 is the actual parameter corresponding to the formal parameter L, thus you can use statements such as L.push_back(*begin2); // OK because L passed by reference begin2++; // OK because begin2 is an input
Write a C++ program to read (name,score) pairs and print out the name and best score. // find_max.cpp read find_max.dat into a vector // after the data is read in (print data as reading it) // print the name with the largest value (highest score) // // A simple main program is good enough // You may use a loop or STL to find the maximum value // test run your program using find_max < find_max.dat // // submit cs291 HW10 find_max.cpp or find_max.C or find_max.cc // // As usual, do it in C++ // Smaller is better // Simpler is better // Here is the file find_max.dat (the file does not contain "// ") // // Joe 3 // Jim 5 // Mary 4 // Mike 1 // John 6 // Jane 2
Write a standard C++ program that pops an X Window and draws a rectangle of some reasonable size. Call the program draw_rect.cc and submit cs291 HW11 draw_rect.cc Further, submit cs291 HW11 Makefile a make file that compiles your program to an executable named draw_rect . Submit any additional files that are needed by your make file. You may do this using all your own code or strip down test_x_plot_d.cc A sample Makefile is Makefile If you use test_x_plot_d.cc, remove the calls to "delay" and remove all drawing except for a call to draw a rectangle. You will need to submit x_plot_d.h and x_plot_d.cc The source files can be captured from this WEB page, but watch out for tab character being converted to spaces. e.g. in the Makefile, there is a single tab character in front of g++ You may wish to use a "cp" command on a gl machine to get the files: cp /afs/umbc.edu/users/s/q/squire/pub/oop/Maketestx . cp /afs/umbc.edu/users/s/q/squire/pub/oop/test_x_plot_d.cc . cp /afs/umbc.edu/users/s/q/squire/pub/oop/x_plot_d.h . cp /afs/umbc.edu/users/s/q/squire/pub/oop/x_plot_d.c . cp /afs/umbc.edu/users/s/q/squire/pub/oop/delay.h . cp /afs/umbc.edu/users/s/q/squire/pub/oop/delay.c . Don't forget that final dot "." it means "here". Then: mv Maketestx Makefile make test_x_plot_d You should see two screens for about 3 seconds each.
Open book. Open note. Bring copies of completed homework. Multiple choice questions based on lectures, reading assignments and homework. Exam covers book: all reading assignments Exam covers homework: all Exam covers lectures: all
Last updated 1/1/01