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

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

Edited 4 Years Ago by pyTony: backtics

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.

Edited 4 Years Ago by Adak: new code flags

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