1 / 18

CS114-009 Class 08

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).

kieve
Download Presentation

CS114-009 Class 08

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 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 Class exercises (group project – give solutions in class) Important: figure out your algorithm before you start to code these problems

  3. Can have Nested Loops #include <iostream> 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 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); } Loops

  5. 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 Example: for and while loops

  6. 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; } Comments on the for loop

  7. Class Exercises • What is the output of the following? #include <iostream> 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. Write using while #include <iostream> 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 <iostream> using namespace std; int main( ) { int a=5, b=15; while (a < b) { cout << "UA" << endl; a += 2; b--; } return 0; } Class Exercises

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

  10. 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 Programs will get bigger

  11. 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 Need to organize programs

  12. 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 Two basic types of functions

  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 <iostream> #include <cmath> 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; } #include <iostream> 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; } Example: compute ab

  15. #include <iostream> #include <cmath> 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; } 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 Comments on compute ab

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

  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 = (x1-x2)2 + (y1-y2)2

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

More Related