Addition Solution

$30.00

Description

File Name:

H:\CPSC1150\Lab9\Addition.java

Purpose

To add 2 numbers in string (because it might be too long for the memory) and add commas to it

.

Input

Two numbers that have less than 40 digits

Output

The addition of the two numbers (number 3) and have all 3 numbers converted to having commas in the appropriate locations

Program Logic (Pseudocode)

Algorithm Addition

Start

(main method)

  1. Ask for one int number (store in string)
  2. Check the number and make sure it is has less than 40 digits
  3. Take the first number to the createarray method, when it returns, store as an array in int
  4. Ask for a second int number (store in string)
  5. Check the number and make sure it is has less than 40 digits
  6. Take the second number to the createarray method, when is returns, store as an array in int
  7. Send the two arrays two the adder method, when it returns, store as an array in int
  8. Takes all the numbers two the commas method and return each as a string.
  9. Take the 3 strings to the output method

(createarray method)

  1. Create an array with the length of the string given (in int)
  2. Create a for loop that sets each character in the string to the place in the array

  1. Array[number] = String(char.at)(number) – ‘0’ so it is not an ascii number

  2. Check to make sure the number is only between 0 to 9

  1. Return the array

(adder method)

  1. Initialize the variables
  1. Carry = 0
  2. Length = 0
  3. End = 0
  1. If num1>= num2
  1. Length = length of num1
  2. End = length of num2
  1. Else
  1. Length = length of num2
  2. End = length of num 1
  1. Create an array num3 in int of length [length + 1]
  2. Create a for loop where you start at 0 and go until it reaches end
  1. array[length-(loopnumber)] = num1[num1 length-(loop number)-1] + num2[num2 length – (loop number) – 1] + carry
  2. carry = 0
  3. If array [length-(loopnumber)] is greater then 10
  1. Array [length-(loopnumber)] = Array [length-(loopnumber)] – 10
  2. Carry = 1
  1. If num1>num2
  1. Array[length-end] = num1[length-end-1] + carry
  2. Create a for loop that makes the rest of the array equal the same as the num1
  1. If num2>num1
  1. Array[length-end] = num2[length-end-1] + carry
  2. Create a for loop that makes the rest of the array equal the same as the num2
  1. Return the array we created

(commas method)

  1. String = “”
  2. Create an if statement seeing if the first number in the array is 0
  1. Add a for loop starting at 1 (so it skips the first number) until the length of the array
  1. String= String + number[loop number]
        1. If (number length – loop number – 1)%3 equals 0
  1. If (Number length – loop number – 1 > 0)
  1. String = String + “,”
  1. else
  1. Add a for loop starting at 0 until the length of the array
  1. String= String + number[loop number]
        1. If (number length – loop number – 1)%3 equals 0
  1. If (Number length – loop number – 1 > 0)
  1. String = String + “,”
  1. Return the String

(output method)

  1. Show that the program is outputting
  2. Add a for loop if string 1 is less than string 3
  1. Create a space for however many digits short it is
  1. Display string 1 with a plus sign beside it
  2. Add a for loop if string 2 is less than string 3
  1. Create a space for however many digits short it is
  1. Display string 2
  2. Add a for loop for how long string 3 is
  1. Create a dash for the length of string 3
  1. Display string 3