﻿

//Coding exercise: Average age of people in a household

//Initialize some variables
var    input   = "";
int    howMany = 0;

//Get valid input for number of people
while ( (!Int32.TryParse(input, out howMany)) && (howMany <= 0) ) {
	Console.WriteLine("How many people live in your household?");
    input = Console.ReadLine();
}

//Initialize some arrays
int[]    ages  = new int[howMany];
string[] names = new string[howMany];

//Get valid names and ages for every person
for (int i = 0; i < howMany; i++ ) {
	input = "";
	while (input == "") {
		Console.WriteLine("What is the name of person #" + (i+1) + "?");
        input = Console.ReadLine();
	}
	names[i] = input;
	input = "";
	while (!Int32.TryParse(input, out ages[i])) {
		Console.WriteLine("How old is person #" + (i + 1) + "?");
		input = Console.ReadLine();
	}
}

//Output average age
Console.WriteLine("The average age is " + Convert.ToInt32(ages.Average()));

//Some helper vars
int indexYoungest = Array.IndexOf(ages, ages.Min());
int indexOldest   = Array.IndexOf(ages, ages.Max());

//Output stats if oldest and youngest person
//(Not catching people potentially having the same age.)
Console.WriteLine("The youngest person is " + names[indexYoungest] + ", at " + ages[indexYoungest] + "yo");
Console.WriteLine("The oldest person is " + names[indexOldest] + ", at " + ages[indexOldest] + "yo");
