template<class T, int U>
int smallest_index(T (&array)[U])
{
int result = 0;
for (int i = 1; i < sizeof(array) / sizeof(T); i++)
result = array[i] < array[result] ? i : result;
return result;
}
// Try to avoid as much as possible to use global variables, observe the new location of alpha, that would help you to understand function more ,try to do the same with MAXINDEX
//You should know that you left the element alpha[24] empty/or taking a random value
Thank you for pointing that out alpha[24] was not included in my for loops.
Yes. Your right about the global variables. I forgot to put it back into main, after I placed it there when troubleshooting adding a function.
Thank you for
1 2 3 4 5 6 7 8 9 10
void function(double array[MAXINDEX])
{
// Print Small Index
int index, smindx = 0;
for (index = 0; index < MAXINDEX; index ++)
{
if (array[index] < array[smindx])
smindx = index;
}
A question I have is the book I am going over tends to create array functions like with the formal parameters like my original function prototype.
e.g
void smallestIndex(constdouble array[], int listSize);
where it appears that you are only using one formal parameter
to me what your doing seems to make sense.
Are you aware of the style difference and why one may work better than another?
His function takes one parameter because the size of the array is defined globally, thus passing it again would be redundant.
In my version, the size of the array is determined at run-time using advanced ideas.
The book's version is beginner-friendly; there's no trivial way to determine the size of a passed array since this decays to a pointer, so the function requires it.
By the way, to print the array, try this:
1 2 3 4 5 6 7 8 9 10 11 12
// Print Table
cout << setprecision(7);
int k = 1;
for (int j = 0; j < MAXINDEX; j++ ,k++)
{
cout << setw(8) << alpha[j] << " ";
if (k == 10)
{
cout << endl;
k = 0;
}
}
@Josue Molina Thank you input here. That makes more sense to me about the function prototype. also thank you for your feedback about the printing, I see what I was doing wrong there.