Question

In: Computer Science

1.) Write a function, base2tobase10 that has a single input, a vector containing 1,s and 0...

1.) Write a function, base2tobase10 that has a single input, a vector containing 1,s and 0 's representing a binary number. For example the binary number 01101011 would be represented by the vector [ 0, 1, 1, 0, 1, 0,1, 1]. Your function should return a single output,the base 10 equivalent of the base 2 number.

First, your function must check the input vector and detremine if any of the vector elemnets are not a 0 or a1. If they are not, print error message "Please enter only ones and 0's" using fprintf() and exit gracefully to the console prompt using "return"

Reall that in Base 10

mySum = 4+70+300+2000+10000

So , in base 2

MySum = 1+2+0+8+0+32+64+0

Test code DO NOT CHANGE ANY OF THIS CODE

score = 0;

% -------------TEST base2tobase10 ------------------

rng(123456);

myBin = [ 1 1 0 1 1];

try

myBase10 = base2tobase10(myBin);

catch

warning('Problem using function. Assigning a value of 0.');

myBase10 = 0

end

if myBase10 == bin2dec(num2str(myBin))

fprintf('first test of base2tobase10 passed')

else

fprintf('first test of base2tobase10 failed')

end

myBin = [1];

try

myBase10 = base2tobase10(myBin);

catch

warning('Problem using function. Assigning a value of 0.');

myBase10 = 0

end

if myBase10 == bin2dec(num2str(myBin))

fprintf('second test of base2tobase10 passed')

else

fprintf('second test of base2tobase10 failed')

end

  1. base2tobase10 .

insert your code in the box below

%insert your code here

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Function First

base2tobase10.m

function res = base2tobase10( v )
res = 0;
for k=length(v):-1:1
res = res + power(2,k-1)*v(k);
end
end

Main.m

clc
clear
rng(123456);

myBin = [ 1 1 0 1 1];

try
  
myBase10 = base2tobase10(myBin);
  
catch
  
warning('Problem using function. Assigning a value of 0.');
  
myBase10 = 0;
  
end

if myBase10 == bin2dec(num2str(myBin))
  
fprintf('first test of base2tobase10 passed')
  
else
  
fprintf('first test of base2tobase10 failed')
  
end

myBin = [1];
fprintf("\n");
try
  
myBase10 = base2tobase10(myBin);
  
catch
  
warning('Problem using function. Assigning a value of 0.');
  
myBase10 = 0;
  
end

if myBase10 == bin2dec(num2str(myBin))
  
fprintf('second test of base2tobase10 passed')
  
else
  
fprintf('second test of base2tobase10 failed')
  
end

output


Related Solutions

There is a package, `beepr`, with a single function, `beep()`. Write a function with an input,...
There is a package, `beepr`, with a single function, `beep()`. Write a function with an input, `x`, that will call `beep()` when `x > 0`. You may assume that the `beepr` package has been loaded. Call your function `xgt0()`. using R studio
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
Write a MATLAB function that accepts input vector x and returns the number of local maximums...
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
Write a JavaScript function to get the values of form containing the following input fields: fname...
Write a JavaScript function to get the values of form containing the following input fields: fname lname address city state
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
USE Coral Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is 6, the output is:011(6 in binary is 110; the algorithm outputs the bits in reverse).
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
In Java  Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order.Ex: If the input is:6the output is:0116 in binary is 110; the algorithm outputs the bits in reverse.
1. Suppose that output q is a function of a single input, labor (L). Describe the...
1. Suppose that output q is a function of a single input, labor (L). Describe the returns to scale associated with each of the following production functions: a. q = 3L. Answer: b. q = L3. Answer:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT