Difference between revisions of "CSC220 Homework 2 2010"
(→Problem #3: working with files) |
(→Part 3) |
||
Line 116: | Line 116: | ||
* Modify your Php program so that it will read the contents of '''play.txt''' as shown below: | * Modify your Php program so that it will read the contents of '''play.txt''' as shown below: | ||
− | + | ;The hero | |
− | + | : the hero is Peter Parker, known as Spiderman | |
− | + | ;The villain | |
+ | : the vilain is Norman Osborn, known as the Green Goblin | ||
+ | ;Supporting Characters | ||
+ | : Mary Jan Watson is the love interest, also known as MJ | ||
+ | : Ben Osborn is the lovin uncle, and we kow that Peter Parker could have prevented his death | ||
+ | : Mark Parker is the loving aunt, also known as Aunt May | ||
+ | :J. Jonah Jameson is the boss, known in the press room as JJ | ||
:Be aware that when I test your programs, I will have my own play.txt file that will not have the same characters, not in the same order, but you can be sure that some of them will be tagged as '''hero'''(s), spelled the same way as in '''play.txt''' or '''vilain'''(s), also spelled the same way. | :Be aware that when I test your programs, I will have my own play.txt file that will not have the same characters, not in the same order, but you can be sure that some of them will be tagged as '''hero'''(s), spelled the same way as in '''play.txt''' or '''vilain'''(s), also spelled the same way. |
Revision as of 16:38, 2 October 2010
--D. Thiebaut 18:45, 22 September 2010 (UTC)
This assignment can be done individually or in pairs, and is due on Wednesday, Sept 29th, at 11:59 p.m. + 1min.
Contents
Problem #1
- Write a Php program that displays the first 10 rows of Pascal's Triangle in a table using loops and arrays. The first 5 lines of the array are shown below.
1 | ||||
1 | 1 | |||
1 | 2 | 1 | ||
1 | 3 | 3 | 1 | |
1 | 4 | 6 | 4 | 1 |
- Store your Php program in a file called hw2a.php and submit it as follows:
submit hw2 hw2a.php
- Hints:
- Make your table display with borders. Feel free to format the rest of the page as you see fit.
- Do not reinvent the wheel. It is acceptable to lookup the algorithm for computing Pascal's triangle.
- Do not hesitate to code in a different language, as a way to approach this. If Java is more comfortable for you, do the coding in Java first, to prove to yourself that you understand the algorithm, then move to to Php.
Problem #2
- Same idea and algorithm as for Problem #1. But this time make your Php code display the first line of the Pascal Triangle that contains a term larger than 1000.
- Your program should output only one series of numbers, which is the given line in the triangle.
- Store your program in a file called hw2b.php and submit it as follows:
submit hw2 hw2b.php
- Hints:
- the line should contain 14 terms...
- you may want to display them in an HTML list if you want, or on a single line.
Problem #3: working with files
Part 1
This problem requires you to investigate some features of Php on your own. I will give you some pointers, but I expect you to do some discovering on your own. This problem might take longer than you think to solve, so start early!
- Do all your work in your public_html directory.
- Create a file called play.txt with the following lines (no blank lines in the file):
Mary Jane Watson, love interest, also known as MJ Peter Parker, hero, Spiderman Harry Osborn, best friend, son of Norman Osborn, the Green Goblin Ben Osborn, loving uncle, and we know that Peter could have prevented his death Norman Osborn, villain, the Green Goblin May Parker, loving aunt, also known as Aunt May J. Jonah Jameson, boss, known in the press room as JJ
- Those of you who have seen the movie (or read the comics) will know what this is all about. :-)
- Create the following Php program in your public directory. Call it readFile.php.
<html>
<body>
<?php
//--- read the lines from file play.txt and store in array $lines ---
$lines = file( 'play.txt' );
//--- print each line in a list ---
print "<ul>\n";
for ( $i=0; $i<count( $lines ); $i++ )
print "<li>$lines[$i]\n";
print "</ul>\n";
?>
</body>
</html>
- Load up the URL of your readFile.php program in your browser, of if you are on hadoop110, run it from the command line. Observe that you get all the lines from the file play.txt.
Part 2
- Go to http://us2.php.net/ and enter the following search term in the search bars:
- explode (search in the function list)
- Read about explode. Look at the examples.
- Same exercise for this function:
- trim (search in the function list)
- Read the documentation, see the examples.
- Use explode and trim to store the individual fields of the lines from the play.txt file into arrays, so that you can display just the names of the characters in Php, or just their role (hero, villain, etc...), or just the extra information we have on each character.
Part 3
- Modify your Php program so that it will read the contents of play.txt as shown below:
- The hero
- the hero is Peter Parker, known as Spiderman
- The villain
- the vilain is Norman Osborn, known as the Green Goblin
- Supporting Characters
- Mary Jan Watson is the love interest, also known as MJ
- Ben Osborn is the lovin uncle, and we kow that Peter Parker could have prevented his death
- Mark Parker is the loving aunt, also known as Aunt May
- J. Jonah Jameson is the boss, known in the press room as JJ
- Be aware that when I test your programs, I will have my own play.txt file that will not have the same characters, not in the same order, but you can be sure that some of them will be tagged as hero(s), spelled the same way as in play.txt or vilain(s), also spelled the same way.
- Hints: if you want to test for 2 different conditions to be true, use this form:
if ( ( expression_1 ) && ( expression_2 ) ) { ----------; ----------; } else { ----------; ----------; }
- && stands for AND, || for OR.
- Your program should either print Hero or Heros as the heading, depending on how many heros are in the file. Similar remark for the vilains.
- Store your program in a file called hw2c.php and submit it as follows:
submit hw2 hw2c.php