To illustrate a practical application of arrays, let’s develop a program that computes the average age of a group of individuals:
// An array storing different ages int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; float avg, sum = 0; // Get the length of the array // Loop through the elements of the array // Calculate the average by dividing the sum by the length // Print the average |
In this example, we construct a program to determine the youngest age among a set of ages:
// An array storing different ages int ages[] = {20, 22, 18, 35, 48, 26, 87, 70}; // Get the length of the array // Create a variable and assign the first array element of ages to it // Loop through the elements of the ages array to find the lowest age |