Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:f5bbc63c654b, 2017-10-23 (annotated)
- Committer:
- CSTritt
- Date:
- Mon Oct 23 13:06:52 2017 +0000
- Revision:
- 0:f5bbc63c654b
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:f5bbc63c654b | 1 | /* |
CSTritt | 0:f5bbc63c654b | 2 | Project: ArrayRunSize |
CSTritt | 0:f5bbc63c654b | 3 | File: main.cpp |
CSTritt | 0:f5bbc63c654b | 4 | Ported to mbed/Nucleo by: Dr. C. S. Tritt |
CSTritt | 0:f5bbc63c654b | 5 | Last revised: 10/22/17 (v. 1.1) |
CSTritt | 0:f5bbc63c654b | 6 | |
CSTritt | 0:f5bbc63c654b | 7 | Horton Program 5.7 Averaging a variable number of grades. Runtime array |
CSTritt | 0:f5bbc63c654b | 8 | creation. Also demonstrates array output showing index values. |
CSTritt | 0:f5bbc63c654b | 9 | |
CSTritt | 0:f5bbc63c654b | 10 | */ |
CSTritt | 0:f5bbc63c654b | 11 | #include "mbed.h" |
CSTritt | 0:f5bbc63c654b | 12 | |
CSTritt | 0:f5bbc63c654b | 13 | int main(void) |
CSTritt | 0:f5bbc63c654b | 14 | { |
CSTritt | 0:f5bbc63c654b | 15 | printf("\nAssure Terminal > local echo is on.\n"); |
CSTritt | 0:f5bbc63c654b | 16 | printf("Enter the number of grades: "); |
CSTritt | 0:f5bbc63c654b | 17 | size_t nGrades = 0; // Number of grades. |
CSTritt | 0:f5bbc63c654b | 18 | scanf("%zd", &nGrades); // The z is a type modifier for size type. |
CSTritt | 0:f5bbc63c654b | 19 | float grade[nGrades]; // Array storing nGrades values. |
CSTritt | 0:f5bbc63c654b | 20 | float sum = 0.0f; // Initialize sum of grades. |
CSTritt | 0:f5bbc63c654b | 21 | printf("\nEnter the %zd grades:\n", nGrades); // Prompt for grade entry. |
CSTritt | 0:f5bbc63c654b | 22 | |
CSTritt | 0:f5bbc63c654b | 23 | // Read the grades to be averaged. |
CSTritt | 0:f5bbc63c654b | 24 | for(size_t i = 0 ; i < nGrades ; ++i) { |
CSTritt | 0:f5bbc63c654b | 25 | printf("%zd> ", i + 1); |
CSTritt | 0:f5bbc63c654b | 26 | scanf("%f", &grade[i]); // Read a grade. |
CSTritt | 0:f5bbc63c654b | 27 | sum += grade[i]; // Add it to the running sum. |
CSTritt | 0:f5bbc63c654b | 28 | } |
CSTritt | 0:f5bbc63c654b | 29 | |
CSTritt | 0:f5bbc63c654b | 30 | // Display results. |
CSTritt | 0:f5bbc63c654b | 31 | printf("The grades you entered are:\n"); |
CSTritt | 0:f5bbc63c654b | 32 | for(size_t i = 0 ; i < nGrades ; ++i) { |
CSTritt | 0:f5bbc63c654b | 33 | printf("Grade[%2zd] = %.1f ", i + 1, grade[i]); |
CSTritt | 0:f5bbc63c654b | 34 | if ((i+1) % 5 == 0) printf("\n"); // After 5 values, go to a new line. |
CSTritt | 0:f5bbc63c654b | 35 | } |
CSTritt | 0:f5bbc63c654b | 36 | |
CSTritt | 0:f5bbc63c654b | 37 | float average = sum/nGrades; // Calculate and display the average. |
CSTritt | 0:f5bbc63c654b | 38 | printf("\nAverage of the %zd grades entered is: %.2f\n", nGrades, average); |
CSTritt | 0:f5bbc63c654b | 39 | while (true) {}; |
CSTritt | 0:f5bbc63c654b | 40 | } |