1 / 24

Tuesday, December 26, 2006

Tuesday, December 26, 2006. There was a most ingenious architect who contrived a new method for building houses, By beginning at the roof and working downward to the foundation. - Jonathan Swift, Gulliver’s Travels. Equivalent statements?. Nested Switch. char ch1, ch2; cin>>ch1>>ch2;

devon
Download Presentation

Tuesday, December 26, 2006

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. Tuesday, December 26, 2006 There was a most ingenious architect who contrived a new method for building houses, By beginning at the roof and working downward to the foundation. - Jonathan Swift, Gulliver’s Travels

  2. Equivalent statements? Nested Switch char ch1, ch2; cin>>ch1>>ch2; switch(ch1) { case 'A': cout << "This A is part of outer switch\n"; switch(ch2) { case 'A': cout << "This A is part of inner switch\n"; break; case 'B': cout << "This B is part of inner switch\n"; } break; case 'B': cout << "This B is part of outer switch\n"; break; }

  3. Equivalent statements? Nested Switch char ch1, ch2; cin>>ch1>>ch2; switch(ch1) { case 'A': cout << "This A is part of outer switch\n"; switch(ch2) { case 'A': cout << "This A is part of inner switch\n"; break; case 'B': cout << "This B is part of inner switch\n"; } break; case 'B': cout << "This B is part of outer switch\n"; break; }

  4. Top down design

  5. Benefits of Top Down Design • Subtasks, or functions in C++, make programs • Easier to understand • Easier to change • Easier to write • Easier to test • Easier to debug • Easier for teams to develop

  6. Hardware design • Subparts or modules • Interface • Software design

  7. Three places where variables are declared: • Inside functions: local variables • In the definition of function parameters: formal parameters • Outside of all functions: global variables

  8. local variables are created when function is called and destroyed when function is exited. formal parameters are used like local variables. Their value is lost once the function terminates. They have a special task of receiving the value of arguments. global variables hold their value throughout the lifetime of your program

  9. int sum(int x, int y); int main(){ int answer; cout<<“In main”; answer = sum(2,3); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; }

  10. Pass by Value Function Call int sum(int x, int y); int main(){ int answer, num1, num2; cin>>num1>>num2; cout<<“In main”; answer = sum(num1,num2); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; }

  11. Examples of Functions #include <iostream.h> double pi(void); int main(void){ double answer; answer=pi(); cout<<"answer is "<<answer<<endl; return 0; } double pi(void){ double value=3.14159; cout<<"I return value of pi when called"<<endl; return value; }

  12. Examples of Functions I return value of pi when called answer is 3.14159

  13. #include <iostream.h> double area(double radius); double pi(); int main(void){ double answer; answer=area(7.5); cout<<"answer is "<<answer<<endl; return 0; } double area(double radius){ double value=pi()*pi()*radius; return value; } double pi(){ return 3.14159; //we can return a constant also }

  14. Output? answer is 74.0219

  15. #include <iostream.h> double Add2(double d, char c, int i); int main(void){ double dd=4.4; char cc='b'; int ii=15; double ans=Add2(dd,cc,ii); cout<<ans<<endl; cout<<dd<<"\t"<<ii<<"\t"<<cc<<endl; return 0; } double Add2(double d, char c, int i){ d=d+2; i=i+2; c=c+2; cout<<d<<"\t"<<i<<"\t"<<c<<endl; return d; }

  16. Output? 6.4 17 d 6.4 4.4 15 b • Parameter list order is important!

  17. int main() { int x=30, y=40, z=45, ans1, ans2; x += (++y) * (x/y) + 35*z; x *= (z*98-y)*78 + 53/z; x /= (34*67*y*(z%x)); ans1 = 3*x+50+y; x=70, y=80, z=85; x += (++y) * (x/y) + 35*z; x *= (z*98-y)*78 + 53/z; x /= (34*67*y*(z%x)); ans2 = 3*x+50+y; cout<<ans1<<endl<<ans2; }

  18. int calculate(int a, int b, int c); int main(){ int x=30, y=40, z=45, ans1, ans2; ans1 = calculate(x, y, z); x=70, y=80, z=85; ans2 = calculate(x, y, z); cout<<ans1<<endl<<ans2; } int calculate(int a, int b, int c){ int value; a += (++b) * (a/b) + 35*c; a *= (c*98-b)*78 + 53/c; a /= (34*67*b*(c%a)); value = 3*a+50+b; return value; }

  19. header files • predefined functions #include <math.h>

  20. Math library #include <math.h> sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), sinh(x), cosh(x), tanh(x), exp(x), log(x), log10(x), pow(x,y), sqrt(x), ceil(x), floor(x)

  21. int main() { int num; double sine_val; for(num=1; num < 100; num++) { sine_val = sin(num*3.14/180.0); cout << num <<" "<<sine_val<< "\n"; } return 0; }

  22. #include <stdlib.h> int rand(void)

  23. Pass by Value Function Call int sum(int x, int y); int main(){ int answer, x, y; cin>>x>>y; cout<<“In main”; answer = sum(x,y); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; }

More Related