954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with smallest index of an Array!!

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

abarnett
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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.

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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

abarnett
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

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

abarnett
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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

zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
 
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

abarnett
Newbie Poster
19 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Cheers... ! works great

HSH

highspeedhook
Newbie Poster
4 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You