Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Program RPM.c
- *
- * Illustrates how a single function can pass back several values using
- * parameters passed by reference.
- *
- * Displays the minimum, maximum and average values of an electric motor
- * RPM data stored in integer array.
- */
- #include <stdio.h>
- /* Number of RPM entries in the array */
- #define DATASIZE 16
- /* Function prototypes */
- void RPMstats(int * min, int * max, double * avg, int data[]);
- int main(void)
- {
- /* Declare int variables to hold minimum, and maximum RPM values */
- int min=0,max=0;
- /* declare double variable to store the average RPM value */
- double avg=0.00;
- /* Hard-coded RPM data values (in real life would be provided by sensor. */
- int RPMdata[] = { 2023, 1987, 1902, 1902, 1689, 1778, 1939, 1546,
- 1345, 1777, 2321, 2322, 2331, 2200, 2222, 1954};
- /* Call RPMstats function to obtain minimum, maximum and average RPM values */
- RPMstats(&min,&max,&avg,RPMdata);
- /* Print the results */
- printf("The min value: %d\n", min);
- printf("The maximum value: %d\n", max);
- printf("The average value: %lf\n", avg);
- /* Pause */
- printf("Press ENTER to exit");
- getchar();
- /* Return success to OS */
- return 0;
- }
- /* Function RPMstats
- *
- * Returns minimum, maximum and average RPM values to the calling program
- * using parameters passed by reference.
- *
- * Parameter min: pointer to integer, will hold the value of the minimum RPM
- * Parameter max: pointer to integer, will hold the value of the maximum RPM
- * Parameter avg: pointer to double, will hold the value of the average RPM
- * Parameter data: array containing the RPM values
- *
- * Return: no return value. All results passed back to parameters by reference
- */
- void RPMstats(int * min, int * max, double * avg, int data[])
- {
- /* local variables */
- int a=0;
- double sum=0.00;
- /* initialise min, max and sum to the value of the 1st element in array */
- *min=data[a];
- *max=data[a];
- /* obtain min, max, and the sum in loop through rest of array*/
- for(a=0;a<DATASIZE;a++)
- {
- if(data[a]<=*min)
- *min=data[a];
- else
- *min=*min;
- if(data[a]>=*max)
- *max=data[a];
- else
- *max=*max;
- sum+=data[a];
- }
- /* Pass back value of average */
- *avg=sum/DATASIZE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement