Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming and Recursion: Write a program that adds Big numbers that are stored in arrays. For example the big number 13563906371945834 could be stored in

Programming and Recursion:

Write a program that adds Big numbers that are stored in arrays. For example the big number 13563906371945834 could be stored in an int array like this:

int[] num1 = {1,3,5,6,3,9,0,6,3,7,1,9,4,5,8,3,4};

Suppose you have another Big number:

int[] num2 = {9,3,5,6,1,2,0,5,6,0,3,4,6,7,1,9,4,2,8,5,6,3};

Your code should add these two numbers by adding individual digits. Assume the least significant digit is at the right of the array, this would be like adding:

0000013563906371945834 + 9356120560346719428563 --------------------------------

Storing the leading 0s makes the problem easier so the first array would be declared:

int[] num1 = {0,0,0,0,0,1,3,5,6,3,9,0,6,3,7,1,9,4,5,8,3,4};

The main complexity here is doing the carries when the sum of individual digits > 10. What do you do if a carry causes the column to the left to be > 10. To get you started, I will provide you with starter code:

public class SumBigNumbers { public static void main(String[] args) { int realSum = 7983428 + 4086653; int[] topNums = {0,0,0,7,9,8,3,4,2,8}; int[] botNums = {0,0,0,4,0,8,6,6,5,3}; int[] sumDigits = new int[10]; int digitSum;

// Your Code goes here to add the digits

System.out.println(Arrays.toString(sumDigits)); // verify your answer System.out.println(realSum); } // Recursive carry method goes here }

Your task is to fill in the actual adding algorithm in the code above.

In addition, provide a recursive solution to the carry problem. In this case, write a carry method that when called carries a 1 to the column to the left of where the carry is detected. Then it should call carry recursively to see if a carry is needed in the next column to the left. In this way the carries ripple across the array. The base case is when no more carries are needed. The method signature might look like this:

private static void carry(int[] topNums, int carryPos) {

Turn in your completed code and the console output.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Semantics In Databases Second International Workshop Dagstuhl Castle Germany January 2001 Revised Papers Lncs 2582

Authors: Leopoldo Bertossi ,Gyula O.H. Katona ,Klaus-Dieter Schewe ,Bernhard Thalheim

2003rd Edition

3540009574, 978-3540009573

More Books

Students also viewed these Databases questions

Question

1.what is the significance of Taxonomy ?

Answered: 1 week ago

Question

What are the advantages and disadvantages of leasing ?

Answered: 1 week ago

Question

Name is needed for identifying organisms ?

Answered: 1 week ago