Task Sheet
Iteration
Task 1
You are to create a program that prints a grid of asterisks.
The following code prompts a user to enter the number of columns they would like and the
number of rows they would like.
<?php
$rows = readline("Please enter the number of rows: ");
$columns = readline("Please enter the number of columns: ");
You must add to the code so that the grid is output in the correct number of rows and
columns. For example, two rows and three columns should produce the following output.
* * *
* * *
You should use two for loops in completing your code.
Task 2
You are to create a program that simulates throwing two dice and counts the number of
throws that are needed until the dice match.
The values of the dice should be output on each throw.
When the dice match your program should output the number of throws and the final
matching values.
Dice 1: 2 | Dice 2: 6
Dice 1: 5 | Dice 2: 3
Dice 1: 5 | Dice 2: 5
Matching pair in 3 turns!
Your output should be similar to this.
Task 3
Write a game of Paper, Rock, Scissors; where the user is prompted to enter their choice as a
case-insensitive string (e.g. Paper or paper would be valid inputs) and the computer's choice
is generated randomly.
The game should continue playing until either the user or the computer scores 3 winning
games.
At the conclusion of the game your program should output the result (who won) and the
scores of both players.
Those not familiar with the game can learn a little about it here
https://en.wikipedia.org/wiki/Rock-paper-scissors. Just as a reminder:
Rock beats Scissors
Paper beats Rock
Scissors beats Paper
Rock, paper or scissors? rock
The computer won - paper beats rock!
Rock, paper or scissors? rock
It was a draw!
Rock, paper or scissors? rock
It was a draw!
Rock, paper or scissors? rock
The computer won - paper beats rock!
Rock, paper or scissors? rock
The computer won - paper beats rock!
The computer has won the game 3-0!
Output should be similar to that shown above.
Task 4
Write a program that stores the colours of the rainbow in order in an array. The program
will then continually prompt the user to enter an integer from 1 to 7 or -1 to end the
program. When the user enters an integer between 1 and 7 it will output the corresponding
colour from the rainbow.
For example, if the user enters 3 your program should output Yellow.
Write and test your program ensuring you test all 7 values and the exit value.
If you are unsure about the colours of the rainbow you can follow this link:
https://www.metoffice.gov.uk/learning/rainbows/colours-of-the-rainbow.
Task 5
The following code contains an array that holds the titles of five films.
<?php
$films = array(
'The Matrix', 'Man of Steel', 'Iron Man',
2
'Avatar', 'Twelve Monkeys'
);
Your task is to figure out how to sort the array so that it is in alphabetical order and then
output it as a list.
Task 6
Add to the following code so that the two-dimensional array $one_to_five_times_table is
output with each ‘table’ on separate lines.
<?php
$one_to_five_times_table = [
[1, 2, 3, 4, 5],
[2, 4, 6, 8, 10],
[3, 6, 9, 12, 15],
[4, 8, 12, 16, 20],
[5, 10, 15, 20, 25]
];
Your output should appear as follows.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Try using two foreach loops to accomplish this task.
3