created 05/27/03; edits: 11/07/2012

# Chapter 46 Programming Exercises

Arrays can be confusing. The problem with arrays is that statements must now do indexing in addition to doing their usual work. Assignment statments used to be simple:

```sum = result + value;
```

Now they can be complicated:

```sum = result[j] + value[k];
```

Actually, arrays are not difficult once you get accustomed to the notation, and that just takes some practice. These exercises provide practice in using subscripts.

Copy each skeleton program to your programming editor and complete it there as directed. Then compile and run the program.

## Exercise 1 — Array Sum

Examine the following program:

```class Exercise1
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3};

sum =

System.out.println( "Sum of all numbers = " + sum );

}
}
```

Complete the assignment statement so that it computes the sum of all the numbers in the array.

## Exercise 2 — Two Arrays

Examine the following program:

```class Exercise2
{
public static void main ( String[] args )
{
int[] val = {13, -4, 82, 17};
int[] twice;

System.out.println( "Original Array: "
+ val + " " + val + " " + val + " " + val );

// Construct an array object for twice.

// Put values in twice that are twice the
// corresponding values in val.

System.out.println( "New Array: "
+ twice + " " + twice + " " + twice + " " + twice );
}
}
```

Complete the program so that a new array `twice` is constructed. Now copy values from `val` to `twice`, but make the values in `twice` double what they are in `val`.

## Exercise 3 — Three Arrays

Examine the following program:

```class Exercise3
{
public static void main ( String[] args )
{
int[] valA   = { 13, -22,  82,  17};
int[] valB   = {-12,  24, -79, -13};
int[] sum    = {  0,   0,   0,   0};

// Add values from corresponding cells of valA and valB
// and put the result in the corresponding cell of sum.

System.out.println( "sum: "
+ sum + " " + sum + " " + sum + " " + sum );
}
}
```

Complete the program with four assignment statements so that each cell of `sum` contains the sum of the corresponding cells in `valA` and `valb`. Ie., add cell zero of `valA` to cell zero of `valB` and put the result in cell zero of `sum`, and so on.

## Exercise 4 — Same Sum

Examine the following program:

```class Exercise4
{
public static void main ( String[] args )
{
int[] valA   = { 13, -22,  82,  17};
int[] valB   = {  0,   0,   0,   0};

// Put values into valB so that the sum of the values
// in corresponding cells of valA and valB is 25.

System.out.println( "valA: "
+ valA + " " + valA + " " + valA + " " + valA );

System.out.println( "valB: "
+ valB + " " + valB + " " + valB + " " + valB );

System.out.println( "sum:  "
+ (valA+valB) + " " + (valA+valB) + " "
+ (valA+valB) + " " + (valA+valB) );
}
}
```

Complete the program with four assignment statements that put values into `valB` so that the sum of corresponding cells in `valA` and `valB` is 25.

## Exercise 5 — Reverse Order

Examine the following program:

```class Exercise5
{
public static void main ( String[] args )
{
int[] val = {0, 1, 2, 3};
int temp;

System.out.println( "Original Array: "
+ val + " " + val + " " + val + " " + val );

// reverse the order of the numbers in the array

System.out.println( "Reversed Array: "
+ val + " " + val + " " + val + " " + val );
}
}
```

Complete the program so that the numbers in the array appear in reversed order. You will need to use the variable `temp` to do this.

Note: this is a harder exercise than you might guess. I sometimes put it on a midterm examination, and some students get the question wrong.