We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,434 Members — Technology Publication meets Social Media

# how to find minimum value in array

i need help finding the low value in an array after reading file with numbers. With my program I am to assume that the size an array is 100 even if only 10, 31, 50 spaces are taken up.

The problem im running in to is when i iniztialize the array i set it equal to zero so all 100 space a filled with zero.
and for some reason whatever the program reads from the file(see file 1) when I call the Min function it only finds the 0's values that were not filled from the file.

file 1(lengthOfFile = 20, and the lowest value should be 39)
40
71
92
82
62
92
53
73
63
53
84
85
86
76
66
57
78
59
39
89

``````int main(void)
{
//Local Variables
double listArray[MAX_ARRAY_SIZE] = {0};
int lengthOfFile = 0;
double minVal = 0.0;

//Begin
lengthOfFile = GetInput(listArray);
minVal = Min(listArray, lengthOfFile);
DisplayStats(minVal);
}// end function main

double Min(double inputArray[], int inputLengthOfList)
{
//Local Variables
int i = 0;
double minVal = 0.0;

for(i = 0; i < inputLengthOfList; i++)
{
if(inputArray[i] < minVal)
minVal = inputArray[i];
}// end for

return minVal;
}// end function Min
``````

Any help and suggestions would be greatly apperitated

Thanks

zingwing

3
Contributors
2
Replies
3 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
zingwing
Newbie Poster
8 posts since Mar 2012
Reputation Points: 10
Skill Endorsements: 0

0 is smaller than any of your numbers in file, so you should initialize `minVal` to `inputArray[0]` and start for from `i=1`.

pyTony
pyMod
Moderator
6,330 posts since Apr 2010
Reputation Points: 879
Skill Endorsements: 27
Question Answered as of 1 Year Ago by pyTony

A better solution is to count up how many values you're putting into your array. For instance, what if the lowest value was zero? What if you have no idea what the lowest value might be in the data?

The better logic is:

1) count the number of values you are putting into your array, and loop ONLY through those values. 30 Values read in? Then you'll search from array[0] to array[29], only.

2) set the initial minimum value to the first value you put into your array. If the value is 100, and there are lesser values in the data, that's OK. Because you'll have an if() statement inside the loop:

``````if(newValue is < minimumValue)
minimumValue = newValue;
``````

and you have the minimum value as soon as you've read in all the newValues, and made this comparison.

It's fast, (no dealing with irrelevant array values (lots of zeroes), clear, and accurate.

Posting Virtuoso
1,641 posts since Jun 2008
Reputation Points: 456