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!!

Good day and welcome aboard. A few things.
You could use something like this:

int smallestIndex( int arr[], int size)
{
    int smallestIndex=0;
    int temp=arr[0];
    for(int i=0;i<size;i++)
    {
       if(arr[i]<temp)
       {
           smallestIndex = i;
           temp=arr[i];
        }
     }
   return i;
}

Hope that helps.

Perhaps return smallestIndex instead?

> but I keep getting a warning at first then it says fatal error that
> variable i hasn't been initialized!
How many i variables are in your function - I count TWO.
Guess which one you're returning - the uninitialised one.

alright alright thanks for the info now here's the issue in main I don't think it's returning it correctly

void main()
{ 
    int arr[6] = {2,5,6,9,3,7};
    int position;

    position = smallestIndex(arr, 6);

	cout << "The smallest Index is: " << position << endl;


}

it says the smallest index is 6 which is not true because that stores a 7 in it. so what's wrong w/ that? or am I totally off??

you could also
a. make the function const-correct
b. use a size_t instead of an int for the size

size_t smallestIndex( const int arr[], size_t size)
{
    size_t smallestIndex=0;
    int temp=arr[0];
    for(size_t i=1;i<size;i++)
    {
       if(arr[i]<temp)
       {
           smallestIndex = i;
           temp=arr[i];
        }
     }
   return smallestIndex;
}

thanks for the help! I had some other people in a different community forum helping but yet confusing me all at the same time-I got it figured out!! THANKS!!!

If you've got the correct answer....and the probelm is solved....please post it for the benefit of the entire community.

If you've got the correct answer....and the probelm is solved....please post it for the benefit of the entire community.

alright!

#include <iostream>

using namespace std;
int smallestIndex( int[], int);  // function prototype

void main()
{ 
    int arr[6] = {8,5,6,9,3,7};
    int position;

    position = smallestIndex(arr, 6);

	cout << "The smallest Index is: " << position << endl;


}

 int smallestIndex( int arr[], int size)
{
    int smallestIndex=0;
    int temp=arr[0];
	int i;

    for(i=0; i<size; i++)
    {
       if(arr[i]<temp)
       {
           smallestIndex = i;
                   temp=arr[i];
        }
     }
   return smallestIndex;
 }

there it is

This question has already been answered. Start a new discussion instead.