Difference between revisions of "CSC231 Mystery C Program with signed numbers"

From dftwiki3
Jump to: navigation, search
(Mystery Program)
 
(8 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
=Mystery Program=
 
=Mystery Program=
  
Figure out what the two variables x and y are initialized with when the program starts.
+
* The program below is incomplete.
+
* The two values that were used to initialize x and y have been removed.
 +
* We do have the output of the program, though, corresponding to the original x and y values.
 +
<br />
 +
;Question 1
 +
: Figure out the value of x and y in the original program.
 +
<br />
 +
;Question 2
 +
: Find a way to fix the program so that it outputs the correct information
 +
<br />
 
You may use this [http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html converter] to help you out...
 
You may use this [http://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html converter] to help you out...
 
+
<br />
<code><pre>
+
<source lang="cpp">
 
/* mystery.cpp
 
/* mystery.cpp
 
// D. T.
 
// D. T.
Line 28: Line 36:
 
#include <stdio.h>
 
#include <stdio.h>
  
main() {
+
int main() {
  
 
     short int x = ???? ;
 
     short int x = ???? ;
Line 38: Line 46:
 
     x = x+y;
 
     x = x+y;
 
     printf( "%d\n", x );
 
     printf( "%d\n", x );
 +
 +
    return 0;
 
}
 
}
  
</pre></code>
+
</source>
  
 
<br />
 
<br />
For reference, 2<sup>15</sup> = 32768.
+
For reference, 2<sup>15</sup> = 32768, and  2<sup>16</sup> = 65536.
 +
<br />
 +
<br />
 +
<onlydft>
 
<br />
 
<br />
 +
=Solution=
 
<br />
 
<br />
 +
;Answer 1
 +
* First we need to find the unsigned version of -32756, because that is the value we expected, but this value is too large to be represented in 2's complement.
 +
* The difference between a negative 16-bit number in 2's complement, and its unsigned equivalent is 2<sup>16</sup>, or 65536.
 +
* So, -32756 which is printed, has an unsigned value of -32756 + 65536 = 32780. 
 +
* So, x+y  is 32740 and x+y+y = 32780
 +
* Therefore y = 40
 +
* Therefore x = 32700
 +
* Plug these numbers in the program and verify that we get, indeed, the two numbers listed in the comment section of the program.
 
<br />
 
<br />
 +
;Answer 2
 +
* We could switch from shorts to ints
 +
* We could use unsigned shorts
 +
 
<br />
 
<br />
 +
* [[CSC231_Mystery_C_Program_Solution | Solution Program]]
 +
<br />
 +
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />
Line 64: Line 93:
 
<br />
 
<br />
 
<br />
 
<br />
<onlydft>
+
[[CSC231_Mystery_C_Program_Solution | Solution]]!
+
 
</onlydft>
+
<br />
<br />
 
 
<br />  
 
<br />  
 
<br />
 
<br />
 
[[Category:CSC231]][[Category:C++]]
 
[[Category:CSC231]][[Category:C++]]

Latest revision as of 08:42, 4 November 2015


Mystery Program

  • The program below is incomplete.
  • The two values that were used to initialize x and y have been removed.
  • We do have the output of the program, though, corresponding to the original x and y values.


Question 1
Figure out the value of x and y in the original program.


Question 2
Find a way to fix the program so that it outputs the correct information


You may use this converter to help you out...

/* 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
//
// 32740
// -32756
//
// 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>

int main() {

    short int x = ???? ;
    short int y = ???? ;

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

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

    return 0;
}


For reference, 215 = 32768, and 216 = 65536.


...