CSC220 Homework 2 2010

From dftwiki3
Revision as of 15:23, 22 September 2010 by Thiebaut (talk | contribs)
Jump to: navigation, search

--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.








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
  • 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

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):
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>

  • 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 terms in the search bars:
    • explode (search in the function list)
    • trim (search in the function list)
  • Use explode and trip 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, vilain, 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:

CSC220SpiderManCharacters.png

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.
  • 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