# Function (2)

## Exercise 6

Below is a source code to return a maximum among five numbers. Write the source code, save it as "func6.c", and confirm whether it can run properly.

```#include <stdio.h>

int max_return(int a, int b, int c, int d, int e)
{
int max;

max = a;

if(max < b) max = b;
if(max < c) max = c;
if(max < d) max = d;
if(max < e) max = e;

return (max);

}

int main(void)
{
int d0, d1, d2, d3, d4;
int max;

printf("Input 5 integers\n");
scanf("%d %d %d %d %d", &d0, &d1, &d2, &d3, &d4);

max = max_return(d0, d1, d2, d3, d4);

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

return (0);
}
```
```(Execution result)
\$
\$ ./a.out
Input 5 integers
2 8 3 6 5
Maximum = 8
\$
```

## Exercise 7

In similarity to func6.c, below is a source code to determine a maximum among five integers. Write the source code, complement the expression hidden by "******" and save it as "func7.c". Confirm whether it can run properly.

```#include <stdio.h>

void max_return(int a, int b, int c, int d, int e, int *max)
{

*max = a;

if(*max < b) *max = b;
if(*max < c) *max = c;
if(*max < d) *max = d;
if(*max < e) *max = e;

}

int main(void)
{
int d0, d1, d2, d3, d4;
int max;

printf("Input 5 integers\n");
scanf("%d %d %d %d %d", &d0, &d1, &d2, &d3, &d4);

**********************************************

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

return (0);

}
```

## Exercise 8

In similarity to func7.c, below is a source code to determine a maximum among five integers. In this programming, five integers are input into array. Write the source code, complement the expression hidden by "******" and save it as "func8.c". Confirm whether it can run properly.

```#include <stdio.h>

void max_return(int a, int b, int c, int d, int e, int *max)
{

*max = a;

if(*max < b) *max = b;
if(*max < c) *max = c;
if(*max < d) *max = d;
if(*max < e) *max = e;

}

int main(void)
{
int d;
int max, i;

printf("Input 5 integers\n");
for(i=0; i<5; i++)
scanf("%d", &d[i]);

**********************************************

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

return (0);
}
```

## Exercise 9

In similarity to func8.c, below is a source code to determine a maximum among five integers. In this programming, address of array into which five integers are input is used in a function, "max_return". Write the source code, complement the expression hidden by "******" and save it as "func9.c". Confirm whether it can run properly.

```#include <stdio.h>

void max_return(int a[], int *max)
{
int i;

*max = a;

for(i=1; i<5; i++)
if(*max < a[i]) *max = a[i];
}

int main(void)
{
int d;
int max, i;

printf("Input 5 integers\n");
for(i=0; i<5; i++)
scanf("%d", &d[i]);

**********************************************

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

return (0);
}
```

## Exercise 10

In similarity to func9.c, below is a source code to determine a maximum among five integers. In this programming, pointer is used in a function, "max_return". Write the source code, complement the expression hidden by "******" and save it as "func10.c". Confirm whether it can run properly.

```#include <stdio.h>

void max_return(int *a, int *max)
{
int i;

(1)**********************************************

for(i=1; i<5; i++){
if(*max < *a) *max = *a;
(2)   ******************************************
}
}

int main(void)
{
int d;
int max, i;

printf("Input 5 integers\n");
for(i=0; i<5; i++)
scanf("%d", &d[i]);

(3)**********************************************

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

return (0);
}
```

## Exercise 11

Using any source code among func6.c to func10.c, program to show maximum and minimum among five intergers, save it as "func11.c", confirm whether it can run properly.

```(Execution result)
\$
\$ ./a.out
Input 5 integers
2 8 3 6 5
Maximum = 8
Minimum = 2
\$
```

## Exercise 12

Based on Exercise 11, program to show maximum and minimum among arbitrary number of intergers, save it as "func12.c", confirm whether it can run properly.

```(Execution result)
\$
\$ ./a.out
Input integers repeatedly
Exit by Ctrl-d
8
9
-3
21
-31
-2
18  <- Ctrl-d
Maximum = 21
Minimum = -31
\$
```

Lessons for C programming
Junichi Susaki, Kyoto University