Introduction to Computer Science - The Full Program Simulating | CMSI 182, Assignments of Computer Science

Material Type: Assignment; Class: Intro Computer Science; Subject: Computer Science; University: Loyola Marymount University; Term: Fall 2007;

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-5av
koofers-user-5av 🇺🇸

4

(1)

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSI 182
IN T R O D U C T I O N T O CO M P U T E R SC I E N C E
Fall 2007
Assignment 1115
This is the payoff — after slogging through how we think about algorithms, let’s finally feed them into a
computer, so that they can perform these computations for us.
Not for Submission
If you haven’t done so already, read Chapter 5 in
the Brookshear book.
For Submission
Convert the pseudocode algorithms from last week
into working JavaScript programs. The templates
given below are intended for use with the
JavaScript scratch page that can be found on the
course Web site, so that you can enter the values
you want in the input fields then click Run to see
the result:
1. The full program for converting any number
from 0 to 255 into 8-bit binary would look like
this; fill in the convertTo8Bit function:
// Shortcuts to the input fields.
var form = document.getElementById(“scratch”);
var input1 = form.input1;
var input2 = form.input2;
function convertTo8Bit(number) {
// Fill this out.
}
var answer = convertTo8Bit(input1.value);
alert(answer);
As you may be able to infer from the code, you
can provide the number to convert by typing it
into Input 1 on the Web page. If everything
works, you should get an alert box that con-
tains the 8-bit representation (or an error mes-
sage if the value in Input 1 was out of range).
2. The full program for “simulating” the three-
door game would look like this; fill in the simu-
lateDoorGame function:
// Shortcuts to the input fields.
var form = document.getElementById(“scratch”);
var input1 = form.input1;
var input2 = form.input2;
function simulateDoorGame(trialCount,
switchDoors) {
// Fill this out.
}
var answer = simulateDoorGame(input1.value,
input2.value);
alert(answer);
You can provide the number of “game
rounds” to play in Input 1. To indicate that you
would like to switch doors, type “true” into
Input 2, or “false” otherwise. If everything
works, you should get an alert box that con-
tains the number of times that the “player”
chose the winning door.
Submit your programs in some electronic form, so
that I can try them out myself.
Extra Credit
You will receive extra credit if you also submit
JavaScript versions of the following algorithms,
based on the pseudocode given in Tuesday’s pseu-
docode examples handout:
makeChange(amount, denominationList) The ver-
sion that can handle any system of coin denomi-
nations, expressed in denominationList.
russianPeasantMultiply(factor1, factor2) The ver-
sion on the right, which adds up the final product
right away.

Partial preview of the text

Download Introduction to Computer Science - The Full Program Simulating | CMSI 182 and more Assignments Computer Science in PDF only on Docsity!

CMSI 182

I N T R O D U C T I O N T O C O M P U T E R S C I E N C E

Fall 2007

Assignment 1115

This is the payoff — after slogging through how we think about algorithms, let’s finally feed them into a computer, so that they can perform these computations for us.

Not for Submission

If you haven’t done so already, read Chapter 5 in the Brookshear book.

For Submission

Convert the pseudocode algorithms from last week into working JavaScript programs. The templates given below are intended for use with the JavaScript scratch page that can be found on the course Web site, so that you can enter the values you want in the input fields then click Run to see the result:

  1. The full program for converting any number from 0 to 255 into 8-bit binary would look like this; fill in the convertTo8Bit function: // Shortcuts to the input fields. var form = document.getElementById(“scratch”); var input1 = form.input1; var input2 = form.input2; function convertTo8Bit(number) { // Fill this out. } var answer = convertTo8Bit(input1.value); alert(answer); As you may be able to infer from the code, you can provide the number to convert by typing it into Input 1 on the Web page. If everything works, you should get an alert box that con- tains the 8-bit representation (or an error mes- sage if the value in Input 1 was out of range). 2. The full program for “simulating” the three- door game would look like this; fill in the simu- lateDoorGame function: // Shortcuts to the input fields. var form = document.getElementById(“scratch”); var input1 = form.input1; var input2 = form.input2; function simulateDoorGame(trialCount, switchDoors) { // Fill this out. } var answer = simulateDoorGame(input1.value, input2.value); alert(answer); You can provide the number of “game rounds” to play in Input 1. To indicate that you would like to switch doors, type “true” into Input 2 , or “false” otherwise. If everything works, you should get an alert box that con- tains the number of times that the “player” chose the winning door. Submit your programs in some electronic form, so that I can try them out myself.

Extra Credit

You will receive extra credit if you also submit JavaScript versions of the following algorithms, based on the pseudocode given in Tuesday’s pseu- docode examples handout:

- makeChange(amount, denominationList)^ — The ver- sion that can handle any system of coin denomi- nations, expressed in denominationList. - russianPeasantMultiply(factor1, factor2)^ — The ver- sion on the right, which adds up the final product right away.