Advertisement
Guest User

Untitled

a guest
Sep 11th, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int i, problem = 0;
  5. float readings[6];
  6. float average, average10, average15, diff;
  7.  
  8. void checkwithin10pc()
  9. {
  10. for(i = 0; i < 6; i++)
  11. {
  12. diff = fabs(readings[i] - average);
  13. if (diff > average10)
  14. {
  15. problem++;
  16. if(problem == 1) printf("\nThe following problems occurred:\n");
  17. printf("%d. Voltage at hour %d was %.1f volts (difference of %.1f volts).\n", problem, i + 1, readings[i], diff);
  18. }
  19. }
  20. }
  21.  
  22. void checkwithin15pc()
  23. {
  24. for(i = 1; i < 6; i++)
  25. {
  26. diff = fabs(readings[i] - readings[i - 1]);
  27. if (diff > average15)
  28. {
  29. problem++;
  30. if(problem == 1) printf("\nThe following problems occurred:\n");
  31. printf("%d. Voltage change from hour %d to hour %d was %.1f volts.\n", problem, i, i + 1, diff);
  32. }
  33. }
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39. float total = 0.0f;
  40.  
  41. /* simulate obtaining 6 readings by asking user to input them */
  42.  
  43. for(i = 1; i <= 6; i++)
  44. {
  45. printf("Enter reading at hour %d to 1 dp : ", i);
  46. scanf("%f", &readings[i - 1]);
  47. if (readings[i - 1] < 0)
  48. {
  49. printf("Can't be negative, please re-enter.\n");
  50. i--;
  51. }
  52. else
  53. {
  54. total += readings[i - 1];
  55. }
  56. }
  57.  
  58. average = total / 6.0f;
  59. average10 = .1f * average;
  60. average15 = .15 * average;
  61.  
  62. printf("\nThe average is %.1f volts.\n", average);
  63. printf("10%% = %.1f volts.\n", average10);
  64. printf("15%% = %.1f volts.\n", average15);
  65.  
  66. checkwithin10pc();
  67. checkwithin15pc();
  68.  
  69. if(!problem) printf("No problems were encountered.");
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement