// TEAM 2 - ACTIVITY 3 - PROCEDURE
// COMPUTE THE HIGHEST NUMBER IN THE INPUT ELEMENTS
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>
/* Display */
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
/* Keypad setup */
const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
{'1', '2', '3', '+'},
{'4', '5', '6', '-'},
{'7', '8', '9', '*'},
{'.', '0', '=', '/'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
lcd.setCursor(0, 0); // set cursor at (c,r)
}
void loop() {
// dynamically allocate array with size n
double *data;
lcd.print("NO. OF ELEMENTS:"); //printf("Enter the total number of elements: ");
lcd.setCursor(0, 1);
char key = NULL;
String elements = "";
String inputElements = "";
int n;
while (key != '=') { // equals ('=') key used as enter key (changed to ✔)
key = keypad.getKey();
if (key >= '0' && key <= '9') //only act on numeric keys
{
lcd.print(key);
elements += key;
}
}
n = elements.toDouble(); // convert number of elements to double
delay(1000);
lcd.clear();
// Allocating memory for n elements
data = (double *)calloc(n, sizeof(double));
// Storing elements
for (int i = 0; i < n; ++i) {
lcd.setCursor(0, 0);
lcd.print("ELEMENTS(" + String(n) + "):");
lcd.setCursor(0, 1);
key = NULL;
while (key != '=') { // equals ('=') key used as enter key (changed to ✔)
key = keypad.getKey();
if (key >= '0' && key <= '9' || key == '.') {
inputElements += key;
lcd.print(key);
}
}
data[i] = inputElements.toDouble();
inputElements = "";
lcd.clear();
}
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HIGHEST:");
lcd.setCursor(0, 1);
// Computing the largest number
for (int i = 1; i < n; ++i) {
if (*data < *(data + i)) {
*data = *(data + i);
}
}
lcd.print(*data);
while (true) {
continue;
}
}