lab4.ino (1126B)
1 #include "util.h" 2 3 const char *order[] = {"Secs", "Mins", "Hours"}; 4 const int time_orders = SIZE(order); 5 6 const char *sports[] = {"swim", "run", "cycle"}; 7 const int number_of_sports = SIZE(sports); 8 9 int 10 input_time(const char *order, const char *sport) { 11 SEprintf("How many %s did %s take: ", order, sport); 12 return readint(); 13 } 14 15 void 16 input_sport(const char *sport, int times[time_orders]) { 17 for (int i = 0; i < time_orders; i++) 18 times[i] = input_time(order[i], sport); 19 } 20 21 void 22 normalize(int times[time_orders]) { 23 for (int i = 0; i < time_orders - 1; i++) { 24 times[i + 1] += times[i] / 60; 25 times[i] %= 60; 26 } 27 } 28 29 void 30 setup() { 31 int sport_times[number_of_sports + 1][time_orders] = {0}; 32 Serial.begin(9600); 33 34 for (int i = 0; i < number_of_sports; i++) 35 input_sport(sports[i], sport_times[i]); 36 37 for (int i = 0; i < number_of_sports; i++) 38 for (int j = 0; j < time_orders; j++) 39 sport_times[time_orders][j] += sport_times[i][j]; 40 41 normalize(sport_times[3]); 42 43 SEprintf("Total time: %dsec %dmin %dhr.\n", sport_times[time_orders][0], sport_times[time_orders][1], sport_times[time_orders][2]); 44 } 45 46 void loop() {}