
import java.util.Scanner;

class ArrayExercise {

	public static void main (String args[])
	{
		Scanner scanner = new Scanner(System.in);

		//-------------------------//
		//      Problem 8.1        //
		//-------------------------//
		boolean a[] = new boolean[5];

		// Loop for read in user input
		for( int i = 0; i < a.length; i++ )
		{
			System.out.print("Please input T or F ("+i+") : ");
			char in = scanner.next().charAt(0);

			if( in == 'T' || in == 't' )
				a[i] = true;
			else if( in == 'F' || in == 'f' )
				a[i] = false;
			else
			{	System.out.println("Error: wrong character entered.");
				i--;		// Repeat this index in the next round
			}
		}

		// Loop for printing the result
		for( int i = 0; i < a.length; i++ )
			System.out.println("index "+i+" = "+ a[i] );



		//-------------------------//
		//      Problem 8.4        //
		//-------------------------//
		System.out.print("\nPlease input array size : ");
		int size = scanner.nextInt();

		int b[] = new int[size];	// create array with specified size

		// Input loop
		for( int i = 0; i < b.length; i++ )
		{	System.out.print("Input for index "+i+" : ");
			b[i] = scanner.nextInt();
		}

		// Array search loop
		int max_i = 0;				// set initial max to 1st element
		for( int i = 1; i < b.length; i++ )	// start searching from 2nd element
		{	if( b[i] > b[max_i] )		// if current number > max
				max_i = i;		// make it the new max
		}

		System.out.println("The max number is "+b[max_i]+" at index "+max_i);



		//-------------------------//
		//      Problem 8.2        //
		//-------------------------//
		int c[] = b;	// reuse array from problem b.
		int sum = 0;	// result of summation

		// Calculate summation
		for( int i = 0; i < b.length; sum += c[i++] );

		System.out.println("The summation is "+ sum );
		System.out.println("The average is " + sum/(double)b.length +"\n");



		//-------------------------//
		//      Problem 8.5        //
		//-------------------------//
		System.out.print("Please input 1st array size : ");
		int size1 = scanner.nextInt();

		char d[] = new char[size1];	// create array with specified size

		// Input loop
		for( int i = 0; i < d.length; i++ )
		{	System.out.print("Input char for index "+i+" : ");
			d[i] = scanner.next().charAt(0);
		}

		System.out.print("Please input 2nd array size : ");
		int size2 = scanner.nextInt();

		char temp[] = new char[size2];	// create temp array

		// Loop for copying data from d[] to temp[]
		int minsize;
		if ( size1 < size2 )
			minsize = size1;
		else
			minsize = size2;

		for( int i = 0; i < minsize ; temp[i] = d[i++] );

		// If the new size is larger than original size,
		// ask user for additional input
		if( size2 > size1 )
			for( int i = size1; i < size2; i++ )
			{	System.out.print("Input char for index "+i+" : ");
				temp[i] = scanner.next().charAt(0);
			}

		// Print original array
		System.out.print("Before : ");
		for( int i = 0; i < d.length; i++ )
			System.out.print(d[i]+" " );

		// re-assign original array to temp array & print
		d = temp;
		System.out.print("\nAfter  : ");
		for( int i = 0; i < d.length; i++ )
			System.out.print(d[i]+" " );

		System.out.println("\n");
	}
}

