## waqas94

hey guys !! i am new at c++ so i need a bit of help .. i am trying to solve different problems but i am stuck on this problem ..
write a c++ function , smallestindex that takes as parameters an int array and its size and returns the index of the smallest element in the array .

i dont want the complete source code but i need a bit of guidance how to get started .. i will be thankful ....

## phorce 131

Hey,

You need to firstly look into arrays, and, then how to pass (array pointers) into functions, once you've found these things out you can then sort the array (bubble sort e.g.) and find the lowest value. Not allowed to provide a solution to this probem!

## Bob 15

I would have to politely disagree with phorce. Bubblesort would change the indexes of the elements which would prevent you from returning the index of the smallest value (a requirement).

I'm making an assumption about the type of solution expected here, given the way your problem is worded.

If you think about it, you will need to step through the array that is passed into the function and test the values so that you can determine which is the smallest, which suggests some form of iteration, or loop.

You'll need to maintain a record of the smallest value found so far so that you can compare it with subsequent values.

You'll also need to maintain a record of the index of that smallest value found so that you can return it from the function at the end (so this record will need to persist beyond the lifetime of the loop).

It may help to write out the steps in natural language before you move to coding up your solution.

## np complete 8

If you want to do it with sorting algorithms, you can copy the original array into a new array. Then sort it and store the smallest element. Comparing it with original array would give you its index as required.

There is another way which Bob has pointed out. In a loop check for smallest element. This will not change array index.

## Suzie999 245

Pass the array to a function, and its length of course.
pseudo func code:

``````unsigned returnval = 9999999999999; // or the highest value you expect

for(unsigned i = 0; i < len; i++){

if(array element i is greater than returnval){
continue the loop
}

else make returnval equal to array element i

}

return returnval
``````

## WaltP 2,905

I would have to politely disagree with phorce. Bubblesort would change the indexes of the elements which would prevent you from returning the index of the smallest value (a requirement).

Not if you do it correctly. But that's probably beyond the OP scope of knowlwdge. But, sorting is definitely the wrong way to go. You don't sort just to find the min or max values of any list. You sort to put the values in an order.

If you want to do it with sorting algorithms, you can copy the original array into a new array. Then sort it and store the smallest element. Comparing it with original array would give you its index as required.

Why? Here's what you propose:
1) Loop to copy the entire array into another array
2) Start the bubble sort
2a) Loop to put the lowest value into place (not you now have the lowest value but ignore it that fact)
2b) Loop again to put the next lowest in place.
2c) repeat for all the values -1 in the array (19 times total for 20 values)
3) Loop yet again to compare the sorted last value with each value in the original array.

There is another way which Bob has pointed out. In a loop check for smallest element. This will not change array index.

And this one is:
1) Start by assuming the first value is the lowest and save it and the index
2) Loop to compare each value with the one saved
2a) If less, save it and the index

1 loop vs 21 loops.
19 compares vs 210 compares (for a naive sort)

## WaltP 2,905

Pass the array to a function, and its length of course.
pseudo func code:

``````     unsigned returnval = 9999999999999; // or the highest value you expect

for(unsigned i = 0; i < len; i++){

if(array element i is greater than returnval){
continue the loop
}

else make returnval equal to array element i

}

return returnval
``````

.

``````    returnval = fist value in the array
for(unsigned i = 1; i < len; i++)
{
if(array element i is greater than returnval)
{
continue the loop
}
else make returnval equal to array element i
}
return returnval
``````

But then again, you forgot to "return the index of the smallest element in the array". ;o)