Charles Tritt
/
21_FuncSumEx_v5
Diff: main.cpp
- Revision:
- 116:518fcfbba05c
- Parent:
- 115:f22cbbc92bae
- Child:
- 117:b40379408898
--- a/main.cpp Wed Oct 13 14:27:35 2021 +0000 +++ b/main.cpp Thu Oct 14 15:11:03 2021 +0000 @@ -1,35 +1,62 @@ /* -Project: 21_ConsoleIO_v5 -File: main.cpp - - This simple program demonstrates that C passes arguments by value. + Program 8.3 Calculating an average using functions. + from Horton's Beginning C, 5th ed. - Uses VT-100 escape sequences to prevent scrolling. See - http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html and/or - https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797. + Note isolation of user input in main and a single function. - Written by: Dr. C. S. Tritt; Last revised 10/11/21 (v. 1.1) + Ported to mbed by C. S. Tritt + Last revised: 10/5/17 (v. 1.1) */ #include "mbed.h" -int main() -{ - const int DELAY = 2000000;; // mS wait time. - const char ESC = 27; // Define escape character for escape sequence. - printf("%c[2J%c[H", ESC, ESC); // ANSI/VT100 clear screen/home. +const int MAX_COUNT=10; // Set input limit. Protects against buffer overrun. + +// Function prototypes... +float Sum(float x[], int size); // Sums the values in a float array. +float Average(float x[], int size); // Averages the values in a float array. +int GetData(float *data, int max_count); // Gets data from std Serial. + +// main program - execution starts here. +int main(void) { + float samples[MAX_COUNT] = {0.0}; // Create array for samples. + int sampleCount = GetData(samples, MAX_COUNT); // Get values. + float average = Average(samples, sampleCount); // Find average. + // Display results. + printf("The average of the values you entered is: %.2f.\n", average); + return 0; +} + +// Function to calculate the sum of array elements. n is the number of elements +// in array x. +float Sum(float x[], int size) { + float sum = 0.0; + for(int i = 0 ; i < size ; ++i) sum += x[i]; + return sum; +} - // Replace the code below with your code. - int count = 1; // Declared in outer block. - int iCount = 0; // This is another variable called count. - do { - --iCount; // this applies to inner count. - printf("Count Down = %d and abs(Count Down) = %d\n", - iCount, abs(iCount)); - } while( ++count <= 5); // This works with outer count. +// Function to calculate the average of array elements. Calls Sum. +float Average(float x[], int size) { + return Sum(x, size)/size; +} - printf("count = %d\n", count); // Inner count is dead. - - while(true) { // Main forever loop. - ThisThread::sleep_for(DELAY); // Pause +// Function to read in data items and store in data array. The function returns +// the number of items stored. +int GetData(float *data, int max_size) { + int nValues = 0; + // Clear screen & move cursor to upper left ANSI/VT100 sequence. + const char ESC=27; printf("%c[2J", ESC); printf("%c[H", ESC); + printf("Assure Local Echo is activated!\n"); + printf("Number of values to average (Maximum %d)? ", max_size); + scanf("%d", &nValues); + printf("%d was read.\n", nValues); // added by cst. + if(nValues > max_size) { + printf("Maximum count exceeded. %d items will be read.", max_size); + nValues = max_size; } + printf("Enter values:\n"); + for( int i = 0 ; i < nValues ; ++i) { + scanf("%f", &data[i]); + printf("%f was read.\n", data[i]); // added by cst. + } + return nValues; } \ No newline at end of file