tutorial #6 summer 2005. functions ( parameters list ) { body }

38
Tutorial #6 Summer 2005

Post on 19-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Tutorial #6

Summer 2005

functions

<type> <name>( parameters list )

{

body

}

Max function

int max(int a, int b)

{

if (a > b)

return a;

return b;

}

Max function

#include <stdio.h>

int main()

{

int x, y;

scanf("%d%d", &x, &y);

printf("max = %d\n", max(x, y));

return 0;

}

Max function

int max(int a, int b)

{

return (a > b ? a : b);

}

Max functionvoid 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);

}

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);

}

absolute

int absolute(int x)

{

return (x >= 0 ? x : -x);

}

absolute

int absolute(int x)

{

return (x >= 0 ? x : -x);

}

Triangle area

double triangle_area(double width, double height)

{

double area = width * height / 2;

return area;

}

Triangle area

double triangle_area(double width, double height)

{

return (width * height / 2);

}

messagesvoid 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"); }}

power

double power(double x, int y) /* y >= 0 */

{

double res;

for (res = 1; y > 0; y--)

res *= x;

return res;

}

power

double power(double x, int y) /* y >= 0 */

{

double res = 1;

for(; y > 0; y--)

res *= x;

return res;

}

What happens here?void f(int x) { x++;}int main(){

int x = 5; f(); printf(“x = %d\n”, x); return 0;

}

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;}

Arrays - Assignments

void f(int arr[], int n)

{

for(i = 0; i < n; i++)

arr[i] = val;

}

Programs#include <stdio.h>#include <stdlib.h>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;}

multint mult(int x, int y) /* returns x*y */{ int res = 0; for (; y > 0; y--) res += x; return res;}

Power tablevoid 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 ;}

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;}

Variables

Scope Static Global

scope#include <stdio.h>

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;

}

scope#include <stdio.h>

int main( void )

{

int count = 1;

for ( ; count <= 5; count++)

{

int count = 1;

printf(“%d\n”, count);

}

return 0 ;

}

scope#include <stdio.h>

int main( void )

{

int count = 1;

while (count <= 5)

{

int count = 1;

printf(“%d\n”, count);

count++;

}

return 0 ;

}

scopeint 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;

}

static#include <stdio.h>

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;

}

globals#include <stdio.h>

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;

}

fun#include <stdio.h>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 ;

}

arrays

#include <stdio.h>

int main( void )

{

int a[3];

. . .

return 0 ;

}

a[0] a[1] a[2]

arrays

#include <stdio.h>

int main( void )

{

int a[3][5];

. . .

return 0 ;

}

<type> <name>[rows][columns] ;

0

1

2

0 1 2 3 4

a[0][0]

a[1][3]

arrays

#include <stdio.h>

int main( void )

{

int a[3][5];

. . .

return 0 ;

}

0

1

2

0 1 2 3 4

0 1 2

arrays#include <stdio.h>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 1 2

0

1

2

0 1 2 3 4

arrays#include <stdio.h>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 1 2

0

1

2

0 1 2 3 4

arrays#include <stdio.h>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 ;}

arrays#include <stdio.h>#define N 3int 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;}

typedef

typedef double scalar ;typedef int length;

int main( void ){

double a, b;scalar c, d;int i, j;length k, l;

}

typedef#include <stdio.h>#defineN 10#define M 20typedef 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 NM */}

typedef scalar matrix[N][M] ;typedef vector matrix[N] ;