Pseudocode – 40 course points
In this assignment you will practice writing and evaluating algorithms, and counting the number of operations that are executed when the algorithm is evaluated.
- [4 points] Gravitational Force.
- This problem discusses an algorithm that calculates and displays the gravitational force (F) between two objects.
- The program inputs the mass of the first object (use variable m1), then the mass of the second object (use variable m2), and the distance between the first object and second object (use variable r). Gravitational force is calculated by the following formula:
- F = g * (m1 * m2) / (r*r) where g = 6.6743e-11
- Pre-Condition: assume m1, m2, and r are values greater than or equal to zero. NOTE: Do not use loops for this program. Example: Input: 36000.0 1080.0 50
- Output: 1.037987136E-6
-
- [1.5 points] What are the inputs, outputs, error conditions?
- [2.5 points] Write the algorithm in pseudocode.
- [7.5 points] Palindrome. This problem discusses an algorithm that inputs 6 (six) integer numbers and displays TRUE if:
- the first number is equal to the last number, and
- the second number is equal to the second to last number, and
- the third number is equal to the third to last number.
It displays FALSE otherwise.
NOTE: Do not use IF statements on this program.
Pre-condition: Inputs are integer numbers and there will always be 6 numbers.
Example 1:
Input: 3 2 1 1 2 3
Output: TRUEExample 2:
Input: 1 2 3 4 5 6
Output: FALSE
- [1.5 points] What are the inputs, outputs, error conditions?
- [6 points] Write the algorithm in pseudocode.
- [11.5 points] RU Buses. You might have noticed that each Rutgers Campus Bus has its own four-digit number. Suppose that the sum of an LX buses’ numbers are always even and the sum of an H buses’ numbers are always odd. For example, LX 4136 (4+1+3+6 = 14 is an even number).Write an algorithm that reads a bus four-digit number and displays LX or H depending on which bus route it belongs to.
NOTE: You can use modulus to extract the last digit of a number.
Example:
Input: 4685
4+6+8+5=23 and 23 is an odd number
Output: H
- [1.5 points] What are the inputs, outputs, error conditions?
- [10 points] Write the algorithm in pseudocode.
- [10 points] Evaluate and count operations. Consider the following code.
- SET c TO 2
- SET i TO 1
- WHILE i <= 4
- COMPUTE c AS c+i
- IF c%2 IS 0 THEN
- DISPLAY c
- ELSE
- DISPLAY i
- ENDIF
- ADD 1 TO i
- ENDWHILE
- [5 points] What is displayed?
- [5 points] How many operations are executed?
- [7 points] Evaluate and count operations. Consider the following code where a > 0.
- READ a
- SET b TO 0
- WHILE a >= b
- ADD 1 TO b
- ENDWHILE
- How many operations are executed? Write your answer in terms of a.
Before submission
Collaboration policy. Read our collaboration policy here.
Submitting the assignment. Submit your assignment via the Web submission system called Gradescope. To do this, click on Gradescope on Canvas left pane. You will see the assignment.
Getting help
If anything is unclear, don’t hesitate to drop by office hours or post a question on Piazza.
- Find instructors office hours here
- Post questions on Piazza, it is anonymous. Canvas -> Piazza
- Find tutors office hours on Canvas -> Tutoring, RU CATS
- Find head TAs office hours here
- In addition to office hours we have the CAVE (Collaborative Academic Versatile Environment), a community space staffed with lab assistants which are undergraduate students further along the CS major to answer questions.
Problems by Haolin Jin, Ayden Lyubimov, and Vian Miranda