Below is the code we develop in the video above. It inputs an array of grades, it averages the grades, and then finds the High and Low Grades.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
float grades[25]; int numGrades; int i; String myPrompt; float bucket; float avg; float highGrade = 0; float lowGrade = 100; void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: Serial.println("How Many Grades? "); while (Serial.available() == 0) { } numGrades = Serial.parseInt(); for (i = 0; i < numGrades; i = i + 1) { myPrompt = "Please Input Grade: " + String(i + 1); Serial.println(myPrompt); while (Serial.available() == 0) { } grades[i] = Serial.parseFloat(); } Serial.println(); Serial.println("Your Grades Are: "); for (i = 0; i < numGrades; i = i + 1) { Serial.println(grades[i]); } bucket=0; for (i = 0; i < numGrades; i = i + 1) { bucket = bucket + grades[i]; } avg = bucket / numGrades; Serial.println("Your Average is: " + String(avg)); highGrade=0; lowGrade=100; for (i=0;i<numGrades;i=i+1){ if(grades[i]>highGrade){ highGrade=grades[i]; } if(grades[i]<lowGrade){ lowGrade=grades[i]; } } Serial.println("Your High Grade is: "+String(highGrade)); Serial.println("Your Low Grade is: "+String(lowGrade)); Serial.println(); } |