
import java.util.Scanner;

class Ex10 {

	public static void main ( String args[] )
	{
		Scanner scanner = new Scanner(System.in);
		boolean loop = true;
		int size;
		int array[];

		while( loop )
		{
			int c = menu();

			if( c == 1 )
			{
				System.out.print("Enter starting number : ");
				int start = scanner.nextInt();
				System.out.print("Enter ending number : ");
				int end = scanner.nextInt();
				printNumbers( start, end );
			}
			else if( c == 2 )
			{
				System.out.print("Please input the array size : ");
				size = scanner.nextInt();
				array = new int[size];

				for( int i=0; i < array.length; i++ )
				{	System.out.print("Please input element #"+i+" : ");
					array[i] = scanner.nextInt();
				}

				System.out.println("\nThe summation is "+ sum( array ) );
				System.out.println("The average is "+ average( array ) );
				System.out.println("The minimum number is "+ min( array ) );
			}
			else if( c == 3 )
			{
				System.out.print("\nPlease input 1st integer: ");
				int a = scanner.nextInt();
				System.out.print("Please input 2nd integer: ");
				int b = scanner.nextInt();

				System.out.println( a+" to the power of "+b+" is "+ pow(a,b) );
			}
			else if( c == 4 )
			{
				System.out.print("Please input the array size : ");
				size = scanner.nextInt();
				char ca[] = new char[size];

				for( int i=0; i < ca.length; i++ )
				{	System.out.print("Please input character #"+i+" : ");
					ca[i] = scanner.next().charAt(0);
				}

				System.out.print("\nOriginal array : ");
				System.out.println( ca );
				reverse( ca );
				System.out.print("Reversed array : ");
				System.out.println( ca );
			}
			else if( c == 5 )
			{
				System.out.print("Please input the array size : ");
				size = scanner.nextInt();
				array = new int[size];

				for( int i=0; i < array.length; i++ )
				{	System.out.print("Please input element #"+i+" : ");
					array[i] = scanner.nextInt();
				}

				System.out.print("\n\nPlease input search key : ");
				int key = scanner.nextInt();

				int ans = search( key, array );

				if( ans == -1 )
					System.out.println("Key not found.");
				else System.out.println("Key found at index " + ans );
			}
			else if( c == 6 )
			{
				loop = false;
				System.out.println("Bye bye.");
			}
			else System.out.println("Error: Invalid choice");

		} // end while
	}

	public static int menu()
	{
		Scanner scanner = new Scanner(System.in);
		int choice;

		System.out.println("\n\n(1) Print range of numbers");
		System.out.println("(2) Sum, Avg, Min of int array");
		System.out.println("(3) Power");
		System.out.println("(4) Reverse char array");
		System.out.println("(5) Searching int array");
		System.out.println("(6) Quit\n");

		do{
			System.out.print("Enter a selection : ");
			choice = scanner.nextInt();

			if( choice < 1 || choice > 6 )
				System.out.println("Invalid input.");

		} while( choice < 1 || choice > 6 );

		return choice;
	}

	//--------------//
	//  Problem. 9	//
	//--------------//

	public static void printNumbers( int a, int b )
	{
		if( a > b )				// Base case
			System.out.println();
		else 					// Rec case
		{	System.out.print( a + " " );	
			printNumbers( a+1, b );
		}
	}

	//--------------//
	//  Problem. 10	//
	//--------------//

	public static int sum( int array[] )
	{
		return sum( 0, array );
	}

	public static int sum( int index, int array[] )
	{
		if( index == array.length-1 )			// Base case
			return array[index];

		// Recursive case
		return array[index] + sum( index+1, array );	// Rec step
	}

	//--------------//
	//  Problem. 11	//
	//--------------//

	public static double average( int array[] )
	{
		return average( 0, array );
	}

	public static double average( int index, int array[] )
	{	
		int size;

		if( index == array.length-1 )		// Base case
			return array[index];
							// Rec case
		size = array.length - 1 - index;
		return ((double)array[index] + size*average( index+1, array ))/(size+1);
	}

	//--------------//
	//  Problem. 12	//
	//--------------//

	public static int min( int array[] )
	{
		return min( 0, 0, array );
	}

	public static int min( int curr, int min_index, int array[] )
	{
		if( curr == array.length )			// Base case
			return array[min_index];

		// Recursive case
		if( array[curr] < array[min_index] )
			return min( curr+1, curr, array );	// Rec step

		// Another recursive case
		else return min( curr+1, min_index, array );	// Rec step
	}

	//--------------//
	//  Problem. 13 //
	//--------------//

	public static int pow( int a, int b )
	{
		if( b == 0 )				// Base case
			return 1;

		return  a * pow( a, b-1 );		// Rec case & step
	}

	//--------------//
	//  Problem. 14 //
	//--------------//

	public static void reverse( char a[] )
	{
		reverse( 0, a );
	}

	public static void reverse( int index, char a[] )
	{
		if( index == a.length/2 )		// Base case
			return;
							// Rec case
		char temp = a[a.length-1-index];
		a[a.length-1-index] = a[index];		// swap the character
		a[index] = temp;

		reverse( index+1, a );			// adv to next index
	}

	//--------------//
	//  Problem. 15 //
	//--------------//

	public static int search( int value, int a[] )
	{
		return search( value, 0, a );
	}

	public static int search( int value, int index, int a[] )
	{
		if( index == a.length )			// Base case
			return -1;			// End of array

		if( a[index] == value )			// another Base case
			return index;			// value found

		return search( value, index+1, a );	// Rec case
	}

}

