CS 2073 Computer Programming wEng Applications Ch 4

  • Slides: 71
Download presentation
CS 2073 Computer Programming w/Eng. Applications Ch 4 Modular Programming With Functions Turgay Korkmaz

CS 2073 Computer Programming w/Eng. Applications Ch 4 Modular Programming With Functions Turgay Korkmaz Office: SB 4. 01. 13 Phone: (210) 458 -7346 Fax: (210) 458 -4437 e-mail: korkmaz@cs. utsa. edu web: www. cs. utsa. edu/~korkmaz 1

Name Addr Lecture++; Lecture Content 16 2

Name Addr Lecture++; Lecture Content 16 2

4. 1 Modularity n n How do you solve a big/complex problem? Divide it

4. 1 Modularity n n How do you solve a big/complex problem? Divide it into small tasks and solve each task. Then combine these solutions. Divide and Conquer 3

4. 1 Modularity (cont’d) n In C we use Structure Chart functions also referred

4. 1 Modularity (cont’d) n In C we use Structure Chart functions also referred to as modules to perform specific tasks that Shows how the program we determined in separated into tasks and which our solution tasks reference other tasks. NOTE: It does NOT indicate the sequence of steps in the program! 4

Advantages of using modules n n n Modules can be written and tested separately

Advantages of using modules n n n Modules can be written and tested separately Modules can be reused Large projects can be developed in parallel Reduces length of program, making it more readable Promotes the concept of abstraction n A module hides details of a task We just need to know what this module does We don’t need to know how it does it 5

4. 2 Programmer Defined Functions n n n Every C program starts with main()function

4. 2 Programmer Defined Functions n n n Every C program starts with main()function Additional functions are called or invoked when the program encounters function names Functions could be n n n Pre-defined library functions (e. g. , printf, sin, tan) or Programmer-defined functions (e. g. , my_printf, area) Functions n n Perform a specific task May take arguments May return a single value to the calling function May change the value of the function arguments (call by reference) 6

Function definition return_type function_name (parameters) { declarations; statements; int my_add_func(int a, int b) }

Function definition return_type function_name (parameters) { declarations; statements; int my_add_func(int a, int b) } { int sum; sum = a + b; return sum; } 7

