{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

A3_2010_sol

A3_2010_sol - Assignment 3 Solution Q1 IIT1120 Winter 2010...

This preview shows pages 1–3. Sign up to view the full content.

Assignment 3 Solution Q1 // IIT1120 Winter 2010, Assignment 3, Question 1 // Name: Diana Inkpen, #7654321 // This program implements the sum of two arrays that store big integers import java.io.* ; class A3Q1 { public static void main (String[] args) { // DECLARE VARIABLES/DATA DICTIONARY int [] b1; int [] b2; int [] b; int i; // PRINT OUT IDENTIFICATION INFORMATION System.out.println("ITI 1120 Winter 2010 Assignment 3, Question 1"); System.out.println("Name: Diana Inkpen, #7654321"); System.out.println(); // READ IN GIVENS System.out.println( "Please enter the elements of the first array:"); b1 = ITI1120.readIntLine( ); System.out.println( "Please enter the elements of the second array:"); b2 = ITI1120.readIntLine( ); // BODY OF ALGORITHM b = bigSum(b1, b2); System.out.println( "The sum of the two big numbers is:"); for (i = 0; i < b.length; i++) { System.out.print(b[i] + " "); } System.out.println(); } public static int [] bigSum (int [] b1, int [] b2) { int index1, index2, indexResult ; // INTERMEDIATES int sum, carry; int l1, l2; int l; int [] b; // RESULT l1 = b1.length; l2 = b2.length; if (l1 > l2) {l = l1 + 1;} else {l = l2 + 1;} b = new int[l]; index1 = l1 - 1;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
index2 = l2 - 1; indexResult = l - 1; carry = 0; while (index1 >= 0 && index2 >= 0) { sum = b1[index1] + b2[index2] + carry; b[indexResult] = sum % 10; carry = sum / 10; index1 = index1 - 1; index2 = index2 - 1; indexResult = indexResult - 1;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

A3_2010_sol - Assignment 3 Solution Q1 IIT1120 Winter 2010...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online