Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS114-009 Class 23 Today  MergeSort Code  Review for Exam 3 Announcements  Exam 3 – Nov. 25 th in class Closed book, closed-notes Bring ½ sheet of notebook.

Similar presentations


Presentation on theme: "CS114-009 Class 23 Today  MergeSort Code  Review for Exam 3 Announcements  Exam 3 – Nov. 25 th in class Closed book, closed-notes Bring ½ sheet of notebook."— Presentation transcript:

1 CS114-009 Class 23 Today  MergeSort Code  Review for Exam 3 Announcements  Exam 3 – Nov. 25 th in class Closed book, closed-notes Bring ½ sheet of notebook paper with hand-written notes on it No calculators needed  Programming Project #6 due Nov. 20 by midnight e-mail to vrbsky@cs.ua.eduvrbsky@cs.ua.edu  Programming Project #7 is optional/extra credit. Will be posted later.

2 Examples of recursion void MergeSort(int array[], int begin, int end) { int mid; if (end - begin <= 1) return; mid = (begin + end) / 2; MergeSort(array, begin, mid); MergeSort(array, mid, end); Merge(array, begin, mid, end); }

3 Class Exercise Write a C++ program that reads in a line of input as a string (using getline), and then counts the number of spaces in it. Print the length of the string and the number of spaces found.

4 #include using namespace std; int main ( ) { string str; int blanks=0; getline(cin,str); for (int i = 0; i< str.length(); i++){ if (str[i]==' ') blanks++; } cout << "blanks = " << blanks << endl; return 0; }

5 Class Exercise Write a C++ program that reads in strings (stopping when you enter the word “quit”) and prints out the word in a form of “Pig Latin.” Starts with:  Vowel: append “way” to the end.  Consonant: move first letter to the end and add “ay”  enter = enterway  number = umbernay

6 #include using namespace std; int main( ) { // Prints out the word in a form of Pig Latin string str, newstr; cin >> str; while (str != "quit"){ if ((str.at(0)=='a') || (str.at(0)=='e') || (str.at(0)=='i') || (str.at(0)=='o') || (str.at(0)=='u') || (str.at(0)=='y') || (str.at(0)=='A') || (str.at(0)=='E') || (str.at(0)=='I') || (str.at(0)=='O') || (str.at(0)=='U') || (str.at(0)=='Y') ) newstr=str+"way"; else newstr=str.substr(1, str.length()-1)+ str.at(0) + "ay"; cout << newstr << endl; cin >> str; } return 0; }

7 Class Exercises Write a C++ program that reads in ten names, storing them in an array. Print the alphabetically first name (lexicographic order), along with the longest name. Since the names might contain spaces, read them in one per line using getline  getline(cin, string); Possible sample names  Homer Jay Simpson  Marge Bouvier Simpson  Bartholomew Jo-Jo Simpson  Lisa Marie Simpson  Margaret Simpson  Snowball  Santa’s Little Helper  Nedward Flanders  Maude Flanders  Apu Nahasapeemapetilon  Clancy Wiggum  Dr. Julius Hibbert  Charles Montgomery Burns  Waylon Smithers

8 #include using namespace std; // Prints out the first word (alphabetically) and longest word int main ( ) { string str[10]; string first="ZZZ"; int longest=0; for (int i = 0; i< 10; i++){ getline(cin, str[i]); if (str[i]< first) first=str[i]; //cout << str[i].length() << endl; if (str[i].length() > longest) longest=str[i].length(); } cout << "first = " << first << endl; cout << "longest = " << longest << endl; return 0; }

9 Class Exercise The class we have started is on our web page: Big_INT.cpp Big_INT.cpp Download this program, then compile and run  Modify Write( ) so that it does not print leading zeros in the number  Modify Add( ) so that it is also capable of adding two negative numbers  Modify Read( ) so that it erases the previous contents of the object

10 void INT::Write() { int a = 0, flag=0; if (sign == 'n') cout << '-'; while (a < 100){ if((digits[a]!=0)||(flag==1)){ cout<<digits[a]; flag=1; } a++; }

11 void INT::Add(INT num) { if (num.sign != sign) cout << "This is too hard, I can't do it -- yet!" << endl; else { int a, carry = 0, total; for (a=99; a>=0; a--) { total = digits[a] + num.digits[a] + carry; if (total > 9) carry = 1; else carry = 0; digits[a] = total % 10; }

12 Add this to the code for Read() //erase previous contents for (int a=0; a<100; a++) digits[a] = 0; sign = 'p';

13 Class Exercise Write the definitions to the following methods, whose prototypes you’ll find on cars.cpp setCarInfo() – set a car’s information, given the make, model, type, doors, quantity of the car in stock, cylinder engine, and liters of the engine load() – reads in information from the file cars.txt, and uses setCarInfo() to set the information for an instance of car  Data for each car is on one line. The order of the data: Make, Model, Type, Doors, Liters, Cylinders, Quantity

14 void setCarInfo(Car& c, string ma, string mo, string t, int d, int cy, double l, int q){ c.setMake(ma); c.setModel(mo); c.setType(t); c.setDoors(d); c.setCylinders(cy); c.setLiters(l); c.setQty(q); cout << c.Print() << endl; }

15 void load(Car cars[8]){ string ma, mo, t; int d, cy, q; double l; ifstream cars; cars.open("cars.txt"); for (int i=0; i< 8; i++){ cin >> ma >> mo >> t >> d >> cy >> l >> q; cars[i].setCarInfo(cars, ma, mo, t, d, cy, l, q); }

16 Class exercise Download the file box.cpp from our class web pagebox.cpp Write the definitions to the following methods, whose prototypes you’ll find in box.cpp  volume() – given the height, width, and length of a box, return its volume  compare() – compares the volume of a box to another box; returns true if the volumes of the 2 boxes are equivalent, otherwise returns false. Insert lines of code into main( ) as indicated to utilize the appropriate member function. (see comments in box.cpp)

17 double volume(){ //write volume method return length * width * height; } bool compare(Box b){ //Here's a long version: /*if (volume() == b.volume()) return true; else return false;*/ // Here's a short version: return (volume() == b.volume()); }

18 Class exercise Download the file STRING.cpp from our class web pageSTRING.cpp Write the definitions to the following methods, whose prototypes you’ll find in STRING.cpp  print() – prints a STRING  reverse() – reverses a STRING Finish main() following the commented instructions in the STRING.cpp file.

19 void reverse(){ char temp; for (int a = 0, b = length-1; a<b; a++, b--){ temp = s[b]; s[b] = s[a]; s[a] = temp; } void print(){ for (int a = 0; a<length; a++){ cout<<s[a]; }

20 End of Class 23


Download ppt "CS114-009 Class 23 Today  MergeSort Code  Review for Exam 3 Announcements  Exam 3 – Nov. 25 th in class Closed book, closed-notes Bring ½ sheet of notebook."

Similar presentations


Ads by Google