-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_05.c
63 lines (54 loc) · 1.46 KB
/
ex10_05.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* pr_pset10_05:
*
* Write a function that returns the difference between the largest and
* smallest elements of an array-of-double.
* Test the function in a simple program.
*/
#include <stdio.h>
#define SIZE 10
double difference(double ar[], int size, int * imin, int * imax);
// Print sequence of the array element values.
void print_ar(double ar[], int size);
int main(void)
{
int imin = 0, imax = 0;
double diff;
double num[SIZE] = {5.6,45.17,3.09,9.4,13.09,88.36,83.1,56.8,101.01,7.0};
printf("> Test of a function that returns the difference between \n"
"> the largest and smallest elements of an array-of-double.\n\n");
print_ar(num, SIZE);
diff = difference(num,SIZE,&imin,&imax);
printf("Difference between [%d] and [%d] elements is %g\n",
imax, imin, diff);
return 0;
}
double difference(double ar[], int size, int * imin, int * imax)
{
double largest = ar[0], smallest = ar[0];
for (int i = 0; i < size; i++)
{
if (ar[i] > largest)
{
largest = ar[i];
*imax = i;
}
if (ar[i] < smallest)
{
smallest = ar[i];
*imin = i;
}
}
return largest - smallest;
}
void print_ar(double ar[], int size)
{
printf("Values of an array-of-double:\n");
for (int i = 0; i < size; i++)
{
printf(" %.2f", ar[i]);
if ((i + 1) != size)
printf(";");
}
printf("\n");
}