COMS 1004 homework 5

docx

School

Columbia University *

*We aren’t endorsed by this school

Course

1004

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

9

Uploaded by ChefPenguin3583

Report
1) Pseudocode: (Index starts at 1 instead of 0 since this is pseudocode) Take an array named a with a size of n RotateArrayOne(a, n) temp = a[1] //store the first element in temp variable for i = 2 to n a[i-1] = a[i] //rotate each element one position to the left a[n] = temp //move first element to last position
2) In the method above, the random numbers are generated to array called numbers. However, the line values = numbers only copies the local reference (address) of the array numbers to the local reference of the array values. But this does not affect the numbers inside the array values that were passed to the method as a parameter (the elements of number array won’t be passed into the values array). Therefore, the main() method and makeCombination() method will have two different addresses for values array. The array number thus is a waste of memory as we can directly generate numbers and make changes to the values arra as shown below: public static void makeCombination(int[] values, int n) { Random generator = new Random(); for(int i = 0; i < values.length; i++) { values[i] = generator.nextInt(n); } }
3) (6 points) Assume that the following 10-bit numbers represent signed integers using sign/magnitude notation. The sign is the leftmost bit and the remaining 9 bits represent the magnitude. What is the decimal value of each? ■ 0110011000 ■ 1000000001 ■ 1000000000 0110011000 in decimal: The leftmost bit 0 is the sign bit -> positive integer The remaining 9 bits -> 110011000 0 2 0 + 0 2 1 + 0 2 2 + 1 2 3 + 1 2 4 + 0 2 5 + 0 2 6 + 1 2 7 + 1 2 8 ¿ 0 + 0 + 0 + 8 + 16 + 0 + 0 + 128 + 256 ¿ 408 1000000001 in decimal: The leftmost bit 1 is the sign bit -> negative integer The remaining 9 bits -> 000000001 1 2 0 + 0 2 1 + 0 2 2 + 0 2 3 + 0 2 4 + 0 2 5 + 0 2 6 + 0 2 7 + 0 2 8 ¿ 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 /add the negative sign ¿ 1 1000000000 in decimal: The leftmost bit 1 is the sign bit -> negative integer The remaining 9 bits -> 000000000 0 2 0 + 0 2 1 + 0 2 2 + 0 2 3 + 0 2 4 + 0 2 5 + 0 2 6 + 0 2 7 + 0 2 8 ¿ 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 /0 does not have a minus sign, but it’s included with the negative numbers in signed integer representation, so the decimal value is ¿ 0
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4) (9 points) Assume that our computer stores decimal numbers using 16 bits - 10 bits for a sign/magnitude mantissa and 6 bits for a sign/magnitude base-2 exponent. (The way we showed in class.) Show the internal representation of the following decimal quantities. ○ 7.5 ○ -20.25 ○ -.0625 7.5 = 111.1 = 0.1111 x 2 3 Binary representation: 111.1 Sign of the number: 0 (Positive) Mantissa: 1111000000 Sign of the exponent: 0 (Positive) Exponent: 000011 Sign Mantissa Sign Exponent 0 111100000 0 00011 -20.25 = -10100.01 = -0.1010001 x 2 5 Binary representation: -10100.01 Sign of the number: 1 (Negative) Mantissa: 101000100 Sign of the exponent: 0 (Positive) Exponent: 00101 Sign Mantissa Sign Exponent 1 101000100 0 00101 -0.0625 = -0.0001 = -0.1 x 2 -3 Binary representation: -0.0001 Sign of the number: 1 (Negative) Mantissa: 100000000 Sign of the exponent: 1 (Negative) Exponent: 00011 Sign Mantissa Sign Exponent 1 100000000 1 00011
5) (9 points) Convert the following three base 10 numbers into 32- bit two's complement binary numbers: 1. 512 2. -1023 3. -4,000,000
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
6) (9 points) Convert the following three two's complement binary numbers into decimal numbers: ○ 11111111111111111111111000101100 ○ 11111111111111111111111111111111 ○ 01111111111111111111111111111111
7) (4 points) Using the ASCII table in the book (Appendix A), show the internal binary representation for the following character strings. ○ AbC ○ Joey ○ $23.00 ○ (a-b) Characte r string Each Character ASCII code Represented in binary AbC A, b, C 659867 010000010110001001000011 Joey J, o, e, y 74111101121 0100101001101111011001010111100 1 $23.00 $, 2, 3, ., 0, 0 365051464848 0010010000110010001100110010111 00011000000110000 (a-b) (, a, -, b, ) 4097459841 0010100001100001011011010010111 00110001000101001
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help