Dependencies:   mbed

Committer:
CSTritt
Date:
Fri Oct 06 03:20:11 2017 +0000
Revision:
0:78eefa5ae28e
Initial version. Based on Horton's Program 8.3.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:78eefa5ae28e 1 /*
CSTritt 0:78eefa5ae28e 2 Program 8.3 Calculating an average using functions.
CSTritt 0:78eefa5ae28e 3 from Horton's Beginning C, 5th ed.
CSTritt 0:78eefa5ae28e 4
CSTritt 0:78eefa5ae28e 5 Note isolation of user input in main and a single function.
CSTritt 0:78eefa5ae28e 6
CSTritt 0:78eefa5ae28e 7 Ported to mbed by C. S. Tritt
CSTritt 0:78eefa5ae28e 8 Last revised: 10/5/17 (v. 1.0)
CSTritt 0:78eefa5ae28e 9 */
CSTritt 0:78eefa5ae28e 10 #include "mbed.h"
CSTritt 0:78eefa5ae28e 11
CSTritt 0:78eefa5ae28e 12 const int MAX_COUNT=10; // Set input limit. Protects against buffer overrun.
CSTritt 0:78eefa5ae28e 13
CSTritt 0:78eefa5ae28e 14 // Function prototypes...
CSTritt 0:78eefa5ae28e 15 float Sum(float x[], int n); // Sums the values in a float array.
CSTritt 0:78eefa5ae28e 16 float Average(float x[], int n); // Averages the values in a float array.
CSTritt 0:78eefa5ae28e 17 int GetData(float *data, int max_count); // Gets data from std Serial.
CSTritt 0:78eefa5ae28e 18
CSTritt 0:78eefa5ae28e 19 // main program - execution starts here.
CSTritt 0:78eefa5ae28e 20 int main(void) {
CSTritt 0:78eefa5ae28e 21 float samples[MAX_COUNT] = {0.0}; // Create array for samples.
CSTritt 0:78eefa5ae28e 22 int sampleCount = GetData(samples, MAX_COUNT); // Get values.
CSTritt 0:78eefa5ae28e 23 float average = Average(samples, sampleCount); // Find average.
CSTritt 0:78eefa5ae28e 24 // Display results.
CSTritt 0:78eefa5ae28e 25 printf("The average of the values you entered is: %.2f.\n", average);
CSTritt 0:78eefa5ae28e 26 return 0;
CSTritt 0:78eefa5ae28e 27 }
CSTritt 0:78eefa5ae28e 28
CSTritt 0:78eefa5ae28e 29 // Function to calculate the sum of array elements. n is the number of elements
CSTritt 0:78eefa5ae28e 30 // in array x.
CSTritt 0:78eefa5ae28e 31 float Sum(float x[], int n) {
CSTritt 0:78eefa5ae28e 32 float sum = 0.0;
CSTritt 0:78eefa5ae28e 33 for(int i = 0 ; i < n ; ++i) sum += x[i];
CSTritt 0:78eefa5ae28e 34 return sum;
CSTritt 0:78eefa5ae28e 35 }
CSTritt 0:78eefa5ae28e 36
CSTritt 0:78eefa5ae28e 37 // Function to calculate the average of array elements. Calls Sum.
CSTritt 0:78eefa5ae28e 38 float Average(float x[], int n) {
CSTritt 0:78eefa5ae28e 39 return Sum(x, n)/n;
CSTritt 0:78eefa5ae28e 40 }
CSTritt 0:78eefa5ae28e 41
CSTritt 0:78eefa5ae28e 42 // Function to read in data items and store in data array. The function returns
CSTritt 0:78eefa5ae28e 43 // the number of items stored.
CSTritt 0:78eefa5ae28e 44 int GetData(float *data, int max_count) {
CSTritt 0:78eefa5ae28e 45 int nValues = 0;
CSTritt 0:78eefa5ae28e 46 // Clear screen & move cursor to upper left ANSI/VT100 sequence.
CSTritt 0:78eefa5ae28e 47 const char ESC=27; printf("%c[2J", ESC); printf("%c[H", ESC);
CSTritt 0:78eefa5ae28e 48 printf("Assure Local Echo is activated!\n");
CSTritt 0:78eefa5ae28e 49 printf("Number of values to average (Maximum %d)? ", max_count);
CSTritt 0:78eefa5ae28e 50 scanf("%d", &nValues);
CSTritt 0:78eefa5ae28e 51 printf("%d was read.\n", nValues); // added by cst.
CSTritt 0:78eefa5ae28e 52 if(nValues > max_count) {
CSTritt 0:78eefa5ae28e 53 printf("Maximum count exceeded. %d items will be read.", max_count);
CSTritt 0:78eefa5ae28e 54 nValues = max_count;
CSTritt 0:78eefa5ae28e 55 }
CSTritt 0:78eefa5ae28e 56 printf("Enter values:\n");
CSTritt 0:78eefa5ae28e 57 for( int i = 0 ; i < nValues ; ++i) {
CSTritt 0:78eefa5ae28e 58 scanf("%f", &data[i]);
CSTritt 0:78eefa5ae28e 59 printf("%f was read.\n", data[i]); // added by cst.
CSTritt 0:78eefa5ae28e 60 }
CSTritt 0:78eefa5ae28e 61 return nValues;
CSTritt 0:78eefa5ae28e 62 }