Question

Create a program that converts an integer to the specified base. in Matlab ----The program should...

Create a program that converts an integer to the specified base. in Matlab

----The program should ask for 3 inputs. The number to convert. The base the number is in.

And the base to convert the number to.

----The program should accept a base that is in the range of 2 to 16 inclusive.

----Display the result to the user and ask if they want to exit or convert another number.

 Sub-goals:

o Do not display leading zero's in the result.

o Validate that the number entered is valid for the specified base

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

T = 1;
while T == 1

% prompt user for different things
% asking user to enter the number
prompt = 'Enter the number \n';
num = input(prompt);
% asking user to enter the source base
prompt = 'Source Base \n';
base = input(prompt);
% asking user to enter the target base
prompt = 'Target base \n';
target = input(prompt);

% conversion from base number to decimal number
% multiplying corresponding digit with its power to convert it to
% decimal
  
% 4 6 0 1
% * * * *
% 8^3 8^2 8^1 8^0
summation = 0;
i = 0;
while num > 0
% extracting last digit and multiplying with correponding power
summation = summation + mod(num,10)*( base^i) ;
% removing last digit by dividing number with 10;
num = fix(num/10);
i = i+1;
end

% conversion from decimal number to target number
y = [];
% converting decimal to target base by dividing the number untill it is
% greater then 0
% storing the number in array so to convert it to string
while summation > 0
val = mod(summation,target);
y = [val y];
summation = fix(summation/target);
end

% to remove leading zeros from vector if any
g = find(y == 0);
for i=1:length(g)
if g(i)==i
y(1) = [];
end
end

% to display it as string
y = string(y);
str = '';

% concatenating each digit
for i = 1:length(y)
str = strcat(str,y(i));
end

disp(str)
  
prompt = 'Want to exit, press any number other than 1 \n';
T = input(prompt);
end

OUTPUT

>> HomeworkLib Enter the number 34647 Source Base 345 Target base 678 136607370592 Want to exit, press any number other than 1 Enter the number 34465 Source Base Target base 11100100110101 Want to exit, press any number other than 1

Add a comment
Answer #2

CODE:


initiate = 'Y'; %initially user input taken as default

function [b] = base2base(num, base_in, base_out) %function to convert from one base to another
x = base2dec(num, base_in);
d = floor(log10(x) / log10(base_out));
y = zeros(1, d+1);
for i=0:d;
b(1+d-i) = mod(floor(x/(base_out^i)),base_out);
end
end

function [] = userPrompt(in) %function for taking user input
if in == 'Y' %if Y then take input otherwise exit
prompt1 = 'Enter integer to be converted: ';
prompt2 = 'Input base: ';
prompt3 = 'Output base: ';
num = input(prompt1, 's');
base_in = input(prompt2);
base_out = input(prompt3);
if base_in<2 || base_in>16 || base_out<2 || base_out>16
fprintf('base is not in range, Try again!\n');
userPrompt('Y');
else
disp(base2base(num, base_in, base_out)); %display the converted number
userPrompt(input('Want to convert another number? Y/N: ', 's')); %ask for another conversion
end
else
fprintf('Exiting!');
end
end

userPrompt(initiate);

OUTPUT:

Enter integer to be converted: 15 Input base: 10 Output base: 2 1 1 1 1 Want to convert another number? Y/N: Y Enter integer to be converted: 101011 Input base: 2 Output base: 8 5 3 Want to convert another number? Y/N: Y Enter integer to be converted: 65 Input base: 7 Output base: 12 3 11 Want to convert another number? Y/N: Y Enter integer to be converted: 15 Input base: 18 Output base: 5 base is not in range, Try again! Enter integer to be converted: T

Code screenshot in online Matlab Octave online:

Add a comment
Know the answer?
Add Answer to:
Create a program that converts an integer to the specified base. in Matlab ----The program should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Written in C++) Your assignment is to generate an HTML multiplication table with dimensions specified by...

    (Written in C++) Your assignment is to generate an HTML multiplication table with dimensions specified by the user. You will ask the user for the number of rows then number of columns, and generate an HTML table of the appropriate size. The top left cell should contain the result of 1 x 1, and the bottom right cell should contain the result of num_rows x num_cols. Each row and column may be an integer value between 1 and 12 inclusive...

  • Write a program that performs the following tasks: Display a friendly greeting to the user Prompt...

    Write a program that performs the following tasks: Display a friendly greeting to the user Prompt the user for the value to convert (a String) Accept that String Prompt the user for the value of the initial base (an integer) Accept that integer Prompt the user for the desired base of the output (an integer) Accept that integer If the String is not a legal expression of a number in the initial base, display an error message and exit the...

  • MATLAB help! Converting bases. You will be given an integer in base ten and you will...

    MATLAB help! Converting bases. You will be given an integer in base ten and you will convert it to the equivalent value in the specified base. For example, 105 when coverted to base 4 is 12214. You are to write a FUNCTION that takes two inputs: your base 10 integer, and the base you want to convert to. Your function should then return the array of characters corresponding to the digits of hte equivalent number in the new base.

  • I am writing C++ code, and I need help for this. Create a program that will...

    I am writing C++ code, and I need help for this. Create a program that will convert a decimal value into Roman numeral, and output the result. The program will ask for an integer input, assume all inputs are valid, and the program should not end unless the user tells you to. C++ code.

  • Write a MATLAB script named Converter.m and MATLAB two functions, each in its own script, named...

    Write a MATLAB script named Converter.m and MATLAB two functions, each in its own script, named D2BConverter.m and B2DConverter.m The Selection Output should be the following: Binary to Decimal & Decimal to Binary Converter Please Select: 1. Convert from Decimal To Binary 2. Convert from Binary To Decimal 3. Exit If user select option [1] then you ask the user to enter a decimal number (whole number) and display the equivalent binary, else if the user select option [2] then...

  • Flowchart and pseudocode for a program that takes a user input consisting of an integer number...

    Flowchart and pseudocode for a program that takes a user input consisting of an integer number between 0 and 99 and outputs its conversion as binary. For example, if a user enters 25, the output should be 11001. The program should also validate the input and continuously ask for a new input if the number is not within the appropriate range of values. Additional instructions: check that the user enters a number between 0 and 99.

  • Modular program mathlab: Write a program in Matlab that would continuously ask the user for an...

    Modular program mathlab: Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “You got it” If the user’s die is smaller, it should display, “Nice try” If the results are the...

  • Write a program in C++. You need everything everythihng given You will create a function that...

    Write a program in C++. You need everything everythihng given You will create a function that validates input. It should accept only non-negative integers. If the input is incorrect the function should throw an exception. The exception will not be caught in the function. You will create a function that reads in 2 integer values. One is a number that will be raised to the power of the second. So if 5 and 6 are entered the result would be...

  • read instructions carefully please matlab only Write a MATLAB function for a BMI calculator that receives the inputs for body weight (lbs)and height (ft) from the user's program, calculates th...

    read instructions carefully please matlab only Write a MATLAB function for a BMI calculator that receives the inputs for body weight (lbs)and height (ft) from the user's program, calculates the BMI using the equation below, displays the BMI category, and outputs the BMI value tothe user's program W (kg) h2 (m2) BMI Display Normal if 18.5 s BMI 25 Display Overweight if 25 s BMI <30 Display Obese if BMI 2 30 Else display underweight So you will create two...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT