I am trying to create a program that reads a data file and computes the water usage for each hour of the day, the average hourly usage for the day, and the time of the highest hourly usage.
I already created the main program, and two other functions, along with their headers. My last task is to create the function that processes the data and computes usage.
Here are my files:
/* File: water.h */
#define HOURS 24
/* File: water.c */
#include <stdio.h>
#include "water.h"
#include "meter.h"
#include "reading.h"
#define DEBUG
main()
{ int time[HOURS+1]; /* the hours array */
int readings[HOURS+1]; /* the meter readings data */
int usage[HOURS]; /* the useage for each hour interval */
int hours; /* the number of data readings */
int hi; /* index of the highest usage */
float avg; /* average usage for the day */
/* get the readings data */
/* gives the number of hours in data file */
hours = get_data(time, readings);
#ifdef DEBUG
printf(" debug: hours = %d\n", hours);
#endif
/* process the data */
avg = compute_usage(hours, readings, usage, &hi);
/* print the results */
print_results(hours,usage,time,hi,avg);
}
/* File: reading.h */
int get_data(int times[], int reads[]);
/* given an array for hours and readings, this function reads
the data from the stdin returning the number read */
void print_results(int num, int use[], int hrs[], int hi, float avg);
/* given number of intervals, usage and hours data, the hi index,
and average, this function prints the results. */
/* File: reading.c */
#include <stdio.h>
#include "water.h"
#include "reading.h"
int get_data(int times[], int reads[])
/* given an array for hours and readings, this function reads
the data from the stdin returning the number read */
{ int i = 0;
/* while there is more data */
while((i < (HOURS+1)) && (scanf("%d:00", times+i) != EOF))
{ /* read data */
scanf("%d", reads+i);
/* increment counter */
i++;
}
/* return count */
return i;
}
void print_results(int num, int use[], int hrs[], int hi, float avg)
/* given number of intervals, usage and hours data, the hi index,
and average, this function prints the results. */
{ int i;
/* print the usage data */
for(i = 0; i < num-1; i++)
printf("\t\t%2d:00\t%d\n",hrs[i], use[i]);
printf("\n\nthe average usage was %f\n", avg);
printf("the hi usage was %d at %d:00\n",use[hi], hrs[hi]);
}
The last task that I am now working on is this:
/* File: meter.h */
float compute_usage(int num, int *vals, int use[], int *hi_idx);
/* this function is given the count of meter readings available,
and the array of meter readings. It computes the water
usage for each hourly interval in the array use, and
returns (directly) the average usage for the day and
(indirectly) the index of the highest water usage value. */
So far, this is how my function looks like:
/* File: meter.c */
/* File containing function compute_usage() */
#include <stdio.h>
#include "meter.h"
#define DEBUG
float compute_usage(int num, int *vals, int use[], int *hi_idx)
/* this function is given the count of meter readings available,
* and the array of meter readings. It computes the water
* usage for each hourly interval in the array use, and
* returns (directly) the average usage for the day and
* (indirectly) the index of the highest water usage value. */
{
/* Declarations */
int i = 0; /* Counter initialized aat 0 */
int read;
for (i = 0; i < num; i++)
{
/* Each value of the readings list */
read = use[i];
#ifdef DEBUG
printf(" debug: read = %d\n", read);
#endif
}
}
This is my data file:
/* File: data1 */
0:00 19321
1:00 19324
2:00 19475
3:00 19479
4:00 19481
5:00 19503
6:00 19539
7:00 19547
8:00 19549
9:00 19552
10:00 19554
11:00 19554
12:00 19555
13:00 19557
14:00 19560
15:00 19561
16:00 19566
17:00 19572
18:00 19590
19:00 19595
20:00 19599
21:00 19630
22:00 19644
23:00 19646
24:00 19648
In trying to compute the water usage for each hour of the day, I would simply subtract a preceding reading from the one it is following. For example:
Give Input:
time meter reading
0:00 19321
1:00 19324
2:00 19475
3:00 19479
4:00 19481
Expected Output:
time water usage
0:00 3 b/c (19324 - 19321 = 3)
1:00 151 b/c (19475 - 19324 = 151)
2:00 4 b/c (19479 - 19475 = 4)
3:00 22 b/c (19481 - 19479 = 22)
and so forth....
So in my file meter.c (who's prototype I am not allowed to change), I am now trying to read in only the meter readings (indicated by *vals in the prototype). I just realized this is a pointer and I get a bit confused when it comes to using these. Would anyone know how I can go about reading each meter reading, and then subtract it from the next meter reading?