Thursday, December 28, 2006 One of the main causes of the fall of the Roman Empire was that, lacking...
-
date post
19-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Thursday, December 28, 2006 One of the main causes of the fall of the Roman Empire was that, lacking...
Thursday, December 28, 2006
One of the main causes of the fall of the Roman Empire was that,
lacking zero, they had no way to indicate successful termination of
their C programs.
- Robert Firth
double area, circumference, volume, radius=7.5;//circlearea = 3.1415 * radius * radius;circumference = 2 * 3.1415 * radius;cout<<area<<" "<<circumference<<endl;//spherearea = 4.0 * 3.1415 * radius * radius;volume = 4.0/3.0 * 3.1415 * radius * radius * radius;cout<<area<<" "<<volume<<endl;
double area, circumference, volume, radius=7.5;//circlearea = 3.1415 * radius * radius;circumference = 2 * 3.1415 * radius;cout<<area<<" "<<circumference<<endl;//spherearea = 4.0 * 3.1415 * radius * radius;volume = 4.0/3.0 * 3.1415 * radius * radius * radius;cout<<area<<" "<<volume<<endl;
Avoid magic numbers
double PI = 3.14159;double area, circumference, volume, radius=7.5;//circlearea = PI * radius * radius;circumference = 2 * PI * radius;cout<<area<<" "<<circumference<<endl;//spherearea = 4.0 * PI * radius * radius;volume = 4.0/3.0 * PI * radius * radius * radius;cout<<area<<" "<<volume<<endl;
//Better but still not good enoughdouble PI = 3.14159;double area, circumference, volume, radius=7.5;//circlearea = PI * radius * radius;circumference = 2 * PI * radius;cout<<area<<" "<<circumference<<endl;//spherearea = 4.0 * PI * radius * radius;volume = 4.0/3.0 * PI * radius * radius * radius;cout<<area<<" "<<volume<<endl;
const double PI = 3.14159;double area, circumference, volume, radius=7.5;//circlearea = PI * radius * radius;circumference = 2 * PI * radius;cout<<area<<" "<<circumference<<endl;//spherearea = 4.0 * PI * radius * radius;volume = 4.0/3.0 * PI * radius * radius * radius;cout<<area<<" "<<volume<<endl;
The Black Box Analogy
A black box refers to something that we know how to use, but the method of operation is unknown
A person using a program does not need to know how it is coded
A person using a program needs to know what the program does, not how it does it
Functions and the Black Box Analogy A programmer who uses a function
needs to know what the function does, not how it does it
A programmer needs to know what will be produced if the proper arguments are put into the box
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;
}
int sum(int x, int y);int main(){ int answer;cout<<“In main”;answer = sum(sum(5,6),3);cout<<answer;
return 0;}int sum (int x, int y){int z=x+y;cout<<“In sum”;return z;
}
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 <ctype.h>int isalpha(char c);int isdigit(char c);int isalnum(char c);int ispunct(char c);int isspace(char c);int islower(char c);int isupper(char c);int tolower(char c);int toupper(char c);
Example of local variablesint example(int x){ x=54; return x;}
int main(){int x=23;cout<<x<<"\n"; example(x);cout<<x<<"\n";
x=example(x);cout<<x<<"\n"; return 0;
}
int example(int x){ x=54; return x;}
int main(){int x=23;cout<<x<<"\n"; //prints 23example(x);cout<<x<<"\n"; //prints 23x=example(x);cout<<x<<"\n"; //prints 54return 0;
} return value example
Variables declared outside of all functions: global variables
Global variables hold their value throughout the lifetime of your program
Example of global variables
int global=100;
void example(int x){ global=10*x;}
int main(){cout<<global<<"\n"; global = 200;cout<<global<<"\n"; example(3);cout<<global<<"\n"; example(global); cout<<global<<"\n"; return 0;
}
Example of global variables
int global=100;
void example(int x){ global=10*x;}
int main(){cout<<global<<"\n"; //prints 100global = 200;cout<<global<<"\n"; //prints 200example(3);cout<<global<<"\n"; //prints 30example(global); cout<<global<<"\n"; //prints 300
return 0;}
local variables - life of functionglobal variables - existence of entire
execution of programGlobal variables are initialized at start of
program. They are given a value of zero if no other initialization value is specified.
Un-initialized local variables will have unknown values
Local variables are associated with the block they are declared in.
int a=3;
if(a<=3){
int x=4;
cout<<x;
}
Local variables are associated with the block they are declared in.
int a=3;
if(a<=3){
int x=4;
cout<<x;
}
cout<<x; //Error
int a=10; //global variable
int main(){
int a=20;
cout<<a<<endl; //prints 20
cout<<::a<<endl; //prints 10
return 0;
}
int a=10;
int doSomething(){ int a=30; cout<<a<<endl; return a;}
int main(){int a=20;cout<<a<<endl; cout<<::a<<endl; cout<<doSomething()<<endl; return 0;
}
int a=10;
int doSomething(){ int a=30; cout<<a<<endl; //prints 30 return a;}
int main(){int a=20;cout<<a<<endl; //prints 20cout<<::a<<endl; //prints 10cout<<doSomething()<<endl; //prints 30return 0;
}
What will happen here?
void example(int f){int f; //Error:redefinition of formal parameter ff=3;cout<<f;
}