COMP 1500
1) The textbook has several versions of the algorithm to add up a series of numbers, in
Fortran, C, C++, Java, JavaScript, and Python (pages 77-81).
a) In your owns words, describe the idea of the algorithm. Make sure to include the
important details that matter, while leaving out language specific details.
The algorithm adds up a list of numbers, one at a time. It starts with a running total of 0. Then it
reads the next number, and adds it to the total. It keeps going until there are no numbers left,
which it knows either by counting how many numbers. When the loop ends, the total holds the
sum of every number, and the algorithm prints it.
b) The Fortran example is the only one that doesn’t include the word while to signal
iteration. How does Fortran perform the iterative part of the algorithm?
Fortran is the only example that doesn't use the word while. Instead, it does the iterative part
(the loop) using line labels (numbers like 10 and 20) combined with jumps using if and goto
statements.
c) List 5 examples of how one of the languages expresses part of the algorithm
differently than the others.
- Fortran uses read and write statements, while C++ uses stream operators (cin >>
and cout <<).
- Fortran relies on line labels and goto jumps instead of using the word while.
- Fortran uses .eq. to check equality, whereas C, Java, and Python use = =.
- C, C++, and Java group statements using curly braces {}, while Python uses
indentation.
- C++ requires explicit type declarations like int num;, whereas Python handles
variables dynamically without them.
2) The following four requests could come in to an operating system as it is running on a
computer system:
The clock in the computer has just ‘ticked’ and we need to update a seconds
counter.
The program running on processor 2 is trying to perform an illegal operation
code.
The plug was pulled on the power supply, and the system will run out of power
in 50 msec.