Difference between revisions of "CSC231 Homework 6 2015"
Line 23: | Line 23: | ||
// | // | ||
// g++ mystery.cpp | // g++ mystery.cpp | ||
− | // a.out | + | // ./a.out |
// | // | ||
// The output of the program is the following | // The output of the program is the following | ||
// | // | ||
− | // | + | // 2000000001 |
− | // - | + | // -1294967294 |
// | // | ||
// With what positive values were x and y initialized | // With what positive values were x and y initialized | ||
// at the beginning of the program. Explain why. | // at the beginning of the program. Explain why. | ||
− | // | + | // An int contains 32 bits and is coded in 2's complement. |
*/ | */ | ||
− | #include < | + | #include <iostream> |
− | + | using namespace std; | |
− | + | int main( int argc, char* argv[] ) { | |
− | + | ||
+ | int x = ?????; | ||
+ | int y = ?????; | ||
x = x+y; | x = x+y; | ||
− | + | cout << x << endl; | |
x = x+y; | x = x+y; | ||
− | + | cout << x << endl; | |
+ | |||
+ | return 0; | ||
} | } | ||
+ | |||
</source> | </source> | ||
Line 68: | Line 73: | ||
* How many terms if we use long unsigned ints of 64 bits? | * How many terms if we use long unsigned ints of 64 bits? | ||
− | Explain carefully how you derive your answers. | + | Explain carefully how you derive your answers. Preferred answers are answers that contain a program (in the language of your choice) demonstrating/illustrating in one way or another the answers to the various questions. |
<br /> | <br /> | ||
=Problem #3= | =Problem #3= | ||
Line 94: | Line 99: | ||
* What is the last number displayed by the loop? Why? Is it a valid power of 2? | * What is the last number displayed by the loop? Why? Is it a valid power of 2? | ||
<br /> | <br /> | ||
− | * Assume now that we are interested in printing powers of 3 | + | * Assume now that we are interested in printing powers of 3, so we replace the multiplication line by "x = x * 3;" Is the loop infinite? Why or why not? If not infinite, what is the last number printed by the program? |
<br /> | <br /> | ||
<br /> | <br /> |