Difference between revisions of "CSC231 Homework 3"
(→Problem #2) |
(→Problem #3) |
||
Line 78: | Line 78: | ||
==Problem #3== | ==Problem #3== | ||
− | + | Assume that we write a C or C++ program that displays all the powers of 2 in this fashion: | |
+ | |||
+ | <code><pre> | ||
+ | |||
+ | unsigned int x = 1; | ||
+ | |||
+ | while ( x ) { | ||
+ | printf ( "%d\n", x ); | ||
+ | x = x * 2; | ||
+ | } | ||
+ | |||
+ | </pre></code> | ||
+ | |||
+ | * Is the loop infinite? Why? | ||
+ | |||
+ | * If it is not infinite, how many times does it go through, and how many terms does it print? Why? |
Revision as of 14:18, 24 September 2008
<meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> <meta name="description" content="Dominique Thiebaut's Web Page" /> <meta name="title" content="Dominique Thiebaut -- Computer Science" /> <meta name="abstract" content="Dominique Thiebaut's Computer Science Web pages" /> <meta name="author" content="thiebaut at cs.smith.edu" /> <meta name="distribution" content="Global" /> <meta name="revisit-after" content="10 days" /> <meta name="copyright" content="(c) D. Thiebaut 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,2008" /> <meta name="robots" content="FOLLOW,INDEX" />
Assignment #3
This assignment is due on Wednesday Oct. 1st, at 11:59 p.m. plus one minute.
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
//
// 27001
// -31534
//
// 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>
main() {
short int x = ?????;
short int y = ?????;
x = x+y;
printf( "%d\n", x );
x = x+y;
printf( "%d\n", x );
}
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.
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 usigned ints (32 bits)?
How many terms if we use long unsigned ints of 64 bits?
Explain carefully how you derive your answers. Prefered 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
Assume that we write a C or C++ program that displays all the powers of 2 in this fashion:
unsigned int x = 1;
while ( x ) {
printf ( "%d\n", x );
x = x * 2;
}
- Is the loop infinite? Why?
- If it is not infinite, how many times does it go through, and how many terms does it print? Why?