240 likes | 374 Views
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;
E N D
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; 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; }
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; }
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
Hardware design • Subparts or modules • Interface • Software design
Three places where variables are declared: • Inside functions: local variables • In the definition of function parameters: formal parameters • Outside of all functions: global variables
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
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; }
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; }
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; }
Examples of Functions I return value of pi when called answer is 3.14159
#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 }
Output? answer is 74.0219
#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; }
Output? 6.4 17 d 6.4 4.4 15 b • Parameter list order is important!
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; }
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; }
header files • predefined functions #include <math.h>
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)
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; }
#include <stdlib.h> int rand(void)
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; }