Assignment 1

Part 1

A. (12 points) Unsigned integer representations.

1. (6 marks) Consider the following integers: (i) 3096; (ii) 13357.

(a) Suppose an integer is stored in two bytes (i.e., 16 bits). Find the bit patterns used to represent these integers.

(b) Write the bit patters that you found in question (a) in hexadecimal form.

2. (6 marks) Consider the following hexadecimal integers: (i) 4321; (ii) DCBA; (iii) FFFF.

(a) What are these integers in binary form? (b) What are these integers in the decimal form?

B. (12 points) Signed integer representations.

3. (6 marks) Suppose an integer is stored in two bytes. Find the bit patterns used to store the following integers: (Write your bit patterns in the hexadecimal notation.)

(i) 0; (ii) −3097; (iii) −13157.

(a) In excess-32767 notation. (32767 = 215 − 1) (b) In two's complement notation.

4. (6 marks) What are the integers represented by the following bit patterns?

(i) 6C2B; (ii) C2A6.

(a) The bit patterns are in excess-32767 notation. (b) The bit patterns are in two's complement notation.

C. (30 points) Integer arithmetic.

5. (12 marks) Add the following bit patterns (8 bits in hexadecimal form). Determine, for each case, whether the answer is incorrect due to overflow.

(i) 3C + 67; (ii) 4B + C3; (iii) C1 + AA; (iv) 92 + DC.

(a) Assume the bit patterns represent unsigned integers. (b) Assume the bit patterns represent signed integers in two's complement notation.

6. (12 marks) Subtract the following bit patterns (8 bits in hexadecimal form). Determine, for each case, whether the answer is incorrect due to overflow.

(i) 21 − 4D; (ii) AC − 5D; (iii) CC − 3B; (iv) 7C − A7.

(a) Assume the bit patterns represent unsigned integers. (b) Assume the bit patterns represent signed integers in two's complement notation.

7. (6 marks) Suppose the following bit patterns represent integers in two' complement notation by two bytes.

(i) 4EBC; (ii) CEA4.

Find the bit patterns that represent the same integer in two's complement notation by four bytes.

D. (24 points) Floating-point number representation.

Suppose a floating-point number is stored in one byte. The first bit is the sign bit, the next three bits represent the exponent in excess-3 notation, and the rest four bit stores the decimal part of the mantissa.

8. (4 marks) Find the largest number that this notation can represent. What is the bit pattern?

9. (4 marks) Find the smallest positive number (non-zero) that this notation can represent. What is the bit pattern?

10. (4 marks) List all numbers in the interval [−0.75, −0.875] that can be exactly represented by this notation.

11. (4 marks) List all numbers in the interval [8, 10] that can be exactly represented by this notation.

12. (4 marks) Use at most two sentences to express your observation, obtained from the answers to Questions 10 and 11, about the distribution of the numbers that be exactly expressed by this notation on the real axis.

13. (4 marks) Express the real number 3.6 in this notation. Find the round off error.

Part 2

Write a simple guessing game program in C. In this game the user selects a number between 1 and 100 (range 1-100). The program then tries to guess the number by asking the user questions about the number – “Is your number greater than, equals to or less than the number X?”, where X is a number that the program would like to guess.

Next is a scenario example of the game:

Program Instructions: Please select a number between 1 and 100. The user selects the number 9. Program’s question: Is your number greater than, equals to or less than the number 25? User’s answer: Less Program’s question: Is your number greater than, equals to or less than the number 10? User’s answer: Less Program’s question: Is your number greater than, equals to or less than the number 5? User’s answer: Greater Program’s question: Is your number greater than, equals to or less than the number 7? User’s answer: Greater Program’s question: Is your number greater than, equals to or less than the number 9? User’s answer: Equals Computer Program: It took 5 guesses to guess your number.

Your task is to write a program which attempts to guess the user’s number in as few questions as possible.

1. (2 points) Your program should print the instructions of the game to the user, and then start the game

2. (2 points) In each round of questions the program should prompt the user with the following question:

Is your number smaller than, equals to, or greater than X (enter <, =, > respectively)?

Note: a. X represents the guessed number b. The program should accept <, >, = as a response from the user. 3. (6 points) The program will then read the user’s answer and progresses accordingly. a. Guessing the correct number b. Correctly counting the number of questions c.

4. (3 points) If the program has successfully guessed the number then the program should prompt the user with the guessed number and the number of questions that it took to guess the number.

5. (6 points) If the user tries to cheat then the program should trap the cheating and then quit with the following message: You were cheating!! Goodbye.

6. (5 points ) Correct make file, documentation and ease of reading the code

Solution

Part 1

A

1)a) (i) 3096 in binary is 0000110000011000 (ii) 13357 in binary is 011010000101101 b) (i) C18, (ii) 342D

2) a) (i) 0100001100100001 (ii) 1101110010111010 (iii) 1111111111111111

b) (i) 17185 (ii) 56506 (iii) 65535

B

3) a) (i) 111111111111111 = 7FFF (ii) 111001111100110 = 73E6 (iii) 100110010011010 = 4C9A

b) (i) FFFF (ii) 3E7 (iii) C9B

4) a) (i) -5076 (ii) 17063b) (i) 001001111010101 (ii) 0011110101011001

С

5. i. a) 3C+67=00111100+01100111=10100011=A3 b) ii. a) 4B+C3=01001011+11000011=100001110=10E (overflow) b) iii. a) C1+AA=11000001+10101010=101101011=16B (overflow) b) iv. a) 92+DC=10010010+11011100=101101110=16E (overflow) b)

6. i. a) 21-4D=00100001-01001101 b) a) AC-5D=10101100-01011101=01001111=4F b) a) CC-3B=11001100-00111011=10010001=91 b) a) 7C-A7=01111100-10100111= b)

D

8) 1.5625*24 or 01111001 9) 1*2-3 or 00000000 11) 1*23 = 8

Part 2

#include <stdio.h>
 
int main(void){
int number;
int x=51;
int ready=0;
int won=0;
int guesses=0;
int cheating=0;
char answer;
 
int y=10;
 
printf("Hello and welcome to the best game of XXI century - GUESS THE NUMBER!\n");
printf("Please, enter the number between 1 and 100\n");
 
 
while (ready==0){
scanf ("%d", &number);
if ((number<1) || (number>100)) {printf("You've entered %d, which is not between 1 and 100. Please, try again.\n", number);}
else {ready=1;}
 
}
 
 
while (won==0)
{
getchar();
printf("Is your number greater than, equals to or less than the number %d\n", x);
scanf("%c", &answer);
 
if (answer=='=') {
	won=1; guesses++;
	if (number!=x){cheating=1;}
		}
 
 
if (answer=='<') {
	if ((number>x) || (number==x))  {cheating=1;}
	guesses=guesses+1;
	if ((x-y)<1) {x=0;}
	else {x=x-y;}
 
	}
 
if (answer=='>')  {
	if ((number<x) || (number==x)) {cheating=1;}	
	guesses++; 
	if ((x+y)>100) {x=100;}
	else {x=x+y;}
	}
 
if (guesses>3)  {y=5;}
if (guesses>6) {y=3;}
if (guesses>8) {y=1;}
}
 
if (cheating==0) {printf("It took %d guesses to guess your number\n", guesses);}
else {printf("You were cheating... I don't like it. Goodbye and never come back\n");}
 
}

 
sources/2007/linux_and_c/assignment_1.txt · Последние изменения: 2010/03/05 06:53 От freetonik
 
За исключением случаев, когда указано иное, содержимое этой вики предоставляется на условиях следующей лицензии:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki