CSC231 Homework 6 2015
--D. Thiebaut (talk) 10:46, 4 November 2015 (EST)
This homework is due on Wednesday 11/11/15, at 23:55 p.m.
Problem #1
Same problem as we did in class. Below is the mystery program. Its output is listed in the header of the 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 // // 2000000001 // -1294967294 // // With what positive values were x and y initialized // at the beginning of the program. Explain why. // An int contains 32 bits and is coded in 2's complement. */ #include <iostream> using namespace std; int main( int argc, char* argv[] ) { int x = ?????; int y = ?????; x = x+y; cout << x << endl; x = x+y; cout << x << endl; return 0; }
What two positive numbers are stored in x and y to create the output listed in the header?
Explain your answer.
Problem #2
Assume that we want to compute the Fibonacci series with a compiled program written in a language that supports signed and unsigned ints, in 8-, 16-, 32-, and 64-bit length.
- How many terms can we correctly display if we use unsigned chars (8 bits) to store the Fibonacci terms?
- How many terms if we use unsigned short ints (16 bits)?
- How many terms if we use unsigned ints (32 bits)?
- How many terms if we use long unsigned ints of 64 bits?
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.
Problem #3
class PowersOf2 { public static void main( String args[] ) { int x = 1; while ( x != 0 ) { System.out.println ( x ); x = x * 2; } } }
- Why is the loop not an infinite loop?
- How many times does it go through? Why?
- What is the last number displayed by the loop? Why? Is it a valid power of 2?
- 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?
Moodle Submission
- Submit your answers in 3 different files in the HW 6 PB 1, PB 2, and PB 3 sections on Moodle.
</onlydft>