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

For this C program, you will need to define a number of Sensors (N) associated w

ID: 3765933 • Letter: F

Question

For this C program, you will need to define a number of Sensors (N) associated with text files. These files will be labeled sensor_0.txt, sensor_l.txt, and sensor_2.txt, sensor_3.txt (as defined by the number of sensor inputs). With four text files in total, the user will generally be able to enter anywhere from 2-4 number of sensors. With the text files associated, the format within will be: Note that these files will already be pre-defined, so data needs to only be pulled from these, not written. For formatting, either arrays or struct's can be utilized to pull the associated data. Finally, with these given values, a table should be displayed as such: The power consumption was minimum at time 55(18.5) reported by sensor 0, and maximum at time 67(29.4) reported by sensor 2. Le. calculating the average, min, max, and total values within the sensors. Please use C as the programming language for this.

Explanation / Answer

#include <stdio.h>
struct sensor
{
int tim;
float val;
};
int main()
{
FILE *fp;
struct sensor sens[4][100]={{}};
struct sensor min={0,100},max={0,0};
struct sensor gmin={0,100},gmax={0,0};
int count[4]={0};
int min_id,max_id;
char temp[5];
int i,j,n;
float sum,min_sum=0,max_sum=0,avg_sum=0,total_sum=0;
char file_name[20] = "sensor_1.txt";
printf("enter the number of sensors ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
file_name[7]=i+1+'0';
fp = fopen(file_name,"r");
j=0;
  
while(feof(fp)==0)
{
fscanf(fp,"%d %f %s",&sens[i][j].tim,&sens[i][j].val,&temp);
j++;
}
count[i]=j;
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<count[i];j++)
{
sum+=sens[i][j].val;
if(min.val>sens[i][j].val)
min = sens[i][j];
if(max.val < sens[i][j].val)
max = sens[i][j];
}
printf("%d %f %f %f %f ",i+1,sum/count[i],min.val,max.val,sum);
min_sum+=min.val;
avg_sum+=sum/count[i];
max_sum+=max.val;
total_sum +=sum;
if(gmin.val>min.val)
{
gmin = min;
min_id = i;
}
if(gmax.val < max.val)
{
gmax = max;
max_id = i;
}
}
printf("avg %f %f %f %f ",avg_sum/n,min_sum/n,max_sum/n,total_sum/n);
printf("****************************************************************** ");
printf("The power consumption was minimum at time %d (%f) reported by sensor %d ",gmin.tim,gmin.val,min_id);
printf("and the maximun at time %d (%f) reported by sensor %d ",gmax.tim,gmax.val,max_id);
}