Addition Solution

$30.00

Category:

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
  3. Array[number] = String(char.at)(number) – ‘0’ so it is not an ascii number
  4. Check to make sure the number is only between 0 to 9
  5. Return the array

 

(adder method)

  1. Initialize the variables
  2. Carry = 0
  3. Length = 0
  4. End = 0
  5. If num1>= num2
  6. Length = length of num1
  7. End = length of num2
  8. Else
  9. Length = length of num2
  10. End = length of num 1
  11. Create an array num3 in int of length [length + 1]
  12. Create a for loop where you start at 0 and go until it reaches end
  13. array[length-(loopnumber)] = num1[num1 length-(loop number)-1] + num2[num2 length – (loop number) – 1] + carry
  14. carry = 0
  15. If array [length-(loopnumber)] is greater then 10
  16. Array [length-(loopnumber)] = Array [length-(loopnumber)] – 10
  17. Carry = 1
  18. If num1>num2
  19. Array[length-end] = num1[length-end-1] + carry
  20. Create a for loop that makes the rest of the array equal the same as the num1
  21. If num2>num1
  22. Array[length-end] = num2[length-end-1] + carry
  23. Create a for loop that makes the rest of the array equal the same as the num2
  24. Return the array we created

 

(commas method)

  1. String = “”
  2. Create an if statement seeing if the first number in the array is 0
  3. Add a for loop starting at 1 (so it skips the first number) until the length of the array
  4. String= String + number[loop number]
    1. If (number length – loop number – 1)%3 equals 0
  5. If (Number length – loop number – 1 > 0)
  6. String = String + “,”
  7. else
  8. Add a for loop starting at 0 until the length of the array
  9. String= String + number[loop number]
    1. If (number length – loop number – 1)%3 equals 0
  10. If (Number length – loop number – 1 > 0)
  11. String = String + “,”
  12. 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
  3. Create a space for however many digits short it is
  4. Display string 1 with a plus sign beside it
  5. Add a for loop if string 2 is less than string 3
  6. Create a space for however many digits short it is
  7. Display string 2
  8. Add a for loop for how long string 3 is
  9. Create a dash for the length of string 3
  10. Display string 3

 


error: Content is protected !!