Download presentation
Presentation is loading. Please wait.
1
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
2
§Tutorial §References on website.
3
bool p; // some statements here if (p) and if (true == p) if (!p) and if (false == p)
4
double area, circumference, volume, radius=7.5; //circle area = 3.1415 * radius * radius; circumference = 2 * 3.1415 * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * 3.1415 * radius * radius; volume = 4.0/3.0 * 3.1415 * radius * radius * radius; cout<<area<<" "<<volume<<endl;
5
double area, circumference, volume, radius=7.5; //circle area = 3.1415 * radius * radius; circumference = 2 * 3.1415 * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * 3.1415 * radius * radius; volume = 4.0/3.0 * 3.1415 * radius * radius * radius; cout<<area<<" "<<volume<<endl; Avoid magic numbers
6
double PI = 3.14159; double area, circumference, volume, radius=7.5; //circle area = PI * radius * radius; circumference = 2 * PI * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * PI * radius * radius; volume = 4.0/3.0 * PI * radius * radius * radius; cout<<area<<" "<<volume<<endl;
7
//Better but still not good enough double PI = 3.14159; double area, circumference, volume, radius=7.5; //circle area = PI * radius * radius; circumference = 2 * PI * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * PI * radius * radius; volume = 4.0/3.0 * PI * radius * radius * radius; cout<<area<<" "<<volume<<endl;
8
const double PI = 3.14159; double area, circumference, volume, radius=7.5; //circle area = PI * radius * radius; circumference = 2 * PI * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * PI * radius * radius; volume = 4.0/3.0 * PI * radius * radius * radius; cout<<area<<" "<<volume<<endl;
9
#define PI 3.14159 §At file scope: before main()
10
Functions l Call by Value
11
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
12
Functions and the Black Box Analogy l A programmer who uses a function needs to know what the function does, not how it does it l A programmer needs to know what will be produced if the proper arguments are put into the box
14
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; }
15
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; }
16
Larger of two numbers
17
header files predefined functions #include
18
Math library #include 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)
19
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; }
20
#include int rand(void)
21
#include 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);
22
Example of local variables int 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; }
23
int example(int x){ x=54; return x; } int main(){ int x=23; cout<<x<<"\n"; //prints 23 example(x); cout<<x<<"\n"; //prints 23 x=example(x); cout<<x<<"\n"; //prints 54 return 0; } return value example
24
Variables declared outside of all functions: global variables Global variables hold their value throughout the lifetime of your program
25
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; }
26
Example of global variables int global=100; void example(int x){ global=10*x; } int main(){ cout<<global<<"\n"; //prints 100 global = 200; cout<<global<<"\n"; //prints 200 example(3); cout<<global<<"\n"; //prints 30 example(global); cout<<global<<"\n"; //prints 300 return 0; }
27
§Use of global variables can have unexpected effects. §Software engineering principles
28
§local variables - life of function §global variables - existence of entire execution of program §Global 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
29
Local variables are associated with the block they are declared in. int a=3; if(a<=3){ int x=4; cout<<x; }
30
§Swap example
31
What is wrong here? int a=3; if(a<=3){ int x=4; cout<<x; } cout<<x;
32
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
33
int a=10; //global variable int main(){ int a=20; cout<<a<<endl; return 0; }
34
int a=10; //global variable int main(){ int a=20; cout<<a<<endl; //prints 20 return 0; }
35
We can use scope resolution operator :: to access global variables.
36
int a=10; //global variable int main(){ int a=20; cout<<a<<endl; cout<<::a<<endl; return 0; }
37
int a=10; //global variable int main(){ int a=20; cout<<a<<endl; //prints 20 cout<<::a<<endl; //prints 10 return 0; }
38
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; }
39
int a=10; int doSomething(){ int a=30; cout<<a<<endl; //prints 30 return a; } int main(){ int a=20; cout<<a<<endl; //prints 20 cout<<::a<<endl; //prints 10 cout<<doSomething()<<endl; //prints 30 return 0; }
40
What will happen here? void example(int f){ int f; f=3; cout<<f; }
41
What will happen here? void example(int f){ int f; //Error:redefinition of formal parameter f f=3; cout<<f; }
42
§List of marks §List of names §…
43
Arrays offer a convenient means of grouping together several related variables Arrays
44
Declaring an array §type array_name[size]; l allocates memory for size variables l index of first element is 0 l index of last element is size-1 l size must be a constant
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.