Write a program in matlab that allows numbers to be entered in decimal (base 10)
ID: 3695492 • Letter: W
Question
Write a program in matlab that allows numbers to be entered in decimal (base 10), hexadecimal (base 16), or binary (base 2), and then prints out the number again in all three bases.
To distinguish between different bases we'll say that hexadecimal numbers should always be preceded by a "$" and binary numbers by a "%". Other numbers are assumed to be decimal.
When the program is run it should repeatedly allow a number to be entered and then print it out in all three bases until "q" is typed in. Here's an example of how your program should operate:
Enter #: 16
Decimal: 16
Hexadecimal: 10
Binary: 10000
Enter #: %1111
Decimal: 15
Hexadecimal: F
Binary: 1111
Enter #: $6D
Decimal: 109
Hexadecimal: 6D
Binary: 1101101
Enter #: q
**Test your program for each of the following numbers: 16, %1111, $6D.
Approach
A good way to structure this program is to read in each "number" as a string of characters, look at the first character to determine what kind of number it is (hex “$”, binary “%”, decimal “ ”), and then call an appropriate function to convert the string into a hex, binary, or decimal number Then, print those numbers out next to each of their corresponding bases.
You will have to use these two functions and their variations to convert between bases:
dec2bin(integer input); % e.g. “4” “0100”
hex2dec(string input); % e.g. “1D” “14”
Also, use one or more of the following functions to convert from a string to a number and vice versa:
str2num(string input);
num2str(numeric input);
Some tips:
Remember to use the Top-Down Design Process.
Before attempting to write the program in MATLAB, write down the algorithm that the program will follow. This will help you to understand the execution flow of the program as well as make it easier when you are trying type the syntax. Remember, an algorithm is a sequence of precise instructions which leads to a solution.
When asking for an input that will contain more than just numbers, be sure to define that input as a string.
This project seems tougher at first than it actually is. Once you get one of the if/elseif statements working, the others are just simple variations. What type of loop will you use to execute code repeatedly until some condition is no longer true?
Explanation / Answer
A good way to structure this program is to read in each "number" as a string of characters, look
at the first character to determine what kind of number it is (hex “$”, binary “%”, decimal “ ”), and
hen call an appropriate function to convert the string into a hex, binary, or decimal number
Then, print those numbers out next to each of their corresponding bases.
You will have to use these two functions and their variations to convert between bases:
dec2bin(integer input);
hex2dec(string input);
% e.g. “4” “0100”
% e.g. “1D” “14”
Also, use one or more of the following functions to convert from a string to a number and vice
versa:
str2num(string input);
num2str(numeric input);
Some tips:
Remember to use the Top-Down Design Process.
Before attempting to write the program in MATLAB, write down the algorithm that the
program will follow. This will help you to understand the execution flow of the program as
well as make it easier when you are trying type the syntax. Remember, an algorithm is a
sequence of precise instructions which leads to a solution
The if statement:
Nested if startements:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.