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

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

View Full Document Right Arrow Icon
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;
Background image of page 1

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

View Full DocumentRight Arrow Icon
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;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/06/2011 for the course CSI 1120 taught by Professor Inkpen during the Fall '07 term at University of Ottawa.

Page1 / 7

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online