Programming and Graphics

(Kiana) #1

334 Introduction to C++ Programming and Graphics


The following C++ code computes the sum of the inverses of the squares
of the firstNintegers,


double s=0;
int i;

for (i=1; i<=N; i+1)
{
s=s+1.0/(i*i);
}

InMatlab, the same code reads:


s=0;
for i=1:N
s=s+1.0/i^2;
end

InFortran 77, the same code reads:


s=0
Do i=1,N
s=s+1.0/i**2
End

Note the obligatory six blank spaces at the beginning of each line.


In Chapter 3, we discussed the bubble-sort code for sorting an array of
numbers or names. The Internet site:


http://www.codecodex.com/wiki/index.php?title=Bubble_sort

lists the bubble-sort code in some twenty languages, including C++ and For-
tran. Some of these codes, including Fortran, almost read like English.


In Section 4.5, we discussed the codebits.ccthat computes the maximum
integer that can be described with an available number of bits. The equivalent
Fortran 77program contained in the filebits.fis:


Program bits

Implicit Double Precision (a-h,o-z)
Integer p,q

write (6,*) " Will compute the greatest integer "
write (6,*) " that can be described with n bits "

98 write (6,*) " Enter the number of bits"
write (6,*) " (should be less than 32)"
Free download pdf