Question

MATLAB QUESTION

3. Write function called huge add that adds together two positive integers of any length specified as strings using decimal notation. The single output argument is the result and it is a string as well. The inputs and output must contain digits only; no commas, spaces or any other characters are allowed. If any of these assumptions are violated by the input, the function returns the number-1.

function Huge_Add = huge_add(number1,number2)

sum= number1 + number2;

sprintf(' %+5.2d',sum)

end

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

Here is the code for you:

function output = Huge_Add(string1, string2)

index1 = length(string1);

index2 = length(string2);

output = '';

carry = 0;

while(index1 ~= 0 && index2 ~= 0)

   [digit1, status] = str2num(string1(index1));

   if(status == 0)

   output = -1;

   return;

   end

   [digit2, status] = str2num(string2(index2));

   if(status == 0)

   output = -1;

   return;

   end

   sum = digit1 + digit2 + carry;

   if(sum > 9)

   carry = 1;

   output = strcat(num2str(sum - 10), output);

   else

   carry = 0;

   output = strcat(num2str(sum), output);

   end

   index1 = index1 - 1;

   index2 = index2 - 1;

end

while(index1 ~= 0)

[digit1, status] = str2num(string1(index1));

   if(status == 0)

   output = -1;

   return;

   end

   sum = digit1 + carry;

   if(sum > 9)

   carry = 1;

   output = strcat(num2str(sum - 10), output);

   else

   carry = 0;

   output = strcat(num2str(sum), output);

   end

   index1 = index1 - 1;

end

while(index2 ~= 0)

[digit2, status] = str2num(string2(index2));

   if(status == 0)

   output = -1;

   return;

   end

   sum = digit2 + carry;

   if(sum > 9)

   carry = 1;

   output = strcat(num2str(sum - 10), output);

   else

   carry = 0;

   output = strcat(num2str(sum), output);

   end

   index2 = index2 - 1;

end

if(carry == 1)

   output = strcat('1', output);

end

And the output screenshot is:

《 MATLAB Window Help b g < >しー_ー))) 100% E) Fri 2 Sep 11:58 ANANDA UMMAPUDI Q E Editor - Users/AnandaKumarThummapudi/Document

If you need any refinements, just get back to me.

Add a comment
Know the answer?
Add Answer to:
MATLAB QUESTION function Huge_Add = huge_add(number1,number2) sum= number1 + number2; sprintf(' %+5.2d',sum) end 3. Write function...
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
  • Question 1: Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating...

    Question 1: Write down an function named bitwisedFloatCompare(float number1, float number2) that tests whether a floating point number number1 is less than, equal to or greater than another floating point number number2, by simply comparing their floating point representations bitwise from left to right, stopping as soon as the first differing bit is encountered. The fact that this can be done easily is the main motivation for biased exponent notation. The function should return 1 if number1 > number2, return...

  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • In the first task, you will write a Java program that accepts a string of the...

    In the first task, you will write a Java program that accepts a string of the format specified next and calculate the answer based on the user input. The input string should be of the format dddxxdddxx*, where d represents a digit and x represents any character and asterisk at the end represents that the string can have any number of characters at the end. 1. Prompt the user to enter a string with the specific format (dddxxdddxx*) 2. Read...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • matlab 28 pts Question 8 Problem 4. Write the following function. Do NOT hard code using...

    matlab 28 pts Question 8 Problem 4. Write the following function. Do NOT hard code using any of the examples. Your function should work for all possible inputs as specified by the problem. Hard coding will result in significant point loss. (B) (28 Points) Function Name: birthday Party Input (2): (char) An Mx 11 array formatted with information about various birthdays (char) An MxN array of names Output (l): (char) A string containing information about the best birthday Function Description:...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

  • Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If...

    Hello all, I have a c++/unix (bash) question. I am struggling on starting this assignment. If you could start the assignment and tell me how to do the rest it would be greatly appreciated! (Quick thumbs up answer response if thorough and correct) Maintain automobile records in a database Write a shell script to create, view and modify a simple database that contains automobile records. The shell script has to be done in Bourne shell syntax (bash as a matter...

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
ADVERTISEMENT