forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_group_averages.c
82 lines (73 loc) · 2.7 KB
/
find_group_averages.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
/*******************************************************************************
*
* Program: Find Average Of Groups Of Numbers In A File
*
* Description: Example of finding the average of groups of numbers in a file
* using C. This is a solution to the problem described here:
* http://cplusplus.com/forum/beginner/165952/
*
* YouTube Lesson: https://www.youtube.com/watch?v=kMAg6diYCI0
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
int main()
{
FILE *numbers_file;
// open the file for reading, the file is assumed to have the content:
//
// 5 96 87 78 93 21 4 92 82 85 87 6 72 69 85 75 81 73
//
// Where the numbers 5, 4 and 6 indicate the size of the next group of numbers
// and the subsequent 5, 4 and 6 numbers are part of each group of numbers.
//
numbers_file = fopen("numbers.dat", "r");
// if file did not open successfully, warn the user, and exit with an error
if (numbers_file == NULL)
{
printf("Error opening file.\n");
return 1;
}
double next_number = 0;
int remaining_group_numbers = 0;
int group_length;
double total = 0;
// keep reading in numbers until we reach the end of the file
while (!feof(numbers_file))
{
// remaining_group_numbers will keep track of how many numbers are
// remaining in a group of numbers, and when it reaches zero, we expect
// the next number in the file to be a number specifying how many numbers
// are in the next group of numbers
if (remaining_group_numbers == 0)
{
// read in the group length number, reset remaining_group_numbers
fscanf(numbers_file, "%d", &group_length);
remaining_group_numbers = group_length;
// output how many elements are in the next group
printf("The number of elements in this group is %d\n", group_length);
printf("The data in this group is: ");
}
// if more numbers are left to be read in the remaining group, read in
// that number
else
{
// read in the number and output it as part of the list of group data
fscanf(numbers_file, "%lf", &next_number);
printf("%.0lf ", next_number);
// add the number to the total so we can compute the average, decrement
// remaining_group_numbers as we have now processed another group number
total += next_number;
remaining_group_numbers--;
// if there are no more numbers remaining in the group, we can now compute
// the average, output it, and reset the total
if (remaining_group_numbers == 0)
{
printf("\nAverage: %.2f\n\n", total / group_length );
total = 0;
}
}
}
return 0;
}