An electric power substation measures voltages hourly for 72 continuous hours be
ID: 3806075 • Letter: A
Question
An electric power substation measures voltages hourly for 72 continuous hours before they send off a report to the manager. Write a C program to generate this report that reads in 72 integer voltage readings from standard input in order to determine:
the mean voltage over the time period
the hours at which the recorded voltage varies from the mean by more than 10%
any adjacent hours when the change from one reading to the next was greater than 15% of the mean
the hours when a brownout occurred
You must store the voltage readings in an array. You may assume that all of the values inputted are legal (i.e. 0 <= v <= 125) and that the first value inputted is considered hour #1. Assume that this substation’s goal is to provide a steady voltage of 120v and that a brownout is defined as a time where the voltage drops below 10.8% of this value. Your program should output all of the information shown in the sample run below using exactly the same labels and spacings. If there are no hours that meet one of the criteria you should print the string “[none]” (without the quotes).
2 Sample Run
Sample run data:
Fix the code please, so that it can print None:
#include <stdio.h>
void printHour10Mean(int voltage[], int n, double mean, double mean10)
{
printf("hours that vary >= 10%% from mean: ");
int i = 0;
int first = 1;
for(i = 0; i < n; i++)
{
double diff = voltage[i] - mean;
if (diff < 0) diff = -1*diff;
if (diff > mean10)
{
if (first == 1)
{
printf("%d", (i+1));
first = 0;
}
else
{
printf(",%d", (i+1));
}
}
}
printf(" ");
}
void printHour15MeanNeighbour(int voltage[], int n, double mean, double mean15)
{
printf("hours of neighbors that vary >= 15%% of mean: ");
int i = 0;
int first = 1;
for(i = 1; i < n; i++)
{
double diff = voltage[i] - voltage[i-1];
if (diff < 0) diff = -1*diff;
if (diff > mean15)
{
if (first == 1)
{
printf("(%d,%d)", i, (i+1));
first = 0;
}
else
{
printf(",(%d,%d)", i, (i+1));
}
}
}
printf(" ");
}
void printBrownOutHour(int voltage[], int n, double mean, double brownout)
{
printf("hours brownout occurred: ");
int i = 0;
int first = 1;
for(i = 1; i < n; i++)
{
double diff = voltage[i] - brownout;
if (diff < 0)
{
if (first == 1)
{
printf("%d", (i+1));
first = 0;
}
else
{
printf(",%d", (i+1));
}
}
}
printf(" ");
}
int main()
{
int n = 72;
int voltage[n];
printf("EGRE245 Project #5 Spring 2017 - Yaqoub Alqallaf ");
int i;
double mean = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &voltage[i]);
mean += voltage[i];
}
mean = mean / n;
double mean10 = mean*.1;
double mean15 = mean*.15;
double brownout = 120*(1-.108);
printf("Number of voltages: 72 ");
printf("Mean voltage: %.2f (10%% of mean: %.2f; 15%% of mean: %.2f) ", mean, mean10, mean15);
printf("Brownout threshold: %.2f ", brownout);
printHour10Mean(voltage, n, mean, mean10);
printHour15MeanNeighbour(voltage, n, mean, mean15);
printBrownOutHour(voltage, n, mean, brownout);
}
Explanation / Answer
#include <stdio.h>
void printHour10Mean(int voltage[], int n, double mean, double mean10)
{
printf("hours that vary >= 10%% from mean: ");
int i = 0;
int first = 1;
for(i = 0; i < n; i++)
{
double diff = voltage[i] - mean;
if (diff < 0) diff = -1*diff;
if (diff > mean10)
{
if (first == 1)
{
printf("%d", (i+1));
first = 0;
}
else
{
printf(",%d", (i+1));
}
}
}
printf(" ");
}
void printHour15MeanNeighbour(int voltage[], int n, double mean, double mean15)
{
printf("hours of neighbors that vary >= 15%% of mean: ");
int i = 0;
int first = 1;
for(i = 1; i < n; i++)
{
double diff = voltage[i] - voltage[i-1];
if (diff < 0) diff = -1*diff;
if (diff > mean15)
{
if (first == 1)
{
printf("(%d,%d)", i, (i+1));
first = 0;
}
else
{
printf(",(%d,%d)", i, (i+1));
}
}
}
printf(" ");
}
void printBrownOutHour(int voltage[], int n, double mean, double brownout)
{
printf("hours brownout occurred: ");
int i = 0;
int first = 1;
for(i = 1; i < n; i++)
{
double diff = voltage[i] - brownout;
if (diff < 0)
{
if (first == 1)
{
printf("%d", (i+1));
first = 0;
}
else
{
printf(",%d", (i+1));
}
}
}
printf(" ");
}
int main()
{
int n = 72;
int voltage[n];
printf("EGRE245 Project #5 Spring 2017 - Yaqoub Alqallaf ");
int i;
double mean = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &voltage[i]);
mean += voltage[i];
}
mean = mean / n;
double mean10 = mean*.1;
double mean15 = mean*.15;
double brownout = 120*(1-.108);
printf("Number of voltages: 72 ");
printf("Mean voltage: %.2f (10%% of mean: %.2f; 15%% of mean: %.2f) ", mean, mean10, mean15);
printf("Brownout threshold: %.2f ", brownout);
printHour10Mean(voltage, n, mean, mean10);
printHour15MeanNeighbour(voltage, n, mean, mean15);
printBrownOutHour(voltage, n, mean, brownout);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.