0% found this document useful (0 votes)
2 views3 pages

Homework 5

Homework #5 for ENG 200-04A requires students to write a program to compute the Greatest Common Divisor (GCD) of two integers using recursion and to create a Snake and Ladder game for two players, Red and Green. The GCD program must prompt the user for input and display the result, while the Snake and Ladder game involves rolling a die and moving players according to game rules, with specific positions for snakes and ladders. The assignment is due on November 17, 2024, at 11:59 PM in Moodle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Homework 5

Homework #5 for ENG 200-04A requires students to write a program to compute the Greatest Common Divisor (GCD) of two integers using recursion and to create a Snake and Ladder game for two players, Red and Green. The GCD program must prompt the user for input and display the result, while the Snake and Ladder game involves rolling a die and moving players according to game rules, with specific positions for snakes and ladders. The assignment is due on November 17, 2024, at 11:59 PM in Moodle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ENG 200-04A: Computer Programming for Engineers Dr.

Badour Albahar

Homework #5
November 10, 2024
Due Sunday, November 17, 2024 at 11:59PM in Moodle
Collaborators: (LIST OF YOUR COLLABORATORS, OR WRITE NONE)

1. Write a program that computes the Greatest Common Divisor (GCD) of two integers.
The program should use a recursive function call to calculate the GCD based on the following
formal definition:

x if y = 0
GCD(x, y) =
GCD(y, mod(x, y)) if x ≥ y and y > 0

where mod is the modulus function. The program asks the user to enter two integers. Then,
the program calls the GCD function and prints the result returned by the function.
Sample Output 1:

Enter the first integer: 5


Enter the second integer: 20
GCD = 5

Sample Output 2:

Enter the first integer: 20


Enter the second integer: 6
GCD = 2

Sample Output 3:

Enter the first integer: 101


Enter the second integer: 27
GCD = 1

2. Write a program of Snake and Ladder game (picture shown below). Assume that there
are only two players Red and Green. Game starts with position one for both players. Use a
random number generator to roll a die of 6 faces 1, 2, 3, 4, 5, 6 in two functions: MoveRed()
and MoveGreen(). The die specifies the number of moves for a player, and its value should be
used to update two class-level variables: RedCounter and GreenCounter. Assume that Red
is playing first. Each time the player moves, check if the player reaches a snake or a ladder
and do the action. The winner of the game is the player who reaches 25 or more first.

Homework 5 1 Due November 17, 2024


ENG 200-04A: Computer Programming for Engineers Dr. Badour Albahar

Three Snakes:

• At position 10 takes you down to 2


• At position 17 takes you down to 9
• At position 23 takes you down to 16

Three Ladders:

• At position 6 takes you up to 15


• At position 11 takes you up to 20
• At position 19 takes you up to 22

Sample Output 1:
Game Started
Red and Green at Position one

Die Result of Red = 5


Red moves to position 6
Ladder takes you up to Position 15

Die Result of Green = 3


Green moves to position 4

Die Result of Red = 1


Red moves to position 16

Die Result of Green = 5


Green moves to position 9

Die Result of Red = 2


Red moves to position 18

Die Result of Green = 3


Green moves to position 12

Die Result of Red = 1


Red moves to position 19
Ladder takes you up to Position 22

Die Result of Green = 1


Green moves to position 13

Die Result of Red = 5


Red moves to position 27

Game Stops. Red is the winner!

Homework 5 2 Due November 17, 2024


ENG 200-04A: Computer Programming for Engineers Dr. Badour Albahar

Sample Output 2:

Game Started
Red and Green at Position one

Die Result of Red = 4


Red moves to position 5

Die Result of Green = 5


Green moves to position 6
Ladder takes you up to Position 15

Die Result of Red = 1


Red moves to position 6
Ladder takes you up to Position 15

Die Result of Green = 4


Green moves to position 19
Ladder takes you up to Position 22

Die Result of Red = 6


Red moves to position 21

Die Result of Green = 1


Green moves to position 23
Snake takes you down to Position 16

Die Result of Red = 2


Red moves to position 23
Snake takes you down to Position 16

Die Result of Green = 5


Green moves to position 21

Die Result of Red = 3


Red moves to position 19
Ladder takes you up to Position 22

Die Result of Green = 6


Green moves to position 27

Game Stops. Green is the winner!

Homework 5 3 Due November 17, 2024

You might also like