Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /*
  2. * Program RPM.c
  3. *
  4. * Illustrates how a single function can pass back several values using
  5. * parameters passed by reference.
  6. *
  7. * Displays the minimum, maximum and average values of an electric motor
  8. * RPM data stored in integer array.
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. /* Number of RPM entries in the array */
  14. #define DATASIZE 16
  15.  
  16. /* Function prototypes */
  17. void RPMstats(int * min, int * max, double * avg, int data[]);
  18.  
  19. int main(void)
  20. {
  21.  
  22. /* Declare int variables to hold minimum, and maximum RPM values */
  23. int min=0,max=0;
  24. /* declare double variable to store the average RPM value */
  25. double avg=0.00;
  26.  
  27. /* Hard-coded RPM data values (in real life would be provided by sensor. */
  28. int RPMdata[] = { 2023, 1987, 1902, 1902, 1689, 1778, 1939, 1546,
  29. 1345, 1777, 2321, 2322, 2331, 2200, 2222, 1954};
  30.  
  31. /* Call RPMstats function to obtain minimum, maximum and average RPM values */
  32. RPMstats(&min,&max,&avg,RPMdata);
  33.  
  34. /* Print the results */
  35. printf("The min value: %d\n", min);
  36. printf("The maximum value: %d\n", max);
  37. printf("The average value: %lf\n", avg);
  38.  
  39. /* Pause */
  40. printf("Press ENTER to exit");
  41. getchar();
  42.  
  43. /* Return success to OS */
  44. return 0;
  45. }
  46.  
  47. /* Function RPMstats
  48. *
  49. * Returns minimum, maximum and average RPM values to the calling program
  50. * using parameters passed by reference.
  51. *
  52. * Parameter min: pointer to integer, will hold the value of the minimum RPM
  53. * Parameter max: pointer to integer, will hold the value of the maximum RPM
  54. * Parameter avg: pointer to double, will hold the value of the average RPM
  55. * Parameter data: array containing the RPM values
  56. *
  57. * Return: no return value. All results passed back to parameters by reference
  58. */
  59. void RPMstats(int * min, int * max, double * avg, int data[])
  60. {
  61. /* local variables */
  62. int a=0;
  63. double sum=0.00;
  64.  
  65. /* initialise min, max and sum to the value of the 1st element in array */
  66. *min=data[a];
  67. *max=data[a];
  68.  
  69. /* obtain min, max, and the sum in loop through rest of array*/
  70. for(a=0;a<DATASIZE;a++)
  71. {
  72. if(data[a]<=*min)
  73. *min=data[a];
  74. else
  75. *min=*min;
  76.  
  77. if(data[a]>=*max)
  78. *max=data[a];
  79. else
  80. *max=*max;
  81.  
  82. sum+=data[a];
  83.  
  84. }
  85.  
  86. /* Pass back value of average */
  87. *avg=sum/DATASIZE;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement