Difference between revisions of "CSC231 Homework 3"

From dftwiki3
Jump to: navigation, search
(New page: <meta name="keywords" content="computer science, assembly language, pentium, exercise, machine language, intel" /> <meta name="description" content="Dominique Thiebaut's Web Page" /> <meta...)
 
(Problem #3)
 
(11 intermediate revisions by the same user not shown)
Line 15: Line 15:
  
 
=Assignment #3=
 
=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.
 +
 +
<code><pre>
 +
 +
/* 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 );
 +
}
 +
 +
</pre></code>
 +
 +
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 [http://en.wikipedia.org/wiki/Fibonacci_number 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:
 +
 +
<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?  What is the last number displayed?
 +
 +
 +
Store the answers to all 3 problems in a file called '''hw3.txt''' and submit it as follows:
 +
 +
  submit hw3 hw3.txt

Latest revision as of 21:29, 29 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" />

Back to CSC231 Main Page


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? What is the last number displayed?


Store the answers to all 3 problems in a file called hw3.txt and submit it as follows:

  submit hw3 hw3.txt