This program is broken down into phases for your convenience only. Please turn i
ID: 3914622 • Letter: T
Question
This program is broken down into phases for your convenience only. Please turn in only the final phase. Before turning in your program, please make sure that it allows the user to not enter any ages at all, and does something reasonable in this case (i.e., if the user enters a negative number the first time).
Don't use arrays in this program.
Important Hint: You will save yourself a lot of time and anguish if you follow the patterns and examples given in the lesson closely! The main point of this assignment is to ensure that you understand and can reproduce the loop pattern shown in the example in lesson 4.2.
Phase 1: Write a program for a theater that will keep track of how many people in each of 5 age categories attended a particular movie. Use the 5 age categories listed below in the sample screen output. The user will enter a number of ages, entering a negative number when there are no more ages to enter. Your program will then report on how many people in each age group attended. Sample screen output:
Be sure to use named constants for the boundaries of the age groups.
Phase 2: Modify your program so that, in addition to the report that the program currently produces, it also gives the average age of the people in attendance, the age of the oldest person in attendance, and the age of the youngest person in attendance. The average must be printed rounded to the nearest tenth of a year, as shown below. You may not assume any maximum age of attendees!
Don't forget to look at the examples in lesson 4 for help with this!
Sample screen output:
Phase 3: Modify your program so that it also asks for each attendee's food preference ('p' for popcorn, 's' for soda, 'b' for both). Your program should then report this data as well.
Hint: The lessons contain an example of a loop like this where two questions are asked. Follow the pattern in that example.
Sample screen output:
Your program should allow the user to not enter any ages at all (i.e. the user enters a negative number the first time). In that case it should print a message to explain what happened and then end execution.
Turn in your source code from phase 3 and your output. Turn in only one output, and make it match the sample screen output given above for phase 3 exactly.
Explanation / Answer
Assumptions : Working in Microsoft Windows Console Application Using C#.
Explanation :
There is one class named Program.cs with Main method as its start Point.
It has 2 other methds as GetAge & GetFoodPreference to read user inputs.
Class has constats at top to define Group Age Limits.
Please find code in bold.
Implementation :
using System;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
const int GROUP1_MIN = 0;
const int GROUP1_MAX = 18;
const int GROUP2_MIN = 19;
const int GROUP2_MAX = 30;
const int GROUP3_MIN = 31;
const int GROUP3_MAX = 40;
const int GROUP4_MIN = 41;
const int GROUP4_MAX = 60;
const int GROUP5_MIN = 60;
static void Main(string[] args)
{
int totalCount = 0, totalAge = 0;
int group1Count = 0, group2Count = 0, group3Count = 0, group4Count = 0, group5Count = 0;
int popcornCount = 0, sodaCount = 0, bothCount = 0;
int minAge = 0, maxAge = 0;
double avgAge = 0;
int age = GetAge();
if(age < 0)
{
Console.WriteLine("No one attended the movie.");
Console.ReadLine();
return;
}
else
{
minAge = age;
}
while (age >= 0)
{
string foodPreference = GetFoodPreference();
// Check for the age group
// Add one to the appropriate counter
if (age >= GROUP1_MIN && age <= GROUP1_MAX)
{
group1Count++;
}
else if (age >= GROUP2_MIN && age <= GROUP2_MAX)
{
group2Count++;
}
else if (age >= GROUP3_MIN && age <= GROUP3_MAX)
{
group3Count++;
}
else if (age >= GROUP4_MIN && age <= GROUP4_MAX)
{
group4Count++;
}
else if (age >= GROUP5_MIN)
{
group5Count++;
}
// Add 1 to total count
totalCount++;
// Add age to total age
totalAge += age;
// Check for youngest person
if (age < minAge)
minAge = age;
// Check for oldest person
if (age > maxAge)
maxAge = age;
// Check for food preference
// Add one to the appropriate counter
if (string.Compare(foodPreference, "p", true) == 0)
{
popcornCount++;
}
else if (string.Compare(foodPreference, "s", true) == 0)
{
sodaCount++;
}
else if (string.Compare(foodPreference, "b", true) == 0)
{
bothCount++;
}
age = GetAge();
//foodPreference = GetFoodPreference();
}
Console.WriteLine("age 0 to 18: {0}", group1Count);
Console.WriteLine("age 19 to 30: {0}", group2Count);
Console.WriteLine("age 31 to 40: {0}", group3Count);
Console.WriteLine("age 41 to 60: {0}", group4Count);
Console.WriteLine("over 60: {0}", group5Count);
Console.WriteLine();
Console.WriteLine("food preference popcorn: {0}", popcornCount);
Console.WriteLine("food preference soda: {0}", sodaCount);
Console.WriteLine("food preference both: {0}", bothCount);
avgAge = totalAge / totalCount;
avgAge = Math.Round(avgAge, 1);
Console.WriteLine("The average age was {0}", avgAge);
Console.WriteLine("The youngest person in attendance was {0}", minAge);
Console.WriteLine("The oldest person in attendance was {0}", maxAge);
Console.ReadLine();
}
static int GetAge()
{
string userInput;
Console.Write("Enter age of attendee (negative number to quit): ");
userInput = Console.ReadLine();
return Convert.ToInt32(userInput);
}
static string GetFoodPreference()
{
Console.Write("Enter food preference ('p' for popcorn, 's' for soda, 'b' for both): ");
return Console.ReadLine();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.