Answer:
The solution code is written in Python 3:
- count = 0
- totalAge = 0
- group1_count = 0 #age < 10
- group2_count = 0 #age 10 - 20
- group3_count = 0 #age 20 - 40
- group4_count = 0 #age > 40
- group1_total = 0 #subtotal age <= 10
- group2_total = 0 #subtotal age 11 - 20
- group3_total = 0 #subtotal age 21 - 40
- group4_total = 0 #subtotal age > 40
-
- age = int(input("Input an age: "))
- youngest = age
- oldest = age
-
- while(age != -1):
- count += 1
- totalAge += age
-
- if(age <= 10):
- group1_count += 1
- group1_total += age
- elif(age <= 20):
- group2_count += 1
- group2_total += age
- elif(age <= 40):
- group3_count += 1
- group3_total += age
- else:
- group4_count += 1
- group4_total += age
-
- if(youngest > age):
- youngest = age
-
- if(oldest < age):
- oldest = age
-
- age = int(input("Input an age: "))
-
-
- totalCustomer = count
- avgAge = totalAge / count
- rangeAge = oldest - youngest
- avgAgeGroup1 = group1_total / group1_count
- avgAgeGroup2 = group2_total / group2_count
- avgAgeGroup3 = group3_total / group3_count
- avgAgeGroup4 = group4_total / group4_count
-
- print("The total number of customers: " + str(totalCustomer))
- print("Total number of customer in Group 1 (<=10): " + str(group1_count))
- print("Total number of customer in Group 2 (11 - 20): " + str(group2_count))
- print("Total number of customer in Group 3 (21 - 40): " + str(group3_count))
- print("Total number of customer in Group 4 (> 40): " + str(group4_count))
- print("Average age for all customer: " + str(avgAge))
- print("Youngest age: " + str(youngest))
- print("Oldest age: " + str(oldest))
- print("The range of age: " + str(rangeAge))
- print("Average age for group 1 (<=10): " + str(avgAgeGroup1))
- print("Average age for group 2 (11 - 20): " + str(avgAgeGroup2))
- print("Average age for group 3 (21 - 40): " + str(avgAgeGroup3))
- print("Average age for group 4 (> 40): " + str(avgAgeGroup4))
Explanation:
Firstly, create the counter variables (e.g. count, group1_count, group2_count etc ) to track the total number of customer and number in each age group. Besides, create variables to hold the value of total of age and sub-total of each age group (Line 1 - 10). We presume there are only four age groups.
Next, prompt user to input the first age (Line 12)
Create variable youngest and oldest to track the lowest and highest age (Line 13 -14). At the moment, set the first input age as value of youngest and oldest.
Create a while loop with sentinel value -1 (Line 16). Within the loop, track the customer occurrence by incrementing counter variables by one (Line 17, 21, 24, 27 & 30). At the same time, add the current input age to totalAge and to group total (Line 18, 22, 25, 28, & 31).
If the current youngest value is bigger than the current input age, we set the current age to youngest (Line 33- 34).
If the current oldest value is smaller than the current input age, we set the current age to oldest (Line 36- 37).
After terminating the loop with -1, calculate the average age for all customers and for each age group and the range of age (Line 42- 48)
At last, display all the required calculation results using print function (50 - 62)