English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have to find the location of the smallest element in an array. The code I have does not seem to work. If anyone knows how to do this your help will be gladly appreciated.

2007-09-20 02:48:51 · 3 answers · asked by ballababy0520 1 in Computers & Internet Programming & Design

3 answers

Assume the first number is the smallest, and compare with the rest and replace with any smaller than that

marray = marray of values

smallest = marray[0];

for( int i = 0; i < max array elem; i++
if (marray[i] < smallest )
smallest = marray[i]

2007-09-20 03:04:38 · answer #1 · answered by Arkane Steelblade 4 · 0 0

nSmallestIndex = 1
nSmallestValue = Array[nIndex]
for nIndex = 2 to SizeofArray -1
if nSmallestValue < Array[nIndex] then
nSmallestIndex = nIndex
nSmallestValue = Array[nIndex]
end
next

PS: You never specified the programming language
You did not say if it was a multi dimmension array

2007-09-20 03:00:50 · answer #2 · answered by againyourright 4 · 0 0

define mimimumValue with an initial value of a high number

For i = 0 to length of array step 1
if arrayValue[i] < minimumValue
minimumValue = arrayValue[i]
endIf
EndFor
write text and minimumValue to standard output.

That should do it.

2007-09-20 02:58:27 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers