/*
 * assign : hw6 part 1
 */

#include <stdio.h>
#define MAX_SIZE 5

int compare (int a[], int b[]);

void main(void)
{
	int a[MAX_SIZE], b[MAX_SIZE];
	int counter;

	/* getting two sets of integers.. */
	printf ("Please enter %d number of integers for set A\n", MAX_SIZE);
	for ( counter = 0; counter < MAX_SIZE; counter ++ ) {
		scanf ( "%d", &a[counter]);
	}

	printf ("Please enter %d number of integers for set B\n", MAX_SIZE);
	for ( counter = 0; counter < MAX_SIZE; counter ++ ) {
		scanf ("%d", &b[counter]);
	}

	/* compare & print the result */
	printf ("the result of comparing a and b is %d\n", compare (a,b));

	return ;
}

/* this function will compare two arrays & if a > b,
   retutn -1. if b > a, return 1. if a == b, then retutn 0
*/

int compare ( int a[], int b[] ) {
	int counter;

	/* compare all the elements */
	for ( counter =0; counter < MAX_SIZE; counter ++ ) {
		if ( a[counter] > b[counter] )
			return 1;
		if ( a[counter] < b[counter] )
			return -1;
	}
	
	/* if nothign is different, they are equal.. thus, return 0*/
	return 0;
}





/*
 * assign : hw6 part 2
 */

#include <stdio.h>
#include <string.h>		/* for function strcpy() */
#define MAX_SIZE 5


int compare (char a[], char b[]);

void main(void)
{
	char a[MAX_SIZE], b[MAX_SIZE], c[MAX_SIZE];
	char temp[MAX_SIZE];	/* string temp will be used to store
							   string a, b, or c temporarily during
							   swaping
							 */

	/* get string a, b, c from user. notice it's MAX_SIZE -1.
	   The last element should be used to store null character '\0'
	 */

	printf ("Please enter string a with the length of %d: ", MAX_SIZE -1);
	scanf ("%s", a);

	printf ("Please enter string b with the length of %d: ", MAX_SIZE -1);
	scanf ("%s", b);
	
	printf ("Please enter string c with the length of %d: ", MAX_SIZE -1);
	scanf ("%s", c);

	/* sorting a,b and c. 
	 * sorting algorithm is as follows:
	 * first two if statements will make the biggest element to be in 'c'
	 * after first two if statements, c is the biggest.
	 * so in the third if statement, you only need to check if 'a' and 'b'
	 * is in order. if not, swap.
	 * e.g.   a = "cccc"
	 *        b = "bbbb"
	 *        c = "aaaa"
	 * if ( compare ( a, b ) == 1 )  < -- true, thus, swap
	 *       a = "bbbb"
	 *       b = "cccc"
	 *       c = "aaaa"
	 * if ( compare ( b, c ) == 1 ) < -- true, thus swap
	 *       a = "bbbb"
	 *       b = "aaaa"
	 *       c = "cccc"
	 * if ( compare ( a, b ) == 1 ) <-- true, thus swap
	 *       a = "aaaa"
	 *       b = "bbbb"
	 *       c = "cccc"
	 * DONE!
	 */
	if ( compare ( a, b ) == 1 ) {
		strcpy ( temp, a);
		strcpy ( a, b);
		strcpy ( b, temp);
	}

	if ( compare ( b, c ) == 1 ) {
		strcpy ( temp, b);
		strcpy ( b, c );
		strcpy ( c, temp );
	}

	if ( compare ( a, b) == 1 ) {
		strcpy ( temp, a );
		strcpy ( a, b );
		strcpy ( b, temp );
	}

	/* printing out the result */
	printf ("Smallest : %s\n", a);
	printf ("Middle   : %s\n", b);
	printf ("Biggest  : %s\n", c);

	return;

}

/* this function will compare two arrays & if a > b,
   retutn -1. if b > a, return 1. if a == b, then retutn 0
*/

int compare ( char a[], char b[] ) {
	int counter;

	/* compare all the elements */
	for ( counter =0; counter < MAX_SIZE && a[counter] != '\0' && b[counter] != '\0' ; counter ++ ) {
		if ( a[counter] > b[counter] )
			return 1;
		if ( a[counter] < b[counter] )
			return -1;
	}
	
	/* if nothign is different, they are equal.. thus, return 0*/
	return 0;
}
