Download presentation
Presentation is loading. Please wait.
1
Tutorial #6 Summer 2005
2
functions ( parameters list ) { body }
3
Max function int max(int a, int b) { if (a > b) return a; return b; }
4
Max function #include int main() { int x, y; scanf("%d%d", &x, &y); printf("max = %d\n", max(x, y)); return 0; }
5
Max function int max(int a, int b) { return (a > b ? a : b); }
6
Max function void max(int a, int b) { if (a > b) { printf("%d > %d\n", a, b); return ; } if (b > a) { printf("%d > %d\n", b, a); return ; } printf("%d = %d\n", a, b); }
7
Max function void max(int a, int b) { if (a > b) printf("%d > %d\n", a, b); else if (b > a) printf("%d > %d\n", b, a); else printf("%d = %d\n", a, b); }
8
absolute int absolute(int x) { return (x >= 0 ? x : -x); }
9
absolute int absolute(int x) { return (x >= 0 ? x : -x); }
10
Triangle area double triangle_area(double width, double height) { double area = width * height / 2; return area; }
11
Triangle area double triangle_area(double width, double height) { return (width * height / 2); }
12
messages void messages(int i) { switch(i) { case 0 : printf("first message\n"); break; case 1 : printf("second message\n"); break; case 2 : printf("third message\n"); }
13
power double power(double x, int y) /* y >= 0 */ { double res; for (res = 1; y > 0; y--) res *= x; return res; }
14
power double power(double x, int y) /* y >= 0 */ { double res = 1; for(; y > 0; y--) res *= x; return res; }
15
What happens here? void f(int x) { x++; } int main() { int x = 5; f(); printf(“x = %d\n”, x); return 0; }
16
Arrays - Counting int count(int arr[], int n, int val) { int i, cnt = 0; for (i = 0; i < n; i++) if (arr[i] == val) ++cnt; return cnt; }
17
Arrays - Assignments void f(int arr[], int n) { for(i = 0; i < n; i++) arr[i] = val; }
18
Programs #include int mult(int, int); int power(int, int); void power_table(int, int); int main() { int base, power; printf("Enter base and power of the power table :"); if (scanf("%d%d", &base, &power) < 2) { printf("Input Error"); return 1; } power_table(base, power); return 0; }
19
mult int mult(int x, int y) /* returns x*y */ { int res = 0; for (; y > 0; y--) res += x; return res; }
20
Power table void power_table(int n, int k) { int i, j ; for (i = 1; I <= k; i++) { for (j = 1; j <= n; j++) printf("%-5d",power(j, i)); putchar('\n'); } return ; }
21
Power int power(int x, int y) /* returns x to the power of y */ { int res = 1; for (; y > 0; y--) res = mult(res, x); return res; }
22
Variables Scope Static Global
23
scope #include int main() { int x = 10, y = 15; { int x = 20; printf( x %d, y = %d\n , x++, y++); } printf( x %d, y = %d\n , x, y); return 0; }
24
scope #include int main( void ) { int count = 1; for ( ; count <= 5; count++) { int count = 1; printf(“%d\n”, count); } return 0 ; }
25
scope #include int main( void ) { int count = 1; while (count <= 5) { int count = 1; printf(“%d\n”, count); count++; } return 0 ; }
26
scope int main() { int a = 1, b = 2, c = 3; printf( %3d %3d %3d\n , a, b, c); { int b = 4; double c = 5.0; printf( %d %d %f\n , a, b, c); a b; { int c; c b; printf( %d %d %d\n , a, b, c); } printf( %d %d %f\n , a, b, c); } printf( %d %d %d\n , a, b, c); return 0; }
27
static #include void f(void) { static int cnt = 1; printf( %d , cnt++); } int main() { int i; for(i = 0; i < 5; ++i) f(); for(i = 0; i < 5; ++i) f(); return 0; }
28
globals #include int x = 20, y; void f1(int x) { printf( x = %d y = %d\n , x, y); x = y = 10; } void f2(int y) { printf( x = %d y = %d\n , x, y); x = y = 15; } int main() { y = 5; f1(20); f2(y); printf( x = %d y = %d\n , x, y); return 0; }
29
fun #include int x = 5, y = 6, z = 7; void printvals(int a, int b, int c, int x, int y,int z) { printf( a = %d b = %d c = %d x = %d” “ y = %d z = %d , a, b, c, x, y, z); } void oof(int a, int b, int c) { int x; static int z; printvals(a, b, c, x, y, z); x = a; z = x; y++; { int a; a = x; b = y; c = z; printvals(a, b, c, x, y, z); } printvals(a, b, c, x, y, z); } int main() { int a = 0; x = 1; y = 2; z = 3; oof(7, 8, 9); x++; y++; z++; oof(x, y, z); return 0 ; }
30
arrays #include int main( void ) { int a[3];... return 0 ; } a[0] a[1]a[2]
31
arrays #include int main( void ) { int a[3][5];... return 0 ; } [rows][columns] ; 0 1 2 01234 a[0][0] a[1][3]
32
arrays #include int main( void ) { int a[3][5];... return 0 ; } 0 1 2 01234 0 12
33
arrays #include int main( void ) { int a[3][5], i, j; for (i = 0; i < 3; i++) for (j = 0; j < 5; j++) scanf( %d”, &a[i][j]); return 0 ; } 0 12 0 1 2 01234
34
arrays #include int main( void ) { int a[3][5], i, j; for (j = 0; j < 5; j++) for (i = 0; i < 3; i++) scanf( %d”, &a[i][j]); return 0 ; } 0 12 0 1 2 01234
35
arrays #include int main( void ) { int a[3][5], j, k, sum = 0 ; for (j = 0; j < 3; j++) for (k = 0; k < 5; k++) scanf( %d”, &a[j][k]); for (j = 0; j < 3; j++) for (k = 0; k < 5; k++) sum += a[j][k]; printf( The sum is : %d\n , sum) ; return 0 ; }
36
arrays #include #define N 3 int main( void ) { int matrix[N][N], j, k, sum = 0; for (i = 0; i < N; i++) for (j = 0; j < N; j++) matrix[i][j] = i + j; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) printf( %d “, matrix[i][j]); printf( \n ) ; } return 0; }
37
typedef typedef double scalar ; typedef int length; int main( void ) { double a, b; scalar c, d; int i, j; length k, l; }
38
typedef #include #defineN10 #define M20 typedef double scalar ; typedef scalar vector[M] ; typedef scalar matrix[N][M] ; int main( void ) { vector a, b; /* a & b are both arrays */ matrix mat; /* mat is a two-dimensions array N M */ } typedef scalar matrix[N][M] ; typedef vector matrix[N] ;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.