Docsity

Assignment 3: Using Arrays and String/Character Data | COMP 110, Assignments of Computer Science

Toggle sidebar
bg1
COMP 110
Spring 2007
Assignment 3: Using Arrays and String/Character data
Due: Mon. Feb 26 at 4:00 pm
For this assignment you will create a web page with an embedded JavaScript program.
You will store this program as an HTML file in your UNC web space, and you will give
it an 8-character or more filename that only you know. Then we can retrieve, run, and
grade it by pointing our browser www.unc.edu/~ yourOnyen/yourSecretName.html but no
one else can see it.
Specs
Use a simple text editor to create this program. Do not use Web generation tools. You
may use the Lint tool, or other syntax checkers, to help you debug your JavaScript.
HTML page structure
Your script should be written as a function in the head section of an HTML page. That
HTML page will serve as documentation for the program… it will explain what the
program will do, what inputs are expected from the user (what are legal input values) and
what information the user can expect to be computed, or what activities he/she can
perform. Also explain how to make the program terminate. Somewhere on your HTML
page will be a button that says something like “Click here to run the program”. When
clicked, that button will execute your function/script.
Input/Output
In this program, user input will be obtained using the “prompt” and “confirm” statements.
Output will be reported to the user via the “alert” statements initially, and then at the end
some information will be reported with “document.write” statements.
Functionality
Your program will create an array… initially make it size 10. The user will be asked to
input 10 words (using prompts). The words the user inputs will get stored in your array.
After the array is loaded with the user’s input, the program will begin a section where the
user will be asked to input a word, and the program will check to see if that word is in the
array or not. An alert box will be popped up saying whether the word was in the array
already or not, and if found, it will report what array location the word is in. This section
of code is obviously a loop, so you need a way to get out of the loop. Therefore, have in
this loop (either beginning or end, I don’t care) a “confirm” statement asking if the user
would like to look up a word (or quit).
Keep a second array, the same length as the array of words (10). Initially, load this array
with all 0’s (zeros). This second array will be a collection of counters corresponding to
the array of words. The counters will count how many times a word was looked up
(meaning found). So when a word is looked up, if it is found you need to increment the
pf3

Partial preview of the text

Download Assignment 3: Using Arrays and String/Character Data | COMP 110 and more Assignments Computer Science in PDF only on Docsity!

COMP 110

Spring 2007

Assignment 3: Using Arrays and String/Character data

Due: Mon. Feb 26 at 4:00 pm For this assignment you will create a web page with an embedded JavaScript program. You will store this program as an HTML file in your UNC web space, and you will give it an 8-character or more filename that only you know. Then we can retrieve, run, and grade it by pointing our browser www.unc.edu/~ yourOnyen/yourSecretName.html but no one else can see it.

Specs

Use a simple text editor to create this program. Do not use Web generation tools. You may use the Lint tool, or other syntax checkers, to help you debug your JavaScript. HTML page structure Your script should be written as a function in the head section of an HTML page. That HTML page will serve as documentation for the program… it will explain what the program will do, what inputs are expected from the user (what are legal input values) and what information the user can expect to be computed, or what activities he/she can perform. Also explain how to make the program terminate. Somewhere on your HTML page will be a button that says something like “Click here to run the program”. When clicked, that button will execute your function/script. Input/Output In this program, user input will be obtained using the “prompt” and “confirm” statements. Output will be reported to the user via the “alert” statements initially, and then at the end some information will be reported with “document.write” statements. Functionality Your program will create an array… initially make it size 10. The user will be asked to input 10 words (using prompts). The words the user inputs will get stored in your array. After the array is loaded with the user’s input, the program will begin a section where the user will be asked to input a word, and the program will check to see if that word is in the array or not. An alert box will be popped up saying whether the word was in the array already or not, and if found, it will report what array location the word is in. This section of code is obviously a loop, so you need a way to get out of the loop. Therefore, have in this loop (either beginning or end, I don’t care) a “confirm” statement asking if the user would like to look up a word (or quit). Keep a second array, the same length as the array of words (10). Initially, load this array with all 0’s (zeros). This second array will be a collection of counters corresponding to the array of words. The counters will count how many times a word was looked up (meaning found). So when a word is looked up, if it is found you need to increment the

corresponding counter. For example, let’s call our word array “dictionary” and lets call the counter array “hits”. Let’s say dictionary[6] is the word “pecuniary”. If the user asks to find “pecuniary”, then I will find it in location 6 and I will increment hits[6] by one. At any point id I examine the value stored in hits[6] I will know how many times a user has asked to find that word. Now, when the user is done asking to have words looked up, the program will compute some simple information and produce a simple report on the HTML page (using document.write). Create a report that prints out each word in the array of words, shows its location in the array (was it element 0, 1, 2, etc) and also shows how many times it was looked up. Also compute the average number of times a word was looked up and report that… then you are done. One nice way to report this information is to generate an HTML

containing it. How can we compute this final average? Let’s look at a small example. loc dictionary hits 0 avuncular 2 1 spontaneous 0 2 sporadic 1 3 astrophysics 5 4 culmination 2 5 apoplectic 0 6 pecuniary 4 7 gaelic 0 8 elasticity 0 9 polymorphic 1 10 sparkling 1 To get the average number of lookups per word, we sum up the total lookups that are stored in “hits” and divide by the number of words. In this example, this would be (2+0+1+5+2+0+4+0+0+1+1)/ (16)/

So we had on average each word looked up 1.6 times.

Style

Remember to use good programming style in your program. Make sure the code is nicely indented to give good visual definition to the various blocks of code (loop bodies, if-then- else blocks, etc.). Declared your variables at the top, and give // comments stating their use. Create named constants (variables with initial values that you never change) rather than burying numbers in your code for things like loop limits, conversion factors, etc. Put a large comment (a block comment) at the top of the script explaining its general purpose. For this class, also make sure the first items in that block comment are your name, your ONYEN, and the class identification (COMP 110). Comments should appear