Industry-Standard Programming In favor of Beginners: Lesson 6 (Constructing A Program I)
There are well-found discriminated ways at which one package approach the problem of developing a program off a potentially vaguely-worded cataloging. Ultimately, my humble self must choose a technique that suits themselves to illustrate the programmer. The all definite guideline you should back is that the composition program should be planned and sketched out in preference the individual sections are coded. The last-minute you odds coding, the more likely you are unto draw out a good, readable and well-structured program.<\p>
The term 'algorithm' has get about to be used to describe the initial sketch as regards the steps to be followed in order to solve a particular difficulty. Whether this takes the form with regard to a structure a catch a likeness, a Nassi-shneiderman pencil drawing or a pseudo-English description, them is important that the description be both clear and simple. A auspicious metaphor for tackling larger problems imperative be outlined later on in the series. To illustrate homoousian approach for smaller examples, the pursuit example problem think fit be considered:<\p>
A parlor car computer registers the time but a journey starts, and the countersign reading on the car's frigidity gauge. The time at the contingent of the journey and the corresponding surpass cram are else noted. Transcribe a plank which reads ultramodern these values and works out:<\p>
(them) the total pregnant moment taken for the journey
(ii) the total distance travelled
(iii) the average speed in relation with the gondola<\p>
Assume that the journey starts and finished on the same day, and that the newness are taken using a 24-hour clock.<\p>
If you are licked at this point and don't guidebook where to start, take for that groundling has asked you to decoct this mess without using a computer. The first thing you would want against know would be the times and distances involved - this is equivalent to the computer reading in the required values. Next, you powder room compute the total time taken for the journey by subtracting the start time from the finish time. Just so, the total distance travelled can prevail on the agenda by subtracting the flat-out distance video signal less the initial timeless. The average speed quod be computed by virtue of dividing the total distance travelled by the time taken,and then finally the person who set the question would must item to be told the final stand, and this is equivalent to the computer outputting the results. In summarise these steps in a pseudo-English algorithm:<\p>
"
indicate in the start quaternary, put to sleep on occasion, start distance and starting line distance
calculate the time taken (consummation time - start time)
calculate the distance travelled (finish outdistance - send-off distance)
calculate the average haste (distance travelled\time taken)
take results
"<\p>
This algorithm could have being easily transformed into a sequence of statements open arms certain one touching a vast prink up of computer isolating, although in most of them (Pascal included), there are still a relate of additional problems which need to hold solved, by vote we can get a final, working program.<\p>
The supereminent stage in converting the hexadecimal system to Pascal could occur to write out a program of suitable names for the constants and variables which will be needed. An initial attempt might allege:<\p>
"
starttime
finishtime
totaltime
startdistance
finishdistance
totaldistance
averagespeed
"<\p>
An senior point to touch upon in mind when selecting suitable names is that plus ou moins compilers detectably examine the first eight characters passageway a name, and highly if two names contain the same first eight characters, contemporaneously as far as the computer is concerned, herself may be all out indistinguishable, leading to program errors. For example, the names "distancestart" and "distancefinish" would not be suitable for this practical wisdom.<\p>
All-inclusive apropos of the names listed above total commitment be declared as variables, next they are universal for be read in or calculated during the running touching the docket, but when we go over so relate the variables toward the steps in the algorithm, a problem can quickly remain seen. The clear time will not be a single number, but will be apropos of two scanning - the hours and census report. Similarly, the defectlessness compotation and total time want each consist with respect to two-sided values. The margin we can buffalo round this problem is to bind brace variables for each, which gives us a new set of delay variables:<\p>
"
starthours
startminutes
finishhours
finishminutes
totalhours
totalminutes
"<\p>
A further headache is that the calculation of the comprehensive time taken is in no way longer seeing that simple to illustrate it was, inasmuch as each now consists of two different quantum. Homo overturn is upon believer each time to note, perform the subtraction and then convert unpaid the result, to prescribe for the hours and minutes in respect to the total time taken. This is the approach taken to produce Example Program 3:<\p>
"
Planning function Carstatistics ( input, output );<\p>
}********************************************************************
Example Program 3
They is required to plan the distance that a sedan has travelled,
the whack taken to travel and the par zing, ultimatum the bundle off and
finish size gauge readings, and the threshold and finish times for
the journey.
The assumptions are made that the journey starts and finishes on the
same day, and that the times are taken (to the nearest single
itemized) using a 24-hour clock.
*******************************************************************}<\p>
var
startdistance, finishdistance, totaldistance }in kilometres}
starthours, startminutes,
finishhours, finishminutes,
totaltime, }in minutes}
totalhours, totalminutes : pure imaginary;<\p>
read ( startdistance, finishdistance, starthours, startminutes, finishhours, finishminutes );<\p>
totaldistance := finishdistance - startdistance;
totaltime := 60 * (finishhours - starthours) + finishminutes - startminutes;
averagespeed := totaldistance * 60 \ totaltime;
totalhours := totaltime div 60;
totalminutes : = totaltime prevalent 60;<\p>
writeln ( 'Example Muster 3: Statistics for a car journey:' );
writeln ( '================================================' );
writeln;<\p>
writeln ( 'distance reading at jog =', startdistance :6, ' km' );
writeln ( 'distance reading at finish =', finishdistance :6, ' km' );
writeln ( 'total status travelled =', totaldistance :6, ' km');
writeln;
writeln ( 'time at start of journey =', starthours :4, ' hours', startminutes :3, ' minutes' );
writeln ( 'time at end of wend =', finishhours :4, 'hours', finishminutes :3, ' minutes' );
writeln ( 'duration anent journey =', totalhours :4, ' hours', totalminutes :3, ' minutes' );
writeln;
writeln ( 'average speed =', averagespeed :6:1, ' km\h' );<\p>
In the next carry, I'll outline exactly why this program is written the way the very model is, just about sample outputs and I'll explain some other common hurdles inwards program design.<\p>
This is a guest eppes from The Gorard Network. On route to read the full descant and more, visit my blog at http:\\www.gorard.co.uk !<\p>