Programmer-Defined Functions Terminology n Function Prototype describes how a function is called int my_add_func(int

Programmer-Defined Functions Terminology n Function Prototype describes how a function is called int my_add_func(int a, int b); n Function Call result = my_add_func(5, X); n Function implementation int my_add_func(int a, int b) { … } n. Function parameters Formal parameters n. Actual parameter n. Formal parameters must match with actual parameters in order, number and data type. n. If the type is not the same, type conversion will be applied (coercion of arguments). But this might cause some errors (double int) so you need to be careful! n 8

Example: Pre-defined Functions So far, we used several pre-defined functions! #include <stdio. h> double

Example: Pre-defined Functions So far, we used several pre-defined functions! #include <stdio. h> double sin(double radian); #include <math. h> double sin(double radian) int main(void) { { /* details of computing sin */ } double angle; printf(“Input angle in radians: n“); scanf(“%lf”, &angle); printf(“The sine of the angle is %fn“, sin(angle) ); return 0; } 9

Example: Programmer-defined Functions #include <stdio. h> int main(void) { double x 1, y 1,

Example: Programmer-defined Functions #include <stdio. h> int main(void) { double x 1, y 1, x 2, y 2, dist; printf(“Enter x 1 y 1 x 2 y 2 : ”); scanf(“%lf %lf %lf”, &x 1, &y 1, &x 2, &y 2); dist = sqrt(pow((x 2 -x 1), 2) + pow((y 2 -y 1), 2)); printf(“Distance is %lfn”, dist); return 0; } #include <stdio. h> double distance(double x 1, y 1, x 2, y 2); int main(void) { double x 1, y 1, x 2, y 2, dist; printf(“Enter x 1 y 1 x 2 y 2 : ”); scanf(“%lf %lf %lf”, &x 1, &y 1, &x 2, &y 2); dist = distance(x 1, y 1, x 2, y 2); printf(“Distance is %lfn”, dist); return 0; } double distance(double x 1, y 1, x 2, y 2) { return sqrt(pow((x 2 -x 1), 2) + pow((y 2 -y 1), 2)); } 10

Exercise (6, 8) (-3, 5) (4, -1) n n Suppose you are given the

Exercise (6, 8) (-3, 5) (4, -1) n n Suppose you are given the coordinate points of a triangle as shown above, write a program that can find the length of each edge… User enters: (x 1, y 1), (x 2, y 2), and (x 3, y 3) 11

Value Returning Functions n n Function returns a single value to the calling program

Value Returning Functions n n Function returns a single value to the calling program Function definition declares the type of value to be returned A return expression; statement is required in the function definition The value returned by a function can be assigned to a variable, printed, or used in an expression 12

Void Functions n n n A void function may be called to n perform

Void Functions n n n A void function may be called to n perform a particular task (clear the screen) n modify data n perform input and output A void function does not return a value to the calling program A return; statement can be used to exit from function without returning any value 13

Exercise: void function n Write a program to generate the following output? * **

Exercise: void function n Write a program to generate the following output? * ** ***** for (i=1; i<=5; i++) { for (j=1; j<=i; j++) printf(“*”); printf(“n”); } #include <stdio. h> void print_i_star(int i); main() { int i; for (i=1; i<=5; i++) { print_i_star( i ); } } void print_i_star(int i) { int j; for (j=1; j<=i; j++) printf(“*”); printf(“n”); return; } 14

Example: value returning function n!=n*(n-1)*…*1, 0! = 1 by definition Return Type Declarations Statements

Example: value returning function n!=n*(n-1)*…*1, 0! = 1 by definition Return Type Declarations Statements Function name int fact(int n) { Parameter Declarations int factres = 1; while(n>1) { factres = factres*n; n--; } return(factres); } 15

Example – use fact() #include <stdio. h> int fact(int n); /* prototype */ int

Example – use fact() #include <stdio. h> int fact(int n); /* prototype */ int main(void) { int t= 5, s; s = fact(t) + fact(t+1); t=5 s=? Function call } printf(“result is %dn”, s); return 0; 16

Example – execution of factorial function (cont’d) fact( 5 ) int fact(int n) {

Example – execution of factorial function (cont’d) fact( 5 ) int fact(int n) { int factres = 1; } while(n>1) { factres = factres*n; n--; } return(factres); t=5 s=? n=5 factres = 1 17

Example – execution of factorial function (cont’d) int fact(int n) { int factres =

Example – execution of factorial function (cont’d) int fact(int n) { int factres = 1; } while(n>1) { factres = factres*n; n--; } return(factres); t=5 s=? n=5 4 3 2 1 factres = 1 5 20 60 120 18

Example – execution of factorial function (cont’d) #include <stdio. h> int fact(int n); /*

Example – execution of factorial function (cont’d) #include <stdio. h> int fact(int n); /* prototype */ int main(void) { int t= 5, s; s = 120 + fact(t+1); t=5 s=? Function call } printf(“result is %dn”, s); return 0; 19

Example – execution of factorial function (cont’d) fact( 6 ) t=5 int fact(int n)

Example – execution of factorial function (cont’d) fact( 6 ) t=5 int fact(int n) { int factres = 1; s=? t+1 } while(n>1) { factres = factres*n; n--; } return(factres); n=6 factres = 1 20

Example – execution of factorial function (cont’d) int fact(int n) { int factres =

Example – execution of factorial function (cont’d) int fact(int n) { int factres = 1; } while(n>1) { factres = factres*n; n--; } return(factres); t=5 s=? n=6 5 4 3 2 1 factres = 1 6 30 120 360 720 21

Example – execution of factorial function (cont’d) #include <stdio. h> int fact(int n); /*

Example – execution of factorial function (cont’d) #include <stdio. h> int fact(int n); /* prototype */ int main(void) { int t= 5, s; t=5 s = 840 s = 120 + 720; } printf(“result is %dn”, s); return 0; result is 840 22

Example – reuse of factorial function n Write a statement to compute Enter X,

Example – reuse of factorial function n Write a statement to compute Enter X, Z, K, D … y=(fact(X)+fact(Z)*5)/(fact(K)-fact(D)); 23

Example – reuse of factorial function in another function n Write a select function

Example – reuse of factorial function in another function n Write a select function that takes n and k and computes “n choose k” where int select(int n, int k) { return fact(n)/(fact(n-k)*fact(k)); } 24

Name Addr Lecture++; Lecture Content 17 25

Name Addr Lecture++; Lecture Content 17 25

Function Examples 26

Function Examples 26

Exercise n Write a function to compute maximum and minimum of two numbers int

Exercise n Write a function to compute maximum and minimum of two numbers int max(int a, int b) { int min(int a, int b) if (a > b) { return a; if (a < b) else return a; return b; else } return b; } 27

Exercise n n Are following calls to max function valid? What will be the

Exercise n n Are following calls to max function valid? What will be the result? int max(int a, int b); int min(int a, int b); int main() { int x = 2, y = 3, z = 7, temp; temp = max(x, y); temp = max(4, 6); temp = max(4, 4+3*2); temp = max(x, max(y, z)); } 28

Example for void function void print_date(int mo, int day, int year) { /*output formatted

Example for void function void print_date(int mo, int day, int year) { /*output formatted date */ printf(“%i/%i/%in”, mo, day, year ); return; } 29

Exercise n Write a function that takes score as parameter and computes and returns

Exercise n Write a function that takes score as parameter and computes and returns letter grade based on the scale below. 80 -100 60 -79 40 -59 0 -39 A B C D 30

Solution char get_letter_grade(int score) { char grade; if ((score >= 80) && (score <=100))

Solution char get_letter_grade(int score) { char grade; if ((score >= 80) && (score <=100)) grade = 'A'; else if ((score >= 60) && (score <= 79)) grade = 'B'; else if ((score >= 40) && (score <= 59)) grade = 'C'; else if ((score >= 0) && (score <= 39)) grade = 'D'; return grade; } 31

Exercise n Write a function to compute logba double log_any_base(double a, double b) {

Exercise n Write a function to compute logba double log_any_base(double a, double b) { return log(a)/log(b); } 32

Exercise: Trace functions n What is the output of the following program #include <stdio.

Exercise: Trace functions n What is the output of the following program #include <stdio. h> int function 1(int x) { x = 2; printf("Out 1 = %dn", x); return(x+1); } int main() { int x = 4, y; y = function 1(x); printf("Out 2 = %dn", x); printf("Out 3 = %dn", y); return 0; } Output Out 1 = 2 Out 2 = 4 Out 3 = 3 33

Exercise n What is the output of the following program #include <stdio. h> void

Exercise n What is the output of the following program #include <stdio. h> void function 2() { printf("In function 2n"); } void function 1() { function 2(); printf("In function 1n"); } void function 3() { printf("In function 3n"); function 2(); } int main() { function 1(); function 3(); return 0; } Output In In function 2 1 3 2 34

Parameter Passing n Call by value n n n formal parameter receives the value

Parameter Passing n Call by value n n n formal parameter receives the value of the actual parameter function can NOT change the value of the actual parameter (arrays are an exception) Call by reference n n actual parameters are pointers (ch 5 and 6) function can change the value of the actual parameter 35

Scope of a function or variable n Scope refers to the portion of the

Scope of a function or variable n Scope refers to the portion of the program in which n n It is valid to reference the function or variable The function or variable is visible or accessible #include <stdio. h> int fact(int n); /* prototype */ int main(void) { int t= 5, s; s = fact(t) + fact(t+1); printf(“result is %dn”, s); return 0; } int fact(int n) { int factres = 1; } while(n>1) { factres = factres*n; n--; } return(factres); t=5 s=? n=5 factres = 1 36

Scope of a function or variable n Same variable name can be used in

Scope of a function or variable n Same variable name can be used in different functions #include <stdio. h> int fact(int n); /* prototype */ int main(void) { int t= 5, s; s = fact(t) + fact(t+1); printf(“result is %dn”, s); return 0; } int fact(int t) { int s = 1; } while(t>1) { s = s*t; t--; } return(s); t=5 s=? t=5 s=1 37

Scope n Local scope n n a local variable is defined within a function

Scope n Local scope n n a local variable is defined within a function or a block and can be accessed only within the function or block that defines it Global scope n a global variable is defined outside the main function and can be accessed by any function within the program file. 38

Global vs Local Variable #include <stdio. h> int z = 2; void function 1()

Global vs Local Variable #include <stdio. h> int z = 2; void function 1() { int a = 4; printf("Z = %dn", z); z = z+a; } int main() { int a = 3; z = z + a; function 1(); printf("Z = %dn", z); z = z+a; return 0; } z=2 5 9 12 a=4 a=3 Output Z=5 Z=9 39

Storage Class - 4 types Storage class refers to the lifetime of a variable

Storage Class - 4 types Storage class refers to the lifetime of a variable n automatic - key word auto - default for local variables n n external - key word extern - used for global variables n n Memory is reserved for a global variable throughout the execution life of the program. static - key word static n n Memory set aside for local variables is not reserved when the block in which the local variable was defined is exited. Requests that memory for a local variable be reserved throughout the execution life of the program. The static storage class does not affect the scope of the variable. register - key word register n Requests that a variable should be placed in a high speed memory register. 40

Skip n Study section 4. 3 from the textbook 41

Skip n Study section 4. 3 from the textbook 41

Name Addr Lecture++; Lecture Content 18 42

Name Addr Lecture++; Lecture Content 18 42

4. 4 Random Numbers n What is a random number? n n n Tossing

4. 4 Random Numbers n What is a random number? n n n Tossing a coin (0, 1) Rolling a die (1, 2, … 6) Min, Max, Avg, possible outcomes are equally likely or not, Engineering problems require use of random numbers n How can you compute the area of an irregular shape? 43

Uniform Random numbers n n All outcomes are equally likely For example fair die,

Uniform Random numbers n n All outcomes are equally likely For example fair die, where each outcome has the same probability of 1/6, So we can generate uniform random numbers between 1 and 6 by rolling a die. What if we need random numbers in another range? For example, 1 and 100? 44

Uniform Random numbers (cont’d) n n n In Standard C library, we have a

Uniform Random numbers (cont’d) n n n In Standard C library, we have a function rand() to generate random numbers between 0 and RAND_MAX is a system dependent constant (e. g. , 32, 767) defined in stdlib. h What will be the output of the following printf(“%d %d %dn”, rand(), rand()); n What will be the output, if we re-run the same program? 45

Pseudo-random Numbers n n n Computers generate random numbers using a seed number and

Pseudo-random Numbers n n n Computers generate random numbers using a seed number and an algorithm. So, if you give the same seed, you will always get the same sequence of pseudo -random numbers In Standard C library, we have a function srand(int seed) to give a new seed number 46

Example: generate 10 RNs #include <stdio. h> #include <stdlib. h> int main(void) { /*

Example: generate 10 RNs #include <stdio. h> #include <stdlib. h> int main(void) { /* Declare variables. unsigned int seed; int k; */ /* Get seed value from the user. */ printf("Enter a positive integer seed value: n"); scanf("%u", &seed); srand(seed); /* Generate and print ten random numbers. printf("Random Numbers: n"); for (k=1; k<=10; k++) printf("%i ", rand()); printf("n"); /* Exit program. return 0; } */ */ 47

RNs in a specified range [a b] n n Generate a RN between 0

RNs in a specified range [a b] n n Generate a RN between 0 and 7 x = rand() % 8; Generate a RN between 10 and 17 x = 10 + rand() % 8; int rand_int(int a, int b) { return rand()%(b-a+1) + a; } 48

Floating-Point RNs in a specified range [a b] n n n x = rand()

Floating-Point RNs in a specified range [a b] n n n x = rand() / RAND_MAX will give a random number between 0. 0 and 1. 0 x = rand() / RAND_MAX *(b-a) will give a RN between 0. 0 and b-a The value is then shifted into range [a b] by adding a double rand_float(double a, double b) { return ((double)rand()/RAND_MAX)*(b-a)+a; 49 }

Example: Hi. Lo Game /* Write a program that allows a user to play

Example: Hi. Lo Game /* Write a program that allows a user to play Hi. Lo game. User wins if he/she can guess the number between 1 -100 within at most 6 iterations */ #include <stdio. h> #include <stdlib. h> int rand_int(int a, int b); /* prototype */ void play. Hi. Lo( int s); int main(void) { unsigned int seed; int secret; /* Declare variables */ printf("Enter a positive integer seed value: n"); scanf("%u", &seed); srand(seed); while(1){ secret = rand_int(1, 100); play. Hi. Lo(secret); } return 0; } 50

int rand_int(int a, int b) { return rand()%(b-a+1) + a; } void play. Hi.

int rand_int(int a, int b) { return rand()%(b-a+1) + a; } void play. Hi. Lo(int s) { int i, guess; for(i=1; i <=6; i++){ printf("Enter your guess : "); scanf("%d", &guess); if (guess > s) printf("It is Higher than secretn"); else if (guess < s) printf("It is Lower than secretn"); else { printf("Cong! you wonn"); return; } } printf("Sorry! Try againn"); return; } 51

Exercise: Another “guess the number game” n n Computer selects a random number s

Exercise: Another “guess the number game” n n Computer selects a random number s between [1000 9999] User tries to guess it by entering g Computer tells how many digits are in place, out of place, not in secret number For example, if s is 6234 n User enters g as 7436, then computer says n n 1 digit is in place 2 digits are out of place 1 digit is not in secret number User keeps trying until he finds the secret number 52

Random Number Summary #include <stdlib. h> srand(seed); rn = rand(); /* [0 RAND_MAX] (e.

Random Number Summary #include <stdlib. h> srand(seed); rn = rand(); /* [0 RAND_MAX] (e. g. , 32, 767) */ int rand_int(int a, int b) { return rand()%(b-a+1) + a; } double rand_float(double a, double b) { return ((double)rand()/RAND_MAX)*(b-a)+a; } 53

4. 5 Use of Floating-Point RNs: Instrumentation Reliability n n n Reliability: the portion

4. 5 Use of Floating-Point RNs: Instrumentation Reliability n n n Reliability: the portion of the time that the component works properly. For example, 0. 8 means 80% of the time the component is OK Given the reliability of each component, can you determine the reliability of the whole system? Analytical vs. Simulation 54

Analytical vs. Simulation n Suppose the reliability for each component in previous slide is

Analytical vs. Simulation n Suppose the reliability for each component in previous slide is the same and given by r We can then analytically compute the overall reliability for the systems in (a) and (b) as r 3 and 3 r-3 r 2+r 3, respectively. (how) OR, we can simulate the system using RNs n n n Simulate each component by generating random numbers between 0 and 1. If this number is less than r, then we consider the given component works properly. If all three in (a) or at least one in (b) works properly, then we consider the whole system works properly. We repeat the above experiment (say) 1000 times and find that 600 times the whole system worked properly. Then overall reliability is 600/1000=0. 6. Can you write a simulation program for any configuration where reliability of each component could be different 55

#include <stdio. h> #include <stdlib. h> #include <math. h> /* Determine simulation reliability estimates.

#include <stdio. h> #include <stdlib. h> #include <math. h> /* Determine simulation reliability estimates. */ for (k=1; k<=n; k++) { num 1 = rand_float(0, 1); num 2 = rand_float(0, 1); num 3 = rand_float(0, 1); double rand_float(double a, double b); int main(void) { unsigned int seed; int n, k; double component_reliability, a_series, a_parallel, series_success=0, parallel_success=0, num 1, num 2, num 3; /* Get information for the simulation. */ printf("Enter individual component reliability: "); scanf("%lf", &component_reliability); printf("Enter number of trials: n"); scanf("%i", &n); printf("Enter unsigned integer seed: n"); scanf("%u", &seed); srand(seed); printf("n"); /* Compute Analytical reliabilities. */ a_series = pow(component_reliability, 3); a_parallel = 3*component_reliability - 3*pow(component_reliability, 2) + pow(component_reliability, 3); if (((num 1<=component_reliability) && (num 2<=component_reliability)) && (num 3 <=component_reliability)) series_success++; if (((num 1<=component_reliability) || (num 2<=component_reliability)) || (num 3 <=component_reliability)) parallel_success++; } printf("Analytical Reliability n"); printf("Series: %. 3 f Parallel: %. 3 f n", a_series, a_parallel); printf("Simulation Reliability n"); printf(" Number of trials %i n", n); printf("Series: %. 3 f Parallel: %. 3 f n", (double)series_success/n, (double)parallel_success/n); return 0; } double rand_float(double a, double b) { return ((double)rand()/RAND_MAX)*(b-a) + a; } 56

Exercise n What will be the “if condition” in the simulation code of the

Exercise n What will be the “if condition” in the simulation code of the following system Comp 1, r 1 Comp 2, r 2 Comp 6, r 6 Comp 3, r 3 Comp 4, r 4 Comp 5, r 5 if ( ( (num 1<=r 1 && num 2<=r 2) || (num 4<=r 4 && (num 3<=r 3||num 5<=r 5)) ) && (num 6<=r 6) ) success++; 57

SKIP REST n Study 4. 6 and 4. 7 from the textbook 58

SKIP REST n Study 4. 6 and 4. 7 from the textbook 58

4. 8 Macros * n #define macro_name(parameters) macro_text replaces macro_name in the program n

4. 8 Macros * n #define macro_name(parameters) macro_text replaces macro_name in the program n Examples n n n #define area_tri(base, height) (0. 5*(base)*(height)) #define PI 3. 14 z=x * tri(3, 5) + y; z=x * (0. 5*(3)*(5)) + y; k=2*PI*r; k=2*3. 14*r; 59

4. 9 Recursive Functions* n A function that invokes itself is a recursive function.

4. 9 Recursive Functions* n A function that invokes itself is a recursive function. int fact(int k) { if (k == 0) return 1; else return k*fact(k-1); } k!=k*(k-1)! 60

#include <stdio. h> int fact(int k) { if (k == 0) return 1; else

#include <stdio. h> int fact(int k) { if (k == 0) return 1; else return k*fact(k-1); } int main() { int n; int nf; printf("Enter nn"); scanf("%d", &n); nf = fact(n); printf("Factorial = %dn", nf); system("pause"); return(0); } 61

Fibonacci Numbers n n Sequence {f 0, f 1, f 2, …}. First two

Fibonacci Numbers n n Sequence {f 0, f 1, f 2, …}. First two values (f 0, f 1) are 1, each succeeding number is the sum of previous two numbers. 1 1 2 3 5 8 13 21 34 F(0)=1, F(1) = 1 F(i) = F(i-1)+F(i-2) 62

Fibonacci Numbers int fibonacci(int k) { int term; term = 1; if (k>1) term

Fibonacci Numbers int fibonacci(int k) { int term; term = 1; if (k>1) term = fibonacci(k-1)+fibonacci(k-2); return term; } 63

#include <stdio. h> int fibonacci(int k) { int term = 1; if (k>1) term

#include <stdio. h> int fibonacci(int k) { int term = 1; if (k>1) term = fibonacci(k-1)+fibonacci(k-2); /* Iterative Version of Fibonacci Function */ int fibonacci(int k) { int a, b, c, i; if (k<=1) return 1; else { a = 1; b = 1; i = 2; while (i<=k) { c = a + b; a = b; b = c; i = i + 1; } return(c); } return(term); } int main() { int n; int nfib; printf("Enter nn"); scanf("%d", &n); nfib = fibonacci(n); printf("Fibonacci = %dn", nfib); system("pause"); return(0); } } 64

Extra examples 65

Extra examples 65

Exercise n n Given radius and height of a cylinder. Write a function to

Exercise n n Given radius and height of a cylinder. Write a function to compute the surface area. A = 2*pi*r*(r*h) #define PI 3. 14 double area(double radius, double height) { return 2*PI*radius*(radius+height); } 66

Exercise n n Given radius and height of a cylinder. Write a function to

Exercise n n Given radius and height of a cylinder. Write a function to compute the volume. V = pi*r 2*h #define PI 3. 14 double volume(double radius, double height) { return(PI*radius*height); } 67

Exercise n n Write a function to compute the median of 3 numbers x,

Exercise n n Write a function to compute the median of 3 numbers x, y and z. Possible order of numbers n x<y<z -> median y n x<z<y -> median z n y<x<z -> median x n y<z<x -> median z n z<x<y -> median x n z<y<x -> median y 68

Solution int median(int x, int y, int z) { if (((x<y) && (y<z)) ||

Solution int median(int x, int y, int z) { if (((x<y) && (y<z)) || ((z<y) && (y<x))) return y; else if (((y<x) && (x<z)) || ((z<x) && (x<y))) return x; else return z; } 69

Exercise n n n Assume you have maximum and minimum functions implemented. Use these

Exercise n n n Assume you have maximum and minimum functions implemented. Use these to find median of 3 numbers a < b < c -> median is b Consider 3 pairs (a, b), (b, c), (a, c) n n n min(a, b) = a min(b, c) = b min(a, c) = a Max(a, b, a) = b 70

Solution int median(int x, int y, int z) { return(max(min(x, y), min(x, z), min(y,

Solution int median(int x, int y, int z) { return(max(min(x, y), min(x, z), min(y, z))); } 71