I'm trying to create a program that determines the min when I press 0 and the max when I press 9. The numbers it will pull from is an array of 5 numbers. Can you guys help me understand what's wrong?
Here's my code:
float Lab2Numbers[5] = {4, 6, 9, 1, 3};
void setup() {
Serial.begin(); // Supposed to insert baud in the "()" but currently don't have arduino at home
}
void loop() {
long nMax = 0, nMin = 0;
for (int x = 0; x <= 1; x++;)
{
if (nMax <= Lab2Numbers[5])
{
nMax = Lab2Numbers[5];
}
}
}
Serial.Println(Lab2Max);
for (int x = 0; x <= 1; x++)
{
if (nMin >= Lab2Numbers[x])
{
nMin = Lab2Numbers[x];
}
}
Serial.println(nMin);
delay(10000);
if (Serial.available())
{
char UserInput = Serial.read();
if (UserInput == '0' || UserInput == '9')
{
if (UserInput == '0')
{
Serial.println("The minimum number is: "; nMin);
}
else
{
Serial.println("The maximum number is: "; nMax);
}
}
else
{
Serial.println("Invalid answer. Please input the number 9 or 0: ");
Serial.println(UserInput);
}
}
There are parts of your code that are missing because you didn't read Nick Gammon's two posts at the top of this Forum on guidelines for posting here, especially using code tags. So, read those guidelines, edit your post so the code is within code tags, and then we'll be able to read it. Also, if you use Ctrl-T in the IDE before you post your code, it will be formatted in a standard format.
Since this is likely a homework assignment, here's the answer. However, you better be able to explain it to your teacher or he/she will know you didn't write it.
#define ELEMENTS(x) (sizeof(x) / sizeof(x[0]))
long Lab2Numbers[] = {4, 6, 9, 1, 3};
void setup() {
Serial.begin(9600);
}
void loop() {
long nMax;
long nMin;
if (Serial.available())
{
FindMinMax(Lab2Numbers, &nMax, &nMin, ELEMENTS(Lab2Numbers));
// Serial.println(nMax);
// Serial.println(nMin);
char UserInput = Serial.read();
switch (UserInput) {
case '0':
Serial.print("The minimum number is: ");
Serial.println(nMin);
break;
case '9':
Serial.print("The maximum number is: ");
Serial.println(nMax);
break;
default:
Serial.println("Invalid answer. Please input the number 9 or 0: ");
Serial.println(UserInput);
break;
}
}
}
void FindMinMax(long val[], long *max, long *min, int elements)
{
*max = -2000000000;
*min = 2000000000;
for (int x = 0; x < elements; x++)
{
if (*max <= val[x])
{
*max = val[x];
}
if (*min >= val[x]) {
*min = val[x];
}
}
}
Make sure you sent the Terminal to "No line ending"