Greetings! I am new to the Daniweb community but hopefully you all can help me out with this problem!
I am to write a c++ function, smallestIndex that takes as parameters int array & its size and returns the index of the smallest element. then I have to write a program to test the function. So here's the code so far:
#include <iostream>
using namespace std;
int smallestIndex( int[], int); // function prototype
void main()
{
int arr[10] = {2,5,6,9,3,7,1,15,12,10};
int position;
position = smallestIndex(arr, 10);
cout << "The smallest Index is: " << position << endl;
}
int smallestIndex( int arr[], int size)
{
int smallestIndex=0;
int temp=arr[0];
int i;
for(int i=0;i<size;i++)
{
if(arr[i]<temp)
{
smallestIndex = i;
temp=arr[i];
}
}
return i;
}
I don't know if the function is correct. Someone on a different forum helped me out with correcting it so Hopefully it's right but I keep getting a warning at first then it says fatal error that variable i hasn't been initialized! SO I don't know. Then I was having trouble figuring out how to get it to output the value but hopefully I've figured that out up there at the top?? Let me know! THanks!!