-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_13.c
119 lines (99 loc) · 2.9 KB
/
ex10_13.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* pr_pset10_13:
*
* Write a program that prompts the user to enter three sets of five double
* numbers each. (You may assume the user responds correctly and doesn’t enter
* non-numeric data.) The program should accomplish all of the following:
* a. Store the information in a 3×5 array.
* b. Compute the average of each set of five values.
* c. Compute the average of all the values.
* d. Determine the largest value of the 15 values.
* e. Report the results.
* Each major task should be handled by a separate function using
* the traditional C approach to handling arrays.
*/
#include <stdio.h>
#define SETS 3
#define SET_SIZE 5
void store_data(double ar[], int);
double set_aver(double ar[], int);
double tot_aver(double ar[][SET_SIZE], int);
double largest(double ar[][SET_SIZE], int);
void display(double ar[][SET_SIZE], int);
int main(void)
{
double ar[SETS][SET_SIZE];
printf("Enter three sets of five numbers each.\n");
for (int i = 0; i < SETS; i++)
{
printf("Enter %d values for set %d: ", SET_SIZE, i + 1);
store_data(ar[i], SET_SIZE);
}
printf("\nYour array is:\n");
display(ar, SETS);
for (int i = 0; i < SETS; i++)
printf("Average of %d set values: %.2lf\n",
i + 1, set_aver(ar[i], SET_SIZE));
printf("Average of all values is %.2lf\n",
tot_aver(ar,SETS));
printf("The largest value is %.2lf\n",
largest(ar,SETS));
return 0;
}
// Store the information in a one-dimensional array.
void store_data(double ar[], int size)
{
char c;
for (int i = 0; i < size; i++)
{
while (scanf("%lf", &ar[i]) != 1)
{
while ((c = getchar()) != '\n')
putchar(c);
printf(" is not a number.\nRetry: ");
}
}
}
// Compute the average of set values.
double set_aver(double ar[], int size)
{
double average;
double total = 0.0L;
for (int i = 0; i < size; i++)
total += ar[i];
average = total / size;
return average;
}
// Compute the average of all the values.
double tot_aver(double ar[][SET_SIZE], int rows)
{
double average;
double total = 0.0L;
for (int i = 0; i < rows; i++)
for (int j = 0; j < SET_SIZE; j++)
total += ar[i][j];
average = total / (double)(rows * SET_SIZE);
return average;
}
// Determine the largest value of all array values.
double largest(double ar[][SET_SIZE], int rows)
{
double big_num;
big_num = ar[0][0];
for (int i = 0; i < rows; i++)
for (int j = 0; j < SET_SIZE; j++)
if (big_num < ar[i][j])
big_num = ar[i][j];
return big_num;
}
// Show two-dimensional array.
void display(double ar[][SET_SIZE], int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < SET_SIZE; j++)
printf("%5g ", ar[i][j]);
printf("\n");
}
printf("\n");
}