Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- int i, problem = 0;
- float readings[6];
- float average, average10, average15, diff;
- void checkwithin10pc()
- {
- for(i = 0; i < 6; i++)
- {
- diff = fabs(readings[i] - average);
- if (diff > average10)
- {
- problem++;
- if(problem == 1) printf("\nThe following problems occurred:\n");
- printf("%d. Voltage at hour %d was %.1f volts (difference of %.1f volts).\n", problem, i + 1, readings[i], diff);
- }
- }
- }
- void checkwithin15pc()
- {
- for(i = 1; i < 6; i++)
- {
- diff = fabs(readings[i] - readings[i - 1]);
- if (diff > average15)
- {
- problem++;
- if(problem == 1) printf("\nThe following problems occurred:\n");
- printf("%d. Voltage change from hour %d to hour %d was %.1f volts.\n", problem, i, i + 1, diff);
- }
- }
- }
- int main()
- {
- float total = 0.0f;
- /* simulate obtaining 6 readings by asking user to input them */
- for(i = 1; i <= 6; i++)
- {
- printf("Enter reading at hour %d to 1 dp : ", i);
- scanf("%f", &readings[i - 1]);
- if (readings[i - 1] < 0)
- {
- printf("Can't be negative, please re-enter.\n");
- i--;
- }
- else
- {
- total += readings[i - 1];
- }
- }
- average = total / 6.0f;
- average10 = .1f * average;
- average15 = .15 * average;
- printf("\nThe average is %.1f volts.\n", average);
- printf("10%% = %.1f volts.\n", average10);
- printf("15%% = %.1f volts.\n", average15);
- checkwithin10pc();
- checkwithin15pc();
- if(!problem) printf("No problems were encountered.");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement