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

Let\'s say I have a machine that I would like to know, on average, how much it r

ID: 3545310 • Letter: L

Question

Let's say I have a machine that I would like to know, on average, how much it runs throughout a given day through a percentage value. Every 30 seconds, I will have a device to record the current temperature of the machine. If the machine increases its temperature by at least 0.5 degrees F in one of the 30 second windows, I will know that the machine is turned off. If the machine decreases its temperature by at least 0.5 degrees F within a 30 second window, I will know that the machine is turned on. If the machine does not change by 0.5 degrees within the next 30 second window, it will assume its same on or off status.


Now, I would like to also gather percentage data within each hour. This means that there are 120 30-second intervals within an hour, and there are 24 hours in a day. I have a file that I must read from containing 2,881 decimal values of type double for all of the hourly interval values. I first want to read in all 2,881 values, then create an array with 24 indices for each hour of the day. I want to find the percentage of time the machine is ON within each hour, and then store each hour percentage within its respective array index. This means that I will want to calculate the percentage of ON time for every 120 values in the file I am reading from.


I must gather each hourly percentage so that I can then output the values into a self-made bar graph (the bars consisting of asterisks). The vertical values (or y axis) will show percentage values from 5-100 in 5 percent intervals. The horizontal values (or x axis) will show which hour of the day it is, starting at midnight and ending at 11 pm in 1 hour intervals. For each hour's ON percentage, a loop should output an asterisk or a space moving row to row and starting from the y value of 100. This will create the appearance of a bar graph. Note that during the midnight hour, the machine is always OFF, and this means that the first column of the chart will contain no asterisks, so that each time the code loops through a row, the array index of zero (representing 12 midnight) will always print a space instead of an asterisk.


Overall, I must also calculate the total percentage of time the machine was turned on throughout the entire day. Basically, I need the correct percentage of time for each hour and the entire day. Each hour's number of asterisks based on its percentage must be correct!


Here is what I have so far:

#include<stdio.h>


int main()

{

    double previousValue,currentValue;

    FILE*ifp;

    ifp = fopen("value.txt", "r");

    int percentON[24];

    fscanf(ifp,"%lf",&previousValue);


    int i,j,on,currentState=0;


    //Calculation of PercentageON

    for(i=0;i<24;i++)

    {

        on=0;

        for(j=0;j<120;j++)

        {

            fscanf(ifp,"%lf",&currentValue);

            if(currentState == 0)

            {

                on = on+1;

                if(previousValue-currentValue>=0.5)

                    currentState=1;

            }

            else

            {

                if(currentValue-previousValue>=0.5)

                    currentState=0;

            }


            previousValue = currentValue;

            fscanf(ifp,"%lf",&currentValue);

        }

        percentON[i] = (on*100)/120;


    }


    double sum = 0;

    double total;

    for (i=0; i<24;i++){

            sum+= percentON[i];

    }

    total=sum/24;

    //Printing Bar Graph

    printf("The machine ran %.2lf percent of the time throughout the whole day. ", total);

    for(i=100;i>=5;i=i-5)

    {

        printf("%d ",j);

        for(j=0;j<24;j++)

        {

            if(percentON[j]/i>=1)

                printf(" *");

            else

                printf("  ");

        }

        printf(" ");

    }

    printf(" 12 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11");

    fclose(ifp);

    return 0;

Explanation / Answer

#include<stdio.h>



int main()
{

double previousValue,currentValue;

FILE*ifp;

ifp = fopen("value.txt", "r");

int percentON[24];

fscanf(ifp,"%lf",&previousValue);

int i,j,on,currentState=0;

//Calculation of PercentageON
for(i=0;i<24;i++)
{
on=0;
for(j=0;j<120;j++)
{
fscanf(ifp,"%lf",&currentValue);

if(currentState == 0)
{
if(previousValue-currentValue>=0.5)
{
currentState=1;
on = on +1;
}
}
else
{
if(currentValue-previousValue>=0.5)
currentState=0;
else
on = on+1;
}

previousValue = currentValue;
}

percentON[i] = (on*100)/120;
}



double sum = 0;
double total;

for (i=0; i<24;i++){
sum+= percentON[i];
}

total=sum/24;

//Printing Bar Graph
for(i=0;i<24;i++)
printf("%d ", percentON[i]);
printf(" ");

printf("The machine ran %.2lf percent of the time throughout the whole day. ", total);

for(i=100;i>=5;i=i-5)
{
printf("%3d",i);

for(j=0;j<24;j++)
{

if(percentON[j] >= i)

printf(" *");

else

printf(" ");

}

printf(" ");

}

printf(" 12 1 2 3 4 5 6 7 8 9101112 1 2 3 4 5 6 7 8 91011");

fclose(ifp);

return 0;
}