// july30ex1.cpp : Defines the entry point for the console application.
// This program first creates an array of random ages
// IT then uses functions to compare the two 'sides' based on avg age
// and a function to figure out max age

#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
#define maxage 120
#define numsubjects 200

void compare_sides(void);
int oldest_age(void);

 int peoples_age [numsubjects];

int main(int argc, char* argv[])
{  
     int person, oldest, total_age=0;
	 float avg_age;

	srand(time(NULL));

	for (person=0; person < numsubjects; person++) {
        peoples_age [person] = rand() % 121; 
		printf("Person %3d is %3d years old\n", person, peoples_age[person]);
		total_age += peoples_age[person];

	} /* end for person */

    avg_age = (float) total_age / numsubjects;

	printf("\nThe average age is %6.2f\n", avg_age);

	compare_sides();

	oldest = oldest_age();
	printf("The oldest person was %d years old\n", oldest);

	return 0;
}  /* end main */

void compare_sides(void) {

	int s1_total = 0, s2_total = 0, x;
	float s1_avg, s2_avg;

    for (x=0; x< numsubjects/2; x++)
		s1_total += peoples_age [x];
	s1_avg = (float) s1_total / (numsubjects /2);

    for (x=numsubjects/2; x< numsubjects; x++)
		s2_total += peoples_age [x];
	s2_avg = (float) s2_total / (numsubjects /2);

    printf("Side 1 avg age = %5.2f and Side 2 avg age = %5.2f \n", s1_avg, s2_avg);
	if (s1_avg < s2_avg)
        printf ("Side 1 is younger!\n");
	else if (s1_avg > s2_avg)
		printf ("Side 1 is older!\n");
	else
		printf ("The two sides are the same avg age!\n");

}   /* end function compare_sides */


int oldest_age() {

	int person, max = -1;
	for (person=0; person < numsubjects; person++)
		if (peoples_age[person] > max)
			max = peoples_age [person];



    return max;


} /* end function oldest_age */