Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS114-009 Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, e-mail group solution to in-class.

Similar presentations


Presentation on theme: "CS114-009 Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, e-mail group solution to in-class."— Presentation transcript:

1 CS114-009 Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, e-mail group solution to in-class exercise before Thurs. 9/18 at 9 am  Programming Project #2, due Tues. 9/23 by midnight  Today’s lecture: pages 143-177 (145-179)

2 Class exercises (group project – give solutions in class) Write a C++ program that reads in 20 integers, counts the number of odd and even numbers, and prints out the number of odd and even numbers  Hint: what does num%2 do? Write a C++ program that reads in 10 integers and prints out the largest and smallest values seen Write a C++ program that reads in 20 characters (you will only see vowels in the input – a, e, i, o, and u) and prints out the vowel that occurred the most often Important: figure out your algorithm before you start to code these problems

3 Can have Nested Loops #include using namespace std; int main(){ int a=0,b=0; while (a< 2){ cout << “a= “ << a << endl; while (b < 3) { cout<<“b= “ << b << endl; b++; } b=0; a++; } return 0; } What is the output of this program?

4 Loops Loops involve  Initialization  Test for continuation  Update action The for statement  clean iteration loop  equivalent to while  semi-colons (shown in red) are important Basic syntax for ( initialization ; test to continue ; update action) { statement(s); }

5 Example: for and while loops int main( ) { int z; z = 0; while (z < 10) { cout<< z << endl; z++; } return 0; } int main( ) { int z; for (z=0; z<10; z++) { cout << z << endl; } return 0; } // purple = initialization // red = test to continue // green = updates

6 Comments on the for loop Can declare counter variables that are “local” to this loop int main( ) { for (int a=0; a<10; a++) cout << a << endl; for (int b=10; b>=0; b--) cout << b << endl; } Can do nothing or more than one statement in each part of the for loop int main( ) { int a, b; for (a=0, b=1; a<5; a++, b*=2) cout << a+b << endl; for (a=0; a<5; ) cout << a++ << endl; }

7 Class Exercises What is the output of the following? #include using namespace std; int main( ) { for (int a=0; ; a++) cout << "hello world" << endl; return 0; } What modification(s) should be made to display “hello world” exactly 12 times?

8 Class Exercises Write using while #include using namespace std; int main( ) { for (int a=0, b=5; a < b; a++, b--) cout << "YES" << endl; for (int z=10; z>5; z--) cout << "NO" << endl; return 0; } write using for #include using namespace std; int main( ) { int a=5, b=15; while (a < b) { cout << "UA" << endl; a += 2; b--; } return 0; }

9 Exercise Write a C++ program that reads in ten real numbers and prints out the two largest values that you read.

10 Programs will get bigger Examples  Operating Systems DOS, Windows 3.1, Windows 95, Windows 2000, Windows XP, Vista Currently about 40M lines  Word processing Office 2007 is version 12 of Office  myBama

11 Need to organize programs Break into pieces  Functions Why?  Can’t handle one single main function with over 1,000,000 lines of code  Allows us to reuse what others have done

12 Two basic types of functions Pre-defined functions (built-in functions)  Basic activities cin&cout  math related operations Square root Sine and cosine User-defined functions (programmer-defined)  must write these yourself

13 Today: pre-defined functions Code has already been written One small issue  Not all functions are automatically loaded  Need to inform compiler if using functions #include statements

14 #include using namespace std; int main( ) { double a, b, ans; cout << "Enter a & b: "; cin >> a >> b; ans = pow (a, b); cout << ans << endl; return 0; } Example: compute a b #include using namespace std; int main( ) { int a, b, ans = 1; cin >> a >> b; while (b > 0) { ans = ans * a; b = b – 1; } cout << ans << endl; return 0; }

15 #include using namespace std; int main( ) { double a, b, ans; cout << "Enter a & b: "; cin >> a >> b; ans = pow (a, b); cout << ans << endl; return 0; } Comments on compute a b Don’t forget include file Syntax for calling a function variable=function(params);  Parameters are values passed to the function Other variables Numbers  Parentheses are mandatory, even with no parameters  Don’t always need to assign the results

16 Pre-defined Math Functions abs  Requires #include  Return the absolute value double x, y, x = fabs( y ); Table on page 185 (187) has common mathematical functions pow  Requires #include  Return x y double x, y, z, z = pow( x, y ); sqrt  Requires #include  Returns square root double x, y, y = sqrt( x );

17 Class Exercises Write a C++ program that:  reads in four integers the first two numbers represent the x and y coordinates for point one the last two numbers represent the x and y coordinates for point two  calculates the distance between those two points This should be a double  Prints the distance Dist =  (x 1 -x 2 ) 2 + (y 1 -y 2 ) 2

18 End of Class 08 Read pages 180-189 for next time.


Download ppt "CS114-009 Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, e-mail group solution to in-class."

Similar presentations


Ads by Google