read values into array,
then bubble sort it..
http://mathbits.com/mathbits/compsci/Arrays/Bubble.htm
and you have your min and max.
2007-02-20 14:15:27
·
answer #1
·
answered by Zlavzilla 3
·
1⤊
0⤋
Well, in order to find the maximum and minimum values in a data file, you have to read in the entire file. One could read the entire file into an array, sort it then look at the first and last elements, but this would be a waste of space depending upon the size of the file. There is a more efficient way.
The pseudocode of an algorithm to find the max and min would work something like this.
Find-Max-Min
Maximum Value = The smallest integer possible
Minimum Value = The largest integer possible
While there are still values in the file
Read a value from the file
If value > Maximum Value then Maximum Value = value
if value < Minimum Value then Minimum Value = value
done
All you need to do from there is convert this into C code. Possible options for reading in from a file are fgets, readline or read if you are feeling adventurous. Remember that most file reading options in C will read in char* variables instead of numbers. In these cases you will need to use the function atoi(char* value) to convert from a string to an int.
2007-02-20 23:59:54
·
answer #2
·
answered by Steve T 3
·
0⤊
1⤋
There is no need to store the whole file into an array... Assuming that u have in this file only numbers than you can read each number and u need only 2 vars initialized previosly with:
min = the maximum value possible and max= the minimum value posible (you choose the maximum value possible and minimum value possible). And than compare the number you read from the file with your current max and min... If it's greater than max than this number becomes max, if it is smaller than min than this number becomes min... And untill you read your file, you shold have found the min and max without using an array.
Happy coding :D
2007-02-21 03:28:33
·
answer #3
·
answered by None A 3
·
1⤊
0⤋
"There is no need to store the whole file into an array... Assuming that u have in this file only numbers than you can read each number and u need only 2 vars initialized previosly with:
min = the maximum value possible and max= the minimum value posible (you choose the maximum value possible and minimum value possible). And than compare the number you read from the file with your current max and min... If it's greater than max than this number becomes max, if it is smaller than min than this number becomes min... And untill you read your file, you shold have found the min and max without using an array.
Happy coding :D
"
The Algorithm for this would be more difficult as with a convential sort (bubble). The easiest way in my opinion is to place the data in an array and then use 2 for loops to itterate through the min value and max value.
Take a look at "bubble sort" (google it) this is the easiest way to sort through for values in a file.
rough pseudocode :
array=file
for count =1 to x
for count2 = 2 to x
if count > count2
temp=count
count=count2
max = temp
endif
end for
end for
for count =1 to x
for count2 = 2 to x
if count < count2
temp=count
count=count2
min = count
endif
end for
end for
2007-02-24 05:10:59
·
answer #4
·
answered by Siu02rk 3
·
0⤊
0⤋
I don't know much about C, but the principle will be the same in any language. Here's some pseudo-code that will do it - you'll need to translate it into the actual C code.
declare min, max
min = 100000
max = 0
open file as MyFile
repeat until EOF
{
read line from MyFile into var
if var > max then max = var
if var < min then min = var
}
close MyFile
print max, min
2007-02-21 09:53:15
·
answer #5
·
answered by Daniel R 6
·
0⤊
1⤋