Difference between revisions of "CSC231 Homework 4 2010"
(→Problem 2: C++ Programming) |
(→Problem 2: C++ Programming) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
---- | ---- | ||
<tanbox> | <tanbox> | ||
− | This assignment is due on Thursday, Oct 14, as it was released one day later than usual. It is due at 11:59 p.m. + 1 minute. | + | This assignment is due on Thursday, Oct 14, as it was released one day later than usual. It is due at 11:59 p.m. + 1 minute. You can work in pairs on this assignment, in which case submit only one answer to each problem, and make sure both names appear in the submitted file. |
</tanbox> | </tanbox> | ||
Line 23: | Line 23: | ||
* Use emacs to create a file that you will call hw4.cpp | * Use emacs to create a file that you will call hw4.cpp | ||
− | * Store the following C program in this file: | + | * Store the following C++ program in this file: |
+ | |||
+ | <br /> | ||
<source lang="cpp"> | <source lang="cpp"> | ||
Line 33: | Line 35: | ||
unsigned int x, lastx; | unsigned int x, lastx; | ||
+ | // print 4 blank lines | ||
cout << endl << endl << endl << endl; | cout << endl << endl << endl << endl; | ||
+ | // initialize x and show its first value | ||
x = 3; | x = 3; | ||
cout << "x = " << x << endl; | cout << "x = " << x << endl; | ||
− | // | + | // loop for-ever... |
while ( 1 ) { | while ( 1 ) { | ||
lastx = x; | lastx = x; | ||
x = x * 2; | x = x * 2; | ||
+ | // if the value of x is ever less than or equal to lastx, stop the loop | ||
if ( x <= lastx ) { | if ( x <= lastx ) { | ||
cout << "last x = " << lastx << endl; | cout << "last x = " << lastx << endl; |
Latest revision as of 08:33, 8 October 2010
--D. Thiebaut 23:00, 7 October 2010 (UTC)
This assignment is due on Thursday, Oct 14, as it was released one day later than usual. It is due at 11:59 p.m. + 1 minute. You can work in pairs on this assignment, in which case submit only one answer to each problem, and make sure both names appear in the submitted file.
Problem 1
Assume that we want to compute the Fibonacci series with an assembly language program.
- How many terms can we correctly display if we use unsigned bytes (8 bits) to store the Fibonacci terms?
- How many terms if we use unsigned 16-bit words?
- How many terms if we use usigned double-words of 32 bits?
- How many terms if we use 64 bit integers (2 double-words)?
Explain carefully how you derive your answers.
Problem 2: C++ Programming
- Use emacs to create a file that you will call hw4.cpp
- Store the following C++ program in this file:
#include <iostream>
using namespace std;
main() {
unsigned int x, lastx;
// print 4 blank lines
cout << endl << endl << endl << endl;
// initialize x and show its first value
x = 3;
cout << "x = " << x << endl;
// loop for-ever...
while ( 1 ) {
lastx = x;
x = x * 2;
// if the value of x is ever less than or equal to lastx, stop the loop
if ( x <= lastx ) {
cout << "last x = " << lastx << endl;
cout << "x = " << x << endl;
break;
}
}
cout << endl << endl << endl << endl;
}
- Compile the program as follows:
g++ hw4.cpp -o hw4
- Run the program as follows:
./hw4
- Question 1
- Explain the output you get from the program. Why does the loop stop?
- Question 2
- How many bits does C++ use to store integers (the variable x)?
- Question 3
- Remove the word unsigned in front of int, compile and run the program again. Explain its new output. Why does the loop stop? Do you find negative numbers in the output? Why?
- Question 4
- Change the words "unsigned int" and replace them by "char". Compile and run the program again. How many bits are used by C++ to store characters? Are characters assumed to be signed or not?
Problem #3
- Question 1
- Show the hexadecimal and binary representation of the following decimal numbers, assuming that the numbers are stored in bytes, and assuming that the system used is 2's complement.
0 10 16 127 128 -128 -127 -16 -10
- Question 2
- What is the 2's complement decimal equivalent of the following 16-bit numbers, shown here in hexadecimal?
FFFF 8000 7FFF 1111 1000
- Question 3
- Prove that in 2's complement, -1 is always represented in binary as all the bits set to 1.
Submission
- Submit 1 file with your all your answers to all three problems. Call the file hw4.txt and submit it as follows:
submit hw4 hw4.txt