Presentation is loading. Please wait.

Presentation is loading. Please wait.

Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical.

Similar presentations


Presentation on theme: "Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical."— Presentation transcript:

1 Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical Engine exists, it will necessarily guide the future course of science.” - Charles Babbage (1792 - 1871)

2 §Office hours today

3 Another way of writing this? if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton for punishment" << endl; }

4 Nested if statements  The inside the braces can contain any valid C++ statements, including if statements! // … some other code here char answer; if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance? "; cin >> answer; if (answer == 'y') cout<< "Your balance is "<<balance<< endl; } else { cout << "Here is your money." << endl; } cout << "Good bye." << endl;

5 Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; //bad style- no indentation (code with proper indentation on next slide)

6 Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; /*else statement always refers to nearest if statement that is within same block as else and not already associated with another else*/

7 Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

8 Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else //Error cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

9 Nested if statements // what is output here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z){ cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; } else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0; }

10 §Output is 4

11 SELF TEST // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

12 SELF TEST // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; cout y) block. Try this code for x=10, y=2, z=3, p=13 and also for x=10, y=2, z=13, p=3 */ } else cout<<5<<endl;//statement5; return 0;}

13 char alarm; int fuel_quantity, temperature, pressure; cout<<"Enter alarm fuel_quantity temperature pressure\n"; cin>> alarm >> fuel_quantity >> temperature>> pressure; if (alarm=='y') { if(fuel_quantity <10){ cout<<"Add more fuel\n"; } else{ if (temperature >90){ if (pressure >100) cout<<"RED ALERT! Shut down motor\n"; else cout<<"Turn on pressure valve\n"; } else { cout<<"Check temperature again after 10 minutes\n"; } else { cout<<"No problem with motor\n"; }

14 §Loops of various sorts are used to repeat a set of statements some number of times.

15 §Print numbers from 0 to 1000

16 int count_down=3; while (count_down > 0) { cout << "Hello "; count_down -= 1; }

17 int count_down=3; while (count_down > 0) { cout << "Hello "; //count_down -= 1; } What happens now?

18 int x = 10; while ( x > 0) { cout << x << endl; x = x – 3; }

19 int x = 10; while (x > 0) { cout << x << endl; x = x – 3; } Output using the comparison x 0?

20 What happens here? int x = 1; while (x != 12) { cout << x << endl; x = x + 2; }

21 Print the odd numbers less than 12 int x = 1; while (x != 12) { cout << x << endl; x = x + 2; } How to fix it?

22 While loops What's wrong with this? int x = 10; while ( x > 0 ); { cout << x << endl; x--; }

23 While-loop and for-loop

24 int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; }

25 The for loop for (initialization; expression; increment) { //statements here } Example: flowchart

26 int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; }

27 int x = 1; while (x < 12) { cout<<x<<endl; x = x + 2; } for (x=1; x<12; x=x+2) { cout<<x<<endl; }

28 int i ; for( i=0; i<10; ++i ) { cout << i << " "; }

29 int i; for( i=0; i<10; ++i ) { cout << i << " " ; } int i; for (i=0; i<10; i++) { cout <<i << " " ; }

30 int x, n=100; for (x=0; x<n; x++){ x=x+1; } cout<<"x after end of loop is "<<x<<endl;

31 int x, n=100; for (x=0; x<n; x++){ x=x+1; } cout<<"x after end of loop is "<<x<<endl; x is the smallest even number >= n

32 SELF TEST int x, n=100; for (x=0; x<n; x++){ cout<<x<<endl; x=x+1; } cout<<"x after end of loop is "<<x<<endl;

33 SELF TEST int x, n=100; for (x=0; x<n; x++){ x=x+1; cout<<x<<endl; } cout<<"x after end of loop is "<<x<<endl;

34 The For Loop int i ; for( i=23; i>=30; ++i ) { cout << i << " "; } for (i=13; i>=10; --i) { cout <<i <<" "; }

35 The For Loop What is wrong here? int i, j, k ; for( i=0; i<10; i-- ) { cout<<i; } for (i=0; i<10; ) { j=i+30; k=j+30; }

36 Fibonacci numbers F1=1 F2=2 Fn=Fn-1 + Fn-2 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 …


Download ppt "Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical."

Similar presentations


Ads by Google