CSCI A201/A597

Lecture Notes 27

Spring 2000


Solutions to some of the practice problems.

The solutions to these problems have been worked with Felecia Bell.

1. Append.

Here's a version without procedures (all in main).

public class Append1 {
    public static void main(String[] args) {
	int[] a, b; 
	a = new int[4]; // array of 0's
	b = new int[3]; // array of 0's 
	// initialize a
	for (int i = 0; i < a.length; i++) {
	    a[i] = (int) (Math.random() * (50 - 10) + 10);
	} 
	// now show a
	for (int i = 0; i < a.length; i++) {
	    System.out.print(a[i] + " "); 
	} System.out.println("\n------------------------------"); 
	for (int i = 0; i < b.length; i++) {
	    b[i] = (int) (Math.random() * (20 - 10) + 10); 
	} 
	// now show b
	for (int i = 0; i < b.length; i++) {
	    System.out.print(b[i] + " "); 
	} System.out.println("\n------------------------------"); 
	int[] c; 
	c = new int[7]; // 7 is in fact a.length + b.length
	// now put the elements from a and b into c 
	// first the elements of a
	for(int i = 0; i < a.length; i++) {
	    c[i] = a[i]; 
	} 
	// then the elements of b 
	for (int i = 0; i < b.length; i++) {
	    c[a.length + i]= b[i];
	} 
	// now show c
	for (int i = 0; i < c.length; i++) {
	    System.out.print(c[i] + " "); 
	} System.out.println("\n------------------------------"); 
    } 
} 
Here's the same thing with procedures, with the user specifying the sizes of the two arrays.

public class Append2 {
    public static void main(String[] args) {
	if (args.length != 2) {
	    System.out.println(" Please specify the sizes of the two" +
			       " arrays on the command line.\n"); 
	    System.exit(0); 
	} 
	int sizeA, sizeB; 
	sizeA = Integer.parseInt(args[0]); 
	sizeB = Integer.parseInt(args[1]); 
	int[] a, b;
	a = B.createArray(sizeA); 
	B.display(a); 
	b = B.createArray(sizeB); 
	B.display(b); 
	int[] c;
	c = B.append(a, b); 
	B.display(c); 
    } 
} 


class B {
    public static int[] createArray ( int size ) {
	int[] result = new int[size]; 
	for (int i = 0; i < result.length; i++) {
	    result[i] = (int) (Math.random() * (10 - 0) + 0); 
	} 
	return result; 
    } 
    public static void display( int[] x ) {
	for (int i = 0; i < x.length; i++) {
	    System.out.print(x[i] + " "); 
	} System.out.println("\n------------------------"); 
    } 
    public static int[] append( int[] a1, int[] a2) {
	int[] shelf = new int[a1.length + a2.length]; 
	for (int i = 0; i < a1.length; i++) shelf[i] = a1[i]; 
	for (int i = 0; i < a2.length; i++) shelf[i + a1.length] = a2[i]; 
	return shelf; 
    }
} 
2. Scalar Product.

public class Scalar {
    public static void main(String[] args) {
	int size = Integer.parseInt(args[0]); 
	double[] a, b; 
	a = B.createArray(size); 
	B.display(a); 
	b = B.createArray(size); 
	B.display(b); 
	double result;
	result = B.scalarProduct(a, b);
	System.out.println(result); 
    } 
} 

class B {
    public static double scalarProduct( double[] one, double[] two) {
	double sum = 0; 
	for (int i = 0; i < one.length; i++) {
	    sum = sum + one[i] * two[i];
	}
	return sum; 
    } 
    public static double[] createArray ( int size ) {
	double[] result = new double[size]; 
	for (int i = 0; i < result.length; i++) {
	    result[i] = Math.random() * (10 - 0) + 0; 
	} 
	return result; 
    } 
    public static void display( double[] x ) {
	for (int i = 0; i < x.length; i++) {
	    System.out.print(x[i] + " "); 
	} System.out.println("\n------------------------"); 
    } 
} 
3. Alternating Sum.

We can use the scalar product approach.

public class Alternate1 {
    public static void main(String[] args) {
	int size = Integer.parseInt(args[0]); 
        int[] a, b; 
	a = B.createArray(size); 
	B.display(a); 
	b = B.createAlternateArray(size); 
	B.display(b); 
	int result;
	result = B.scalarProduct(a, b);
	System.out.println(result); 
    } 
} 

class B {
    public static int[] createAlternateArray(int size) {
	int[] result = new int[size]; 
	for (int i = 0; i < size; i++) {
	    if (i % 2 == 0) {
		result[i] = 1; 
	    } else {
		result[i] = -1; 
	    } 
	} 
	return result; 
    }
    public static int scalarProduct( int[] one, int[] two) {
	int sum = 0; 
	for (int i = 0; i < one.length; i++) {
	    sum = sum + one[i] * two[i];
	}
	return sum; 
    } 
    public static int[] createArray ( int size ) {
	int[] result = new int[size]; 
	for (int i = 0; i < result.length; i++) {
	    result[i] = (int) (Math.random() * (10 - 0) + 0); 
	} 
	return result; 
    } 
    public static void display( int[] x ) {
	for (int i = 0; i < x.length; i++) {
	    System.out.print(x[i] + " "); 
	} System.out.println("\n------------------------"); 
    } 
} 
And we could also do it in a way of its own.

public class Alternate2 {
    public static void main(String[] args) {
	int size = Integer.parseInt(args[0]); 
	int[] a = new int[size]; 
	for (int i = 0; i < a.length; i++) {
	    a[i] = (int) (Math.random() * (25 - 5) + 5); 
	    System.out.print(a[i] + " "); 
	} System.out.println(); 
	int b = B.alternate(a); 
	System.out.println("the alternate sum is: " + b); 
    } 
} 

class B {
    public static int alternate(int[] a) {
	int sum = 0;
	for (int i = 0; i < a.length; i++) {
	    if (i % 2 == 0) {
		sum = sum + a[i];
	    } else {
		sum = sum - a[i];
	    }
	}
	return sum; 
    }
} 

Last updated: Apr 25, 2000 by Adrian German