CS 110 Take Home Exam

Due: 1:15pm, Monday, November 15


1. Write a program that allows me to order a sandwich. I like sandwiches, and I like to be able to choose exactly what is on my sandwich. But, occasionally, I'm in a hurry and I just need to choose a predetermined type of sandwich.

Your program should allow me to order a sandwich and designate an on-campus address to which the sandwich will be delivered. I should have the option of either selecting a pre-made sandwich or of building my own sandwich. After I have ordered my sandwich, your program should display a window that lists exactly what type of sandwich I ordered and tells me the price of the sandwich. Different sandwiches should have different prices!

Remember that sandwiches have various components. They have bread, they have vegetables, they have condiments, and they have meat. I'm not a vegetarian. I like meat on my sandwich.

Your program should use all of the form elements we've discussed - radio buttons, checkboxes, text boxes, and select boxes. Before you start programming, you'll want to think very carefully about what type of form elements you are going to use for each stage of the ordering process.


2. Write a program the gets from the user 5 base-power pairs of numbers. These numbers should be stored in two different arrays, one for the bases and one for the powers. Your program should then calculate the result each base-power pairing and store these results in a third array. You should use the power function that you've already written for a previous assignment to actually perform the calculation. At the end, your program should display the results.
3. Write a functional decomposition for a program that allows the user to play craps. Craps is played as follows:

1. The player rolls two 6-sided dice. 2. On the first roll

  • If the player rolls a 7 or an 11, they win.
  • If they roll a 2, 3, or 12, this is CRAPS and they lose.
  • If the first roll is anything else, the number rolled becomes the "target number.
    3. At this point, the person is trying to roll the target number again.
  • If they roll a 7 before they roll the target number, they lose.
  • If they roll the target number before they roll a seven, they win.
  • If they roll anything else, then they keep rolling.
    4. There are 15 errors in the following program. Find at least 10 of them:

    <html>
    <body>
    <head>
    <title>This page has errors</title>
    <script language="javascript">
    
    function square(x) {
    
    	y = x * x;
    }
    
    function output() {
    	var inArray = new Array(5);
    	var outArray;
    
    	with(myForm) 
    		inArray[0] = parseInt(num1.value, 10);
    		inArray[1] = parseInt(num2.value, 10);
    		inArray[2] = parseInt(num3.value, 10);
    		inArray[3] = parseInt(num4.value, 10);
    		inArray[4] = parseInt(num5.value, 10);
    
    	for(i=0; i<inArray.length; i++) {
    		outArray(i) = square inArray[i];
    
    	with(document) {
    		open();
    		write("<html><head><title>Squares rule!</title></head>");
    		write("<body>");
    		for(i=0; i<inArray.length; i++) {
    			write("The square of + inArray[i] + " is " outArray[i] + "<p>");
    		}
    		write("</body></html>");
    		close();
    	}
    </script>
    </head>
    <body>
    <form name="theForm">
    Number 1:<input type="text" size=3 name="num1"><br>
    Number 2:<input type="text" size=3 name="num2"><br>
    Number 3:<input type="text" size=3 name="num3"><br>
    Number 4:<input type="text" size=3 name="num4"><br>
    Number 5:<input type="text" size=3 name="num2"><br>
    <p>
    <input type="button" value="Squares!" onclick="output">
    </form>
    </body>
    </html>
    

    5. For each of the following two loops, create a chart that shows the values of i and answer each time through the loop.

    var i=1;
    var answer=0;
    while(i <= 10) {
    	answer = answer + (1/i);
    }
    
    
    ==========
    
    var answer=1;
    
    for(var i=0; i<10; i++) {
    	answer = answer * (1/i);
    }