Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Field Width Specifiers

The field width specifiers, are basically a way to force the formatting of numerical output. Using field width specifiers allows the user to decide how the output will look on your screen.
Assuming that the variables sum and total are both 255.
Note:- The character '_', means a space is produced on the screen.

  • INTEGER
    WRITELN(sum); produces: _________255 default field width of 12
    WRITELN(sum:5); produces: __255
    WRITELN(sum:7); produces: ____255
    WRITELN(sum:0); produces: 255 takes only as much room as it needs.
  • REAL
    WRITELN(total); produces: 2.550000E+02 exponential result.
    WRITELN(total:8); produces: 2.6E+02
    WRITELN(total:6:2); produces: 255.00
    WRITELN(total:8:4); produces: 255.0000


Notice that the double field width specifiers of the REAL. The first number is the total number of places including one for the decimal point, while the second number is the number of places after the decimal point. This next program calculates the equivalent miles from the given input in Kilometers. The program uses the reserved word CONST, which is short for constant, to define the value of the multiplication factor factor (possibly a bad choice of identifier there). This is an aid in much larger programs, when the same value might be used often, and prevents the number becoming the victim of a typo further on in the program. Another benefit of using the CONST is that if you at some point in the future decide that you need to change its value, it only needs to be changed in one place. You could imagine, wanting to use the ratio PI in a program, with the difficulty of typing the approximation 3.1415927 in thirty or more times. It would be much better just to put in the program the lines,

CONST pi = 3.1415927;


This would allow you to write the identifier pi where ever you need the value. Again, the identifier called factor, could be any name you choose. Notice that with CONST an equals sign is required rather than a colon.

Program 6

PROGRAM metric(INPUT,OUTPUT); { this program converts kilometers into miles } CONST factor = 0.62137; VAR kilometers, miles : REAL; BEGIN WRITE('Enter kilometers : '); READLN(kilometers); WRITE(kilometers:4:2,' Km is equivalent to '); WRITELN(kilometers*factor:4:2,' miles'); END. { program metric }


What's New? Both variables their types declared as REAL this time, note the difference in the field width specifiers. Notice too in the output, the use of both WRITE and WRITELN. The difference in operation of these two reserved words is simply that with WRITE, the cursor remains on the same line while the program continues on to the next line, and the WRITELN makes the cursor take a new line. (experiment with them!). Also notice in the final line,

WRITELN(kilometers*factor:4:2,' miles');


that the '*' character is used, this is the multiplication operator in Pascal for both the types INTEGER and REAL. This is another simple program, which prompts the user for the three dimensions of a room, then computes the volume.

Program 7



PROGRAM volume(INPUT,OUTPUT); { program calculates the volume of a room } VAR width, length, height : REAL; BEGIN WRITE('Enter the width of the room : '); READLN(width); WRITELN; WRITE('Enter the length of the room : '); READLN(length); WRITELN; WRITE('Enter the height of the room : '); READLN(height); WRITELN; WRITE('The volume of the room is : '); WRITELN(width*length*height:4:2); END. { program volume }


What's New? You have seen the answer being calculated in the output line already. The new thing is WRITELN;. This makes the program output a blank line on the screen.
Try adding a

WRITELN(' =====================================');


and you will have begun to add output formatting to the screen layout. If the theorem of the Greek Mathematician, Pythagoras (582 - 500 BCE), is not known to you, it is this. The square of the hypotenuse, of a right-angled-triangle is equal to the sum of the squares of the other two sides. Simply put. If the lengths of the shortest two sides of a right-angled-triangle are squared, (multiplied by themselves), and then added together, the length of the hypotenuse is the square root of the result. Here is a program that uses that theorem to calculate the hypotenuse, given the lengths of the other two sides.

Program 8

PROGRAM pythagoras(INPUT,OUTPUT); { program calculates the length of a triangles hypotenuse, given the length of the other two sides.} VAR height, length : REAL; BEGIN WRITE('Please enter height : '); READLN(height); WRITE('Please enter length : '); READLN(length); WRITELN; WRITE('The length of the hypotenuse is : ');{*} WRITELN(SQRT(SQR(height) + SQR(length)):4:2); END. { program pythagoras }


What's New? Well, now we are starting to use the built in functions of Pascal. The first, SQR, given a number, INTEGER or REAL produces the square of that number. The next, SQRT produces the square root of the number. The line marked {*} might just be a little confusing to you. The order of the calculations on that line is this:

  1. The square of the variable height is calculated.
  2. The square of the variable length is calculated.
  3. The results of 1 and 2 above added together.
  4. The square root of 3 above calculated, and written to the screen as the result.


This is a fairly simple, and powerful program. I hope you are beginning to see the possibilities to which computer programs can be turned.


Date: 2016-03-03; view: 748


<== previous page | next page ==>
Comments in a program | More on the type STRING
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)