I am trying to design a program which contains a function which returns and outputs the smallest element in an array. When compiling the following code, I get the error message "mismatch in formal parameter list" for the function smallestIndex; the error appears to me in the line for returning the value from the function to the main. Any ideas on what I might be doing wrong?
#include <iostream>
using namespace std;
const int ARRAY_SIZE = 5;
int smallestIndex(int[], int);
void printArray(int[], int);
int main()
{
int A[ARRAY_SIZE] = {5,1,45,9,17};
printArray(A, ARRAY_SIZE);
int smallest;
smallest = smallestIndex(A, ARRAY_SIZE);
cout << smallest << endl;
system("PAUSE");
return 0;
}
void printArray(int A[], int size)
{
for (int i=0; i< size; i++)
cout << A[i] << " ";
cout << endl;
}
int smallestIndex(int A[], int size)
{
for (int index = 0; index < size-1 ; index++)
If you were to explicitly add {} to smallestIndex, you'd get
1 2 3 4 5 6 7 8 9 10 11 12
int smallestIndex(int A[], int size)
{
for (int index = 0; index < size-1 ; index++)
{
if (A[index] < A[index + 1])
{
int min = A[index];
}
}
return min;
}
You'd notice that the scope of variable min is just between lines 6-8. The min you return is the function std::min(). If you declared int min before the loop, it would be fine.