CSC115 · 2081 · 10 marks
What are the characteristics of array?Write a program to input age of 500 persons and display the following: a) Average age b) Age between 25 to 30[2+8]
What are the characteristics of array?Write a program to input age of 500 persons and display the following: a) Average age b) Age between 25 to 30[2+8]
C ProgrammingChapter 6: ArraysIntroduction to Array
Quick answer
An array is a collection of similar data type elements stored in contiguous memory locations. Its main characteristics are: Homogeneous Elements; Contiguous Memory Allocation; Index/Subscript Based Access.
Characteristics of Array and Program for Age Analysis
Characteristics of Array [2 marks]
An array is a collection of similar data type elements stored in contiguous memory locations. Its main characteristics are:
- Homogeneous Elements: All elements in an array must be of the same data type (e.g., all int, all float).
- Contiguous Memory Allocation: Array elements are stored in consecutive memory locations.
- Index/Subscript Based Access: Each element is accessed using an index starting from 0 (lower bound = 0, upper bound = size - 1).
- Fixed Size: The size of an array is fixed at the time of declaration and cannot be changed during execution.
- Random Access: Any element can be directly accessed using its index in constant time.
- Single Variable Name: Multiple values are stored under one variable name with different indices.
Program to Input Age of 500 Persons [8 marks]
#include<stdio.h>
#include<conio.h>
void main()
{
int age[500], i;
int count = 0; /* count of persons with age between 25 to 30 */
float sum = 0, average;
clrscr();
/* Input age of 500 persons */
for(i = 0; i < 500; i++)
{
printf("Enter age of person %d: ", i + 1);
scanf("%d", &age[i]);
}
/* Calculate sum and count persons with age between 25 to 30 */
for(i = 0; i < 500; i++)
{
sum = sum + age[i];
if(age[i] >= 25 && age[i] <= 30)
{
count++;
}
}
/* a) Calculate average age */
average = sum / 500;
printf("\n--- Results ---\n");
/* Display average age */
printf("\na) Average Age = %.2f", average);
/* b) Display ages between 25 to 30 */
printf("\n\nb) Ages between 25 to 30:\n");
for(i = 0; i < 500; i++)
{
if(age[i] >= 25 && age[i] <= 30)
{
printf("Person %d : %d years\n", i + 1, age[i]);
}
}
printf("\nTotal persons with age between 25 to 30: %d", count);
getch();
}
Explanation of the Program
| Step | Description |
|---|---|
| Declaration | int age[500] declares an integer array of size 500 to store ages |
| Input Loop | A for loop runs from i=0 to i<500 to input all 500 ages |
| Sum Calculation | Each age is added to sum to find total |
| Condition Check | age[i] >= 25 && age[i] <= 30 checks if age falls in the range |
| Average | average = sum / 500 gives the average age |
| Output | Average is displayed and ages in range 25-30 are printed with person number |
Sample Output
--- Results ---
a) Average Age = 35.46
b) Ages between 25 to 30:
Person 3 : 27 years
Person 7 : 25 years
Person 12 : 30 years
...
Total persons with age between 25 to 30: 85
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.