Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS Class 16 Today Announcements Review for Exam

Similar presentations


Presentation on theme: "CS Class 16 Today Announcements Review for Exam"— Presentation transcript:

1 CS114-009 Class 16 Today Announcements Review for Exam
Programming Project #4 due by midnight Exam 2 is Tuesday, 10/28 during class Closed book, closed-notes Bring ½ sheet of notebook paper with hand-written notes on it No calculators needed 0 – 2

2 Exam 2 Topics: Writing and tracing functions Nested loops
Void and non-void functions Prototype vs. definition Parameters: - pass by value, reference, arrays Functions can call other functions Nested loops Reading from /writing to files Testing for eof Formatting output

3 Programming Project #4 Do not assume there will only be 2 lines of data There could be many more lines of data

4 Class Exercise Write a version of FindMin that takes four parameters (four integers). Hint: The easiest way is to invoke some of the other versions that have been written above for a smaller number of arguments. Can you write it in one line of code? 15-26 int FindMin(int w, int x, int y, int z) { return FindMin( FindMin(w,x), FindMin(y,z) ); int FindMin (int w, int x, int y, int z) { int min1 = FindMin (w, x); int min2 = FindMin (y, z); return FindMin (min1, min2); }

5 #include <iostream>
using namespace std; int FindMin(int, int); int FindMin(int, int, int); int main( ) { int a, b, c, d, x, y, z; cin >> a >> b >> c >> d; x = FindMin(a,b); y = FindMin(c,d); z = FindMin(x,y); cout << z << endl; // This is better! x=FindMin(a,b,c); z=FindMin(x, d); return 0; }

6 int FindMin(int x, int y) {
if (x < y) return x; else return y; } int FindMin(int x, int y, int z) { return FindMin(x,z); return FindMin(y,z); // one line of code: // z=FindMin(FindMin(a,b), FindMin(c,d))

7

8 Exam Review

9 In-Class Exercise Write a C++ program that will read the ten numbers shown below from the file data.txt and write the even numbers to the file even.txt and the write the odd numbers to the file odd.txt. You can create the input file data.txt by either 1) using Notepad 2) File / Add New Item / <select a text file> Make sure your input data file is in the same directory as your C++ project. 31 – 45 This is a “harder” class exercise in that they have to actually do something. You might want to put the previous slide back up for them to reference after you’ve discussed the assignment with them

10 #include<iostream>
#include<fstream> using namespace std; // You can find the results from this program in the 2 files odd.txt and even.txt // these files will appear in your project folder - just look for them // Remember, you must create the data.txt file yourself int main () { int value; ifstream Infile; Infile.open("data.txt"); ofstream Oddfile; Oddfile.open("odd.txt"); ofstream Evenfile; Evenfile.open("even.txt"); for(int i=0; i<10; i++){ Infile >> value; if (value%2==0) Evenfile << value << " "; else Oddfile << value << " "; } return 0;

11 Class Exercises Write a program that generates the output below: 1 10
100 1000 10000 100000 Write a program that generates the output below: 45 – 50 Have them do one or the other

12 #include<iostream>
using namespace std; int main () { int num=1; while (num <= ) { cout.setf(ios::right); cout.width(6); cout << num << endl; num=num*10; } return 0;

13 #include<iostream>
using namespace std; int main () { for (int i=0; i<5; i++){ cout.setf(ios::left); cout.width(7); cout << (i*250); cout.setf(ios::right); cout << ( *i) << endl; cout.unsetf(ios::right); // if both right and left set, // the right dominates, so must unsetf to set left again } return 0;

14 Class Exercise Write C++ programs that use nested for loops to display one of the following designs: XXXXXX XXXXXXXXXX XXXXX XXXXX XXXXXXXX X X XXXX XXXXXX X X XXX XXXX X X XX XX X X X XXXXX 18 – 35 Have them figure out two of these

15 #include <iostream>
using namespace std; int main( ) { int i, j, k; for (i=6; i>0; i--){ for (j=(6-i); j>0; j--){ cout << " "; } for (j=0; j<i; j++){ cout << "X"; cout << endl << endl;

16 cout << endl << endl;
for (i=0; i<5; i++){ for (j=0; j<i; j++){ cout<< " "; } for (int k=10-i*2; k>0; k--){ cout << "X";

17 for (int j=0; j<5; j++){ if (i==0 || i==5 || j==0 || j == 4)
for (i=0; i<6; i++){ for (int j=0; j<5; j++){ if (i==0 || i==5 || j==0 || j == 4) cout << "X"; else cout <<" "; } cout << endl << endl;

18 // Another way to do this
for (i=0; i<6; i++){ if (i%5 == 0) cout << "XXXXX" ; else { cout << "X"; for (j=0; j<3; j++) cout << " "; cout <<"X"; } cout << endl << endl; return 0;

19 Class Exercise Re-write your program from the start of class so that it uses three functions: One to read in the array values One to compute the average One to print all values greater than the average int main ( ) { int values[10]; read ( values ); double avg = average ( values ); cout << "average is : " << avg << endl; print ( values, avg ); return 0; }

20 #include<iostream>
using namespace std; void read (int values []){ for (int a=0; a< 10; a++) cin >> values[a]; } double average(int values []){ double sum=0.0, avg; sum=sum+values[a]; avg=sum/10.0; return avg;

21 void print (int values[], double avg){
for (int a=0; a<10; a++) if (values[a] > avg) cout << values[a] << endl; } int main () { int values[10]; read(values); double avg=average(values); cout<< "average is: " << avg<< endl; print(values, avg); return 0;

22 End of Class 16 Read pages 545 – 553 (old text) for next time


Download ppt "CS Class 16 Today Announcements Review for Exam"

Similar presentations


Ads by Google