Hi, I found a program for picking out the lowest value in an array. I can't seem to be able to get to the return value which should be 24. Printing the parameter attached to the return is not coming out on the serial print out. Had no objections to my coding after a few tries.
Anybody know how I can get the 'return' result?
Here is the code;
int array[6] = {25, 27, 24, 28, 26};
int getMin(int* array, int size);
return minIndex; //unconditionally end the loop() function and return to where it was called
Serial.println(minIndex); //so this code never gets executed
}
Reverse the order of the lines, but why are you using return anyway ?
The minimum value in your array is zero, reason being that you declare the array to have 6 elements but you only assign 5 elements and that cases the compiler to fill the 6th element with a zero. Another problem is that you are accessing the elements 1..5 leaving out the element at index 0.
EDIT: Element at index 0 is used as base for comparisons.
I have reversed the order of the lines Bob. It is now printing '5'. The lowest value is 24. So this is wrong. I am using return because this is a difficult to understand program which I found on the forum somewhere. I therefore wanted to copy it out exactly as given.
Therefore there is still something wrong with the program or the 24 is there somewhere and I need to find it
Looked up your sketch reference. It wouldn't work in my case. The elements of the array are determined in void loop. All of the sketches I have seen so far relating to this don't take place in the void loop. This would not be my complete sketch. The partial sketch should be returning 24. When interchanging the 'return' and 'print' lines as suggested I am getting the number 5. Interesting that this happens to be the number of elements in the array.
"return" should be giving me the minimum number but where do I find the actual value of the return?
No surprise really as you are printing minIndex, not the minimum value
Try this
int array[] = {25, 27, 24, 28, 26}; //allow the compiler to set the size of the array
byte NUMBER_OF_ELEMENTS = sizeof(array) / sizeof(array[0]); //calculate the ni,ber of array elements
void setup()
{
Serial.begin(115200);
}
void loop()
{
int min = 32000; //start with a huge value
for (int index = 0; index < NUMBER_OF_ELEMENTS; index++)
{
if (array[index] < min)
{
min = array[index]; //save the new minimum *value*
}
}
Serial.println(min); //print the miniumum value
}
Note how it gets the compiler to do some of the work
You are confusing 2 different things. If you want to store text in an array then you need one extra character at the end for the null terminator, which is zero (0x00). The zero tells any code using the text where it ends. "Hello" for example requires 6 places in an array.
You are not storing text, you are storing numbers. You need 1 place for each number, so 5 in the case of the examples you've given, which will be indexed from 0 to 4.
I do not need to look up anything, I know how it works. You are referring to c-strings (aka char arrays) which usually are null-terminated but that does not apply to binary arrays. You go look that up.
Yes I have it now. Program now working fine to pick out lowest number in a series of 5 numbers. Had to get rid of a few things in the example program I found that just weren’t necessary at all and probably stopped it working. Didn’t need “ int getMin(int* array, int size);” in the declaration. Or is it possible that this single line of code will get the minimum of the array? This is simpler than using the “for” statement. That is the strangest coding I have ever seen. Is that a multiply sign before array? If that does work where would the result be showen? How exactly would that fit into the coding?
It's a function prototype.
The Arduino IDE makes them automatically for you, but they are a part of C/C++ for forward references.
The '*' operator used here monadically means the variable is a pointer.
Yes, that is correct. What I meant was that a c-string may contain a list of strings / lines where each string is terminated by a '\n' and the entire list is terminated by '\0'. In Windows API you may have string lists where each string is terminated by '\0' and the list is then terminated by an empty string which results in a double null termination '\0\0'.