Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description of Part (a) In MAL an integer is represented using 32 bits. Assume t

ID: 3831954 • Letter: D

Question

Description of Part (a)

In MAL an integer is represented using 32 bits. Assume that the bits are numbered right to left from 0 to 31. Thus, the rightmost bit is numbered 0 and the leftmost bit is numbered 31. We say that the bits 0 through 15 form the right half and the bits 16 through 31 form the left half of the integer.

You are required to write a MAL program that prompts a user for a positive decimal integer[1], reads the integer typed by the user and outputs the following values:

1.The total number of 1's in the right half of the binary representation of the integer.

2.The total number of 0's in the left half of the binary representation of the integer.

3.The highest power of 2 that evenly divides the integer[2].

4.The value of the largest digit in the decimal representation of the integer.

Example: Suppose the user types the decimal integer 1536. The 32-bit binary representation of 1536 is as follows:

0000 0000 0000 0000 0000 0110 0000 0000

For this example, the required answers are as follows:

1.The number of 1's in the right half of the binary representation of the given integer = 2.

2.The number of 0's in the left half of the binary representation of the given integer = 16.

3.The largest power of 2 that evenly divides the given integer = 9[3].

[1] A positive integer is one that is strictly greater than zero.

[2] An integer x evenly divides an integer y if the remainder when y is divided by x is zero.

[3] The largest power of 2 that evenly divides 1536 is 9^3
because 1536 = 3 * 512 = 3 * 2^9. This highest power is also the number of 0's at the end of the binary representation of 1536.

1.The value of the largest digit in the decimal representation of the given integer = 6.

Program outline:

The outline for your program for Part (a) must be the following.

Prompt the user for a positive integer.

Read the integer.

Compute the four quantities mentioned above and print the answers.

Stop.

Programming Suggestions:

1)MAL program must be in a file named .

2)Each time your program for Part (a) is executed, it should handle integer.

3)You may assume that the value typed by the user is a positive decimal integer. Thus, there is no need to do any error checking in this part.

4)There is no need to convert the integer to binary; when the integer is read in (using ), it is already in binary form.

5)Use bitwise operations to count the number of 1’s (0’s) in the right (left) half.

6)To find the highest power of 2 that divides the integer, count the number of 0’s at the end of the binary representation or use successive divisions by 2.

7)To extract the decimal digits and compute the largest digit, use successive divisions by 10.

Explanation / Answer

For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following. 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the newline character. 3. A word is any sequence of characters that does not contain a whitespace character. In this part, you are required to write a MAL program that prompts the user for a line of text and reads the line typed by the user. If the line contains just white space characters your program should simply output the message “Line contains only white space characters.” and stop. Otherwise, your program should compute and output the following. (a) The number of non-whitespace characters in the line. (b) The number of words in the line. (c) The maximum length of a word in the line. (d) The minimum length of a word in the line. (e) The word of maximum length in the line. (f) The word of minimum length in the line.

Suppose the line typed by the user is the following: It was the best of times and it was the worst of times. The answers for the above line are: No. of non-whitespace characters: 43 No. of words: 13 Maximum length of a word: 6 Minimum length of a word: 2 Word of maximum length: times. Word of minimum length: It Note that the word of maximum length (6) is "times." (without the quotes), which includes the punctuation mark at the end