Activity: Get started with PHP

(no submission)

Purpose: Hands-on experience with PHP; get ready to work on your assignment (course project)

For this activity, you may work alone or with another student in this course. Write PHP programs to solve at least three of the following problems.


Problem #1: Simple grids

Imagine you are writing a PHP app that takes a grid information and a tile. Use  simplegrid.php  (text version), write 2 functions: isDiagonal and isEdge

  1. Imagine that the user specifies the width of a grid, as well as a tile. Write a PHP function named isDiagonal that will return either true or false depending on whether or not the tile is along the diagonal of the square grid. For example, on a 7x7 grid, assuming the tile numbers (starting at the top-left corner) are 1, 2, 3, ..., 49

    1 2 3 4 5 6 7
    8
    15
    22 23 24 25 26 27 28
    29 30
    36
    43

    all yellow tiles are on the diagonal.

    You may use the following formulas in your algorithm as needed:

    $row = intdiv(($tile - 1), $width);       // intdev() returns the integer quotient of the division of dividend by divisor
    $col = fmod(($tile - 1), $width);         // fmod() returns the remainder of the division 

  2. Imagine that the user specifies the width and height of a grid, as well as a tile. Write a PHP function named isEdge that will return either true or false depending on whether or not the tile is along the edge of grid. For example, on a 7x8 grid, assuming the tile numbers (starting at the top-left corner) are 1, 2, 3, ..., 56

    1 2 3 4 5 6 7
    8
    15
    22 23 24 25 26 27 28
    29 30
    36
    43
    50

    all yellow tiles are on the edge.

    You may use the following formulas in your algorithm as needed:

    $row = intdiv(($tile - 1), $width);       // intdev() returns the integer quotient of the division of dividend by divisor
    $col = fmod(($tile - 1), $width);         // fmod() returns the remainder of the division 

Problem #2: Compute Average

Use  computeAvg.php  (text version). Write a PHP function that accepts six project scores and an option specifying whether the lowest project score will be dropped. All scores are out of 100 points, except the first (which is out of 30) and the fifth (which is out of 50). You may assume that the drop option is given as a boolean value where  true  means  drop the lowest project score  and  false  means  no drop.  If there are multiple lowest project scores (i.e., same scores), you decide which one to drop.

The function then computes and returns the highest average score for the projects.

For example, if a user enters
    Score for project 1 = 15
    Score for project 2 = 55
    Score for project 3 = 55
    Score for project 4 = 55
    Score for project 5 = 25
    Score for project 6 = 55

With the lowest project score being dropped, the function returns 54
Without the lowest project score being dropped, the function returns 53.33

You do not need to round the average.


Problem #3: Combine two friend books

Use  combineFriendBook.php  (text version). Write a PHP function that accepts two arrays of friend books and returns a merged friend book.

For example, if the incoming address books are

friend_book1 = Array('Humpty' => '111-111-1111', 'Dumpty' => '222-222-2222', '
                     'Duh' => '333-333-3333', 'Oops' => '444-444-4444', 'Huh' => '555-555-5555')

friend_book2 = Array('Dumpty' => 'dumpty@uva.edu', 'Duh' => 'duh@uva.edu', 
                     'Humpty' => 'humpty@uva.edu', 'Huh' => 'huh@uva.edu', 
                     'Wacky' => 'wacky@uva.edu') 
The function returns a merged friend book in a (human-readable) format; for example,
Humpty : [ 111-111-1111, humpty@uva.edu ]
Dumpty : [ 222-222-2222, dumpty@uva.edu ]
Duh : [ 333-333-3333, duh@uva.edu ]
Oops : [ 444-444-4444 ]
Huh : [ 555-555-5555, huh@uva.edu ]
Wacky : [ wacky@uva.edu ] 

Problem #4: Simple arrays

Use  simplearray.php  (text version), write 2 functions: findLargest and divideByThree

  1. Write a PHP function named findLargest that takes an array of integers and then returns the index of the last occurrence of the largest element in the array. For example,
    • If an incoming array is [1, 20, 3, 4, 5, 6, 7, 8, 9], the function would return 1
    • If an incoming array is [1, 2, 4, 50, 50, 8, 30], the function would return 4
    • If an incoming array is [0, 0, 0, 0, 0, 0], the function would return 5
    • If an incoming array is [1, 2, 4, 50, 50, 8, 50], the function would return 6

  2. Write a PHP function named divideByThree that takes an array of integers and then returns the number of elements in the array that are evenly divisible by 3. For example,
    • If an incoming array is [1, 2, 3, 4, 5, 6, 7, 8, 9], the function would return 3
    • If an incoming array is [1, 2, 4, 5, 7, 8, 30], the function would return 1
    • If an incoming array is [0, 0, 0, 0, 0, 0], the function would return 0
    • If an incoming array is [1, 2, 4, 50, 50, 8, 50], the function would return 0

To deploy and test your program use one of the following options: