CS114-009 Class 08 Today Exercises Nested loops for statement Built-in functions Announcements ...

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

Transcript of CS114-009 Class 08 Today Exercises Nested loops for statement Built-in functions Announcements ...

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

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)

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

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

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

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?

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

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); }

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

Example: for and while loopsint 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

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

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;

}

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

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?

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

Class ExercisesWrite 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;

}

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

Exercise Write a C++ program that reads in ten real

numbers and prints out the two largest values that you read.

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

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

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

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

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

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

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

Today: pre-defined functions Code has already been written

One small issueNot all functions are automatically loadedNeed to inform compiler if using functions

#include statements

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

#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;

}

Example: compute ab

#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;

}

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

#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;

}

Comments on compute ab

Don’t forget include file Syntax for calling a

functionvariable=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

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

Pre-defined Math Functions 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 );

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

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 twocalculates the distance between those two points

This should be a doublePrints the distance

Dist = (x1-x2)2 + (y1-y2)2

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

End of Class 08

Read pages 180-189 for next time.