Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this C program, you are given an array of values for a number of sensors (N)

ID: 3765936 • Letter: F

Question

For this C program, you are given an array of values for a number of sensors (N). These values will be located within a text file. These values are structured as (for each sensor): will prompt the user to define a Start Time (ST) and an End Time (ET) to designate which values within the matrix array should be used. For example: (ST = 730, ET = 750), thus the range of Will be used. If a value of 734 is give as Start Time (ST), or End Time (ET), you must utilize linear interpolation (i.e. using the average value of time_stamp between 730 and 740) Finally, with these values, you must find the 1) average power consumption, 2) min and max power consumed, as well as 3) total power consumer for a given Sample Rate (SR). The sample rate will determine the number of "rectangles" used for integration methods (either use midpoint integration or trapezoidal). This is useful for finding the total power consumed. This should be written in C programming language. Please feel free to ask for clarification at any step.

Explanation / Answer

#include <stdio.h>
#include <string.h>

#define OK 0
#define NO_INPUT 1
#define TOO_LONG 2

int ReadFile(char fileLocation[20], int timestamps[300], float powerConsumed[300]);
static int getLine (char *prmpt, char *buff, size_t sz);
main()
{
int timestamps[300], numberOfSensors, st, et, flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0, count, rc, i, minPowerConsumedSensorId = 0, maxPowerConsumedSensorId = 0, minPowerConsumedTimeStamp = 0, maxPowerConsumedTimeStamp = 0;
float powerConsumed[300], sampleRate = 0, minPowerConsumed = 0, maxPowerConsumed = 0;
char choice, fileLocation[20];

do
{
printf(" a). - Enter the number of sensors ");
printf(" b). - Enter file location ");
printf(" c). - Time interval st and et ");
printf(" d). - Sample rate ");
printf(" e). - Get statistical valuea ");
printf(" f). - Exit ");
scanf("%c",&choice);
switch (choice)
{
case 'a':
       printf(" a). - Enter the number of sensors ");
       scanf("%d", &numberOfSensors);
       flag1 = 1;
break;
case 'b':
       printf(" b). - Enter file location "); // Enter name of text file for simplicity. Assumed that text file is in the same folder as source file
       rc = getLine ("Enter string> ", fileLocation, sizeof(fileLocation));
               if (rc == NO_INPUT) {
               // Extra NL since my system doesn't output that on EOF.
               printf (" No input ");
               return 1;
               }

               if (rc == TOO_LONG) {
               printf ("Input too long [%s] ", fileLocation);
               return 1;
               }
       flag2 = 1;
       count = ReadFile(fileLocation, timestamps, powerConsumed);
break;
case 'c':
       printf(" c). - Time interval st and et ");
       scanf("%d%d", &st, &et);
       flag3 = 1;
break;
case 'd':
       printf(" d). - Sample rate ");
       scanf("%f", &sampleRate);
       flag4 = 1;
break;
case 'e':
       if (flag1 == 0) {
           printf("Number of sensors has not been defined ");
           break;
       }
       if (flag2 == 0) {
           printf("File location has not been defined ");
           break;
       }
       if (flag3 == 0) {
           printf("Time interval has not been defined ");
           break;
       }
       if (flag4 == 0) {
           printf("Sample rate has not been defined ");
           break;
       }
       printf(" Sensor ID Average Min Max Total ");
       printf("/t--------- -------- ---- ---- ------ ");
       for   (i = 0; i < count; i++) {
           if (timestamps[i] <= et && timestamps[i] >= st) {
               printf(" %d %f ", &i, &powerConsumed[i]);
               if (powerConsumed[i] < minPowerConsumed) {
                   minPowerConsumed = powerConsumed[i];
                   minPowerConsumedSensorId = i;
                   minPowerConsumedTimeStamp = timestamps[i];
               }
               if (powerConsumed[i] > maxPowerConsumed) {
                   maxPowerConsumed = powerConsumed[i];
                   maxPowerConsumedSensorId = i;
                   maxPowerConsumedTimeStamp = timestamps[i];
               }
           }
           else continue;
       }
       printf(" ----------------------------------------------------------------- ");
       printf(" The power consumed was minimum at time %d reported by sensor %d, and maximum at time %d reported by sensor %d ", &minPowerConsumedTimeStamp, &minPowerConsumedSensorId, &maxPowerConsumedTimeStamp, &maxPowerConsumedSensorId );
       break;
}
}
while (choice != 'f');
}

int ReadFile(char fileLocation[20], int timestamps[300], float powerConsumed[300])
{
FILE *inFile;
int i, count=0;
char temp[2];
inFile=fopen(fileLocation,"r");
if (inFile == NULL)
{
printf("File cannot be opened.");
fclose(inFile);
return -1;
}
while (fscanf(inFile, "%d%f%s", timestamps[count], powerConsumed[count], temp)!= EOF)
{
   count++;
}
fclose(inFile);
return count;
}

static int getLine (char *prmpt, char *buff, size_t sz) {
int ch, extra;

// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf ("%s", prmpt);
fflush (stdout);
}
if (fgets (buff, sz, stdin) == NULL)
return NO_INPUT;

// If it was too long, there'll be no newline. In that case, we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff)-1] != ' ') {
extra = 0;
while (((ch = getchar()) != ' ') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}

// Otherwise remove newline and give string back to caller.
buff[strlen(buff)-1] = '';
return OK;
}