Jump to content

So I'm stuck on this question.

Assume that a file containing a series of integers is named as numbers.txt and exists on your computer's disk. Write a program that determines the largest and the smallest number stored in the file. Then write the largest number and smallest number back to the same file.

 

 

I can not seem to get this to go. We have to create our own number text before hand with random numbers in it as well. Can anyone please help me with this? Not just telling me what to do but actually showing (with code) what variables and things I have to do?

Link to comment
https://linustechtips.com/topic/74189-pascal-help/
Share on other sites

Link to post
Share on other sites

Let me rephrase.. What line would I have to use to get Pascal to find the highest and lowest number given in a text file? and how can I write those to values back to the file

 

There is no one liner for a problem like that.

You have to open the file, read the contents and, for each number, check if it is the lowest or the highest you have seen (you need to keep these in two vars) and when there are no more numbers write the min and max to the file and close it.

I can't give you any specifics on Pascal because I never used it, but that is the basic principle.

Link to comment
https://linustechtips.com/topic/74189-pascal-help/#findComment-1018491
Share on other sites

Link to post
Share on other sites

var lowvalue, highvalue, thisvalue, : integer;


     


begin


lowvalue := {largest integer value possible} ;


highvalue := {smallest integer value possible}  ;


read (thisvalue);


while not EOF do


     begin


     if (thisvalue < lowvalue)


     then lowvalue := thisvalue


     else if (thisvlaue > highvalue)


            then highvalue := thisvalue;


     read (thisvalue);


     end;


writeln ("Lowest value = ", lowvalue);


writeln ("Highest value = ", highvalue);


end;


80+ ratings certify electrical efficiency. Not quality.

 

Link to comment
https://linustechtips.com/topic/74189-pascal-help/#findComment-1036388
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×