Question: Question 2 : Write a program which will prompt for and read a string that represents an eight digit base - 4 number ( i

Question 2: Write a program which will prompt for and read a string that represents an eight digit base-4 number (i. e. only the digits 0,1,2,3 may appear in the number). The program will then echo the users input and print the base 4 number as a four digit hexadecimal number. The program will keep prompting for base-4 numbers until the user enters a blank line (i. e. the user hits only the enter key) at which time it will terminate.
To do this you will need to convert the string of characters representing the base 4 number into a binary number or any other way. The conversion will be done as follows: create an array of eight byte-word in the memory call POWERFOUR which contains powers of 4 corresponding to the 8 digits in the base-4 number (47,46,....40). Now process each of the characters in the string by first converting its ascii code into the integer value it represents, then multiplying it by the corresponding element in POWERFOUR, and then adding this product into the binary number. Once all the characters have been processed, the number can be printed as 4 digit hexadecimal number.
Here is a high level description of the programs operation:
1) prompt the user for a number
2) read the users input intoa buffer in memory
3) Check the length of the input. If the length is 1, then the user has entered a blank line and you should end the program. If the length is greater than 1, the user entered a number which is processed.
4) Convert the string into a binary number using the method above (or other method).
5) Print the binary number as a hexadecimal number
6) Return to step 1
You may assume there are no errors in the input, i. e. the user will always enter either an 8-digit base-4 number or a blank line.
The output for your program should look approximately like this:
Student Number : 1234566
Course: COSC2406
Instructor: Simon Xu
Assignment: 2
Question: 2
Enter an 8-digit base-4 number
Or enter a blank line to quit: 12301230
12301230(base 4)=6C6C (base 16)
Enter an 8-digit base-4 number
Or enter a blank line to quit: 32103210
32103210(base 4)= E4E4(base 16)
Enter an 8-digit base-4 number
Or enter a blank line to quit:
Program ended normally

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!