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

From dftwiki3
Jump to: navigation, search
(Solution)
 
Line 56: Line 56:
 
<br />
 
<br />
 
<br />
 
<br />
+
<onlydft>
 +
<br />
 
=Solution=
 
=Solution=
 
<br />
 
<br />
Line 75: Line 76:
 
* [[CSC231_Mystery_C_Program_Solution | Solution Program]]
 
* [[CSC231_Mystery_C_Program_Solution | Solution Program]]
 
<br />
 
<br />
 +
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />

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.


...