Difference between revisions of "CSC220 Homework 2 2010"
(→Problem #2) |
(→Problem #3: working with files) |
||
Line 65: | Line 65: | ||
<code><pre> | <code><pre> | ||
+ | |||
<html> | <html> | ||
+ | |||
<body> | <body> | ||
+ | |||
<?php | <?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"; | print "<ul>\n"; | ||
− | + | for ( $i=0; $i<count( $lines ); $i++ ) | |
− | + | print "<li>$lines[$i]\n"; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
print "</ul>\n"; | print "</ul>\n"; | ||
Line 84: | Line 84: | ||
</body> | </body> | ||
</html> | </html> | ||
+ | |||
</pre></code> | </pre></code> | ||
Revision as of 15:03, 22 September 2010
--D. Thiebaut 18:45, 22 September 2010 (UTC)
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 pass 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
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):
Peter Parker, hero, Spiderman Norman Osborn, vilain, the Green Goblin Mary Jane Watson, love interest, also known as MJ 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 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>