This is one of the more confusing programs that I have come across. First I do not totally understand what the requirements for the program are. However I think that the program comes close enough for the user to make modifications to meet the requirements.


This type of question can be typical for the books used for teaching programming languages. I have come across several books that put projects in the end of the chapter, where the projects are impossible to complete the way the book wants it done.


The user, from the United Kingdom (given the way the town is spelled, most likely form Wales) writes:


Hello, Could you please do some video tutorials on using loops to show if a number is greater than the rest of the numbers entered.

Write a program to ask the user to enter 12 integers and displays the largest integer, the smallest integer, it will terminate if a negative one number is entered, but it will work if the first value is that negative value.




Video: Pascal Helper Series Video 2 - A Very Confusing Program (9:51)
































The code used in this tutorial:



program Project1;


{$mode objfpc}{$H+}


uses

  crt,

  {$IFDEF UNIX}{$IFDEF UseCThreads}

  cthreads,

  {$ENDIF}{$ENDIF}

  Classes

  { you can add units after this };


var

  cnt, largest, smallest, num : integer;

  first_neg, stop:boolean;


begin


  clrscr();     {clears the screen uses crt}

  cnt := 1;

  stop := false;

  first_neg := false;


  repeat

    write('Enter An Integer: ');

    readln(num);


    if cnt = 1 then   {if this is the first value}

    begin

      if num = -1 then

        first_neg := true;   {the first value is negative}


      largest := num;   {by default the first value will be the highest and lowest}

      smallest := num;

    end

    else

    begin

      if (num = -1) and (first_neg = false) then  {if there is a -1 and the first

                                                                     value is something other than -1 }

         stop := true  {used to end the loop}

       else           {everything is fine}

      begin

        if num > largest then  {if the value is greater than the largest being held}

          largest := num;


       if num < smallest then {if the value is smaller than the smallest being held}

          smallest := num;

       end;


    end;

    cnt := cnt + 1;   {increment the counter to prevent an infinite loop}

  until (cnt = 13)  or (stop = true);



    writeln;

    writeln('The Largest Integer: ', largest);

    writeln('The Smallest Integer: ', smallest);


    readln;


end.


Previous                 Next