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?

Recommended Answers

All 5 Replies

To access the values of an array using a pointer is quite easy

int arr[]={1,2,3,4};
int* p= arr;
int i=0;

for(i=0;i<4;i++)
          printf("%d %d\n",*(p+i), p[i]);      // To ways to do the same thing

When p is actually a short form for *(p+i). Now you can use this example in your code

I just realized the meter readings need to be read by use[] instead. How can I read in this array, and then store the second value.
Like in the first line of data:
0:00 19321

How can I read that in and store just 19321?

You need to look at what is common for all the data lines. One thing they all have, is a space (or two or three, maybe a small tab), between the hours numbers, and the meter reading.

Another thing you might notice, is that the hours entry always uses two digits, after the colon. The next digit after those two, is always the first digit of the meter reading.

Another idea (not tested, but probably true), is that the meter readings are formatted like this, on the input line:

[H]H:MM   DDDDD\n

Where [H]H is an [optional] hour, followed by a mandatory hour, then a colon and two minute digits.

Then the spaces or tab. Then the meter digits, immediately followed by the newline char: '\n', which you can't see. So, in all liklihood, the meter reading are the digits immediately before the newline char,

Which gives us this:

spaces DDDDD \n, without any real gaps. Clearly, the meter readings take up the last 5 digits, if you worked backward from the end of the line, and stopped at the space.

So you have several ways to find your meter readings, and then it's just a matter of putting them into your array.

I would recommend a while loop (because you don't know how many rows of data there might be), and fgets() to put the data into a char str[50] = { '\0' };

From there, pick out any way you want to find the meter reading, using any of the above idea's - or work out one you do like. Don't be afraid to explore - that's a good way to learn valuable lessons, although it does take time.

Remember that fgets() returns NULL when it reaches the end of the file, NOT EOF. ( fscanf() returns EOF, but not fgets() ).

You need to look at what is common for all the data lines. One thing they all have, is a space (or two or three, maybe a small tab), between the hours numbers, and the meter reading.

Another thing you might notice, is that the hours entry always uses two digits, after the colon. The next digit after those two, is always the first digit of the meter reading.

Another idea (not tested, but probably true), is that the meter readings are formatted like this, on the input line:

[H]H:MM   DDDDD\n

Where [H]H is an [optional] hour, followed by a mandatory hour, then a colon and two minute digits.

Then the spaces or tab. Then the meter digits, immediately followed by the newline char: '\n', which you can't see. So, in all liklihood, the meter reading are the digits immediately before the newline char,

Which gives us this:

spaces DDDDD \n, without any real gaps. Clearly, the meter readings take up the last 5 digits, if you worked backward from the end of the line, and stopped at the space.

So you have several ways to find your meter readings, and then it's just a matter of putting them into your array.

I would recommend a while loop (because you don't know how many rows of data there might be), and fgets() to put the data into a char str[50] = { '\0' };

From there, pick out any way you want to find the meter reading, using any of the above idea's - or work out one you do like. Don't be afraid to explore - that's a good way to learn valuable lessons, although it does take time.

Remember that fgets() returns NULL when it reaches the end of the file, NOT EOF. ( fscanf() returns EOF, but not fgets() ).

What if it isn't a character string? Use[] is declared as an int. Should I just use scanf?

You could scanf() for a number, (getting the hours), a char (getting the colon), and another number, (getting the minutes), then one more number (getting the meter reading).

Hours and minutes with a two digit format might make your int's look different, but that can be handled in the printout.

I hesitate to recommend scanf(), but it can be used well, *if* the data is strictly formatted, and you know how scanf() works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.