Difference between revisions of "CSC231 Homework 3 Solution"

From dftwiki3
Jump to: navigation, search
(New page: Answers provided by D. Thiebaut =Problem 1= Here is the original program: <code><pre> /* mystery.cpp // D. T. // mystery program // To compile and run this program: /...)
 
(Problem 2)
Line 42: Line 42:
 
=Problem 2=
 
=Problem 2=
  
I simply created a spreadsheet with several column, as shown [[CSC103_Fibonacci_storage.pdf | here]].
+
I simply created a spreadsheet with several column, as shown [[Media:CSC103_Fibonacci_storage.pdf | here]].
  
 
The column labeled "Bytes" for example, has a formula of the type '''=if( Bxx<=2^8, 1, 0 )''', which compares the Fibonacci term on the same row, in Column B, and if it is less than 2 to the 8th power, writes a 1 (good) in the column, else 0 (byte-storage too small).  A similar formula is used in the word column, but this time with 2^16, in the dword column, with 2^32, and in the qword (quad word) column, with 2^64.
 
The column labeled "Bytes" for example, has a formula of the type '''=if( Bxx<=2^8, 1, 0 )''', which compares the Fibonacci term on the same row, in Column B, and if it is less than 2 to the 8th power, writes a 1 (good) in the column, else 0 (byte-storage too small).  A similar formula is used in the word column, but this time with 2^16, in the dword column, with 2^32, and in the qword (quad word) column, with 2^64.

Revision as of 16:04, 6 October 2008

Answers provided by D. Thiebaut

Problem 1

Here is the original program:

/* mystery.cpp         
// D. T. 
// mystery program  
// To compile and run this program:        
//     
//     g++ mystery.cpp     
//     a.out           
// 
// The output of the program is the following
//
// 27001
// -31534
//
// With what positive values were x and y initialized
// at the beginning of the program.  Explain why.
// (a short int contains 16 bits)
*/

#include <stdio.h>

main() {

    short int x = 20000;
    short int y = 7001;


    x = x+y;
    printf( "%d\n", x );

    x = x+y;
    printf( "%d\n", x );
}

Problem 2

I simply created a spreadsheet with several column, as shown here.

The column labeled "Bytes" for example, has a formula of the type =if( Bxx<=2^8, 1, 0 ), which compares the Fibonacci term on the same row, in Column B, and if it is less than 2 to the 8th power, writes a 1 (good) in the column, else 0 (byte-storage too small). A similar formula is used in the word column, but this time with 2^16, in the dword column, with 2^32, and in the qword (quad word) column, with 2^64.

The byte storage is good only up to the 13th term (233).

The word storage is good only up to the 24th term (46,368).

The dword storage is good up to the 47th term (2,971,215,073)

The qword storage is good up to the 93rd term (1.22002 E+19)

Problem 3

if you start with 1 in a 32 bit register, you have in binary 0000 0000 0000 0000 0000 0000 0000 0001.

Multiplying it by 2 yields 0000 0000 0000 0000 0000 0000 0000 0010.

Again yields 0000 0000 0000 0000 0000 0000 0000 0100.

And again: 0000 0000 0000 0000 0000 0000 0000 1000.

After 31 multiplications we get: 1000 0000 0000 0000 0000 0000 0000 0000.

One more multiplication and the leading 1 disappears: 0000 0000 0000 0000 0000 0000 0000 0000

the all-0 value is equivalent to False in programming. Any non-zero value is True. The program will stop at this point. It will have printed all the 32 values of x where there is a 1 in a different bit position.

The last number displayed is 2^31.