Difference between revisions of "CSC231 Final Exam 2015"

From dftwiki3
Jump to: navigation, search
Line 53: Line 53:
 
You may assume that the stack is at most 6 MBytes in length.
 
You may assume that the stack is at most 6 MBytes in length.
  
==Part 2 (1 point)==
+
Submit a pdf with your answers in the Final PB 1 Section on Moodle.  Make sure you write your name at the top of the file!
Write the Tower of Hanoi program in assembly.  Store your program in a file called '''hanoi.asm'''.
+
<br />
 +
=Problem #2=
 +
<br />
 +
Python can handle very larger integer numbers, much larger than the integers Java can handle.
  
Your program should display the number of disks ''N'' used, and then the series of moves that need to be taken to move all the disks from one peg to the other.   Here is an example:
+
With real numbers, however, Python sometimes behaves in unexpected ways. To see an example of this, get a copy of the following Python program: [[CSC231 sum_of_floats.py | sum_of_floats.py]], and study it. It creates a list of 10 random floating-point numbers, each one the result of picking a random real number between 0 and 1.0, which is then multiplied by 10 raised to some other random integer in the range -40 to +40.
  
<code><pre>
+
The program then computes the sum of the ten numbers as stored in the list. That's sum1. Next it sorts the list in increasing order and computes the sum again, this time saving it into sum 2. Finally, it reverses the list so that it is sorted in decreasing order, and computes the sum of the numbers which it stores in sum3. It then compares sum1, sum2, and sum3, and displays the list and the sums if these are not equal.  
Towers of Hanoi game
 
Number of disks = 4
 
move disk from A to C
 
move disk from A to B
 
move disk from C to B
 
move disk from A to C
 
move disk from B to A
 
move disk from B to C
 
move disk from A to C
 
move disk from A to B
 
move disk from C to B
 
move disk from C to A
 
move disk from B to A
 
move disk from C to B
 
move disk from A to C
 
move disk from A to B
 
move disk from C to B
 
 
 
</pre></code>
 
 
 
Make the program use a global variable that defines how many disks are used:
 
 
 
<code><pre>
 
          section .data
 
N        dd      4
 
</pre></code>
 
  
Use 4 for N when you submit your program.  Of course your program should work for other values of N!  (Make sure that your program degrades gracefully, i.e. it shouldn't crash if N is set to 1, to 0, or to a negative number!
+
Before answering the questions below, you may want to read this [https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BinMath/addFloat.html interesting page] explaining how floating-point numbers are added inside an FPU.
 
 
Store your answers to Part 1 in the header of the program and submit it as follows:
 
 
 
              submit exam hanoi.asm
 
 
 
=Problem #2 (2 points)=
 
 
 
We saw that Python seems to be more "clever" about floating point numbers than other languages.
 
 
 
Sometimes, however, it seems to behave in unexpected ways. To see an example of this, get the following Python program: [[CSC231 sum_of_floats.py | sum_of_floats.py]], and study it. It creates a list of 10 random floating-point numbers, each one the result of picking a random real number between 0 and 1.0, which is then multiplied by 10 raised to some other random integer in the range -40 to +40.
 
 
 
The program then computes the sum of the ten numbers as stored in the list. That's sum1. Next it sorts the list in increasing order and computes the sum again, this time saving it into sum 2. Finally, it reverses the list so that it is sorted in decreasing order, and computes the sum of the numbers which it stores in sum3. It then compares sum1, sum2, and sum3, and displays the list and the sums if these are not equal.  
 
  
 
'''Question 1''': Explain why the 3 sums are usually the same, but ''sometimes'' not equal.  Base your explication on the 32-bit IEEE Floating-Point format which we saw in class.
 
'''Question 1''': Explain why the 3 sums are usually the same, but ''sometimes'' not equal.  Base your explication on the 32-bit IEEE Floating-Point format which we saw in class.
Line 104: Line 69:
 
'''Question 2''': What recommendations would you make when one has to compute the sum of several floating-point numbers?
 
'''Question 2''': What recommendations would you make when one has to compute the sum of several floating-point numbers?
  
'''Question 3''': What test could you use when you are given a list of numbers to figure out  whether the sum ''might'' not be correct when the addition is performed?
+
'''Question 3''': What test could you perform (in any language of your choice) when you are given a list of numbers to figure out  whether the sum ''might'' not be correct when the addition is performed?
  
'''Question 4''': Do we have to be worried about incurring the same problem when computing the product of a series of real numbers?
+
'''Question 4''': Do we have to be worried about incurring the same problem when computing the product of a series of real numbers?  Why?
  
 
Store your answers in a text file (not a Word doc file, please!) called '''sumfloat.txt''' and submit it as follows:
 
Store your answers in a text file (not a Word doc file, please!) called '''sumfloat.txt''' and submit it as follows:

Revision as of 09:17, 12 December 2015


...