Examples of Array of Structures in Programming: Student, Library, and Employee Records

Understanding how to use array of structures (AoS) in programming is a valuable skill, especially for those working with C. This article provides detailed examples in C, demonstrating how to manage and manipulate data using arrays of structures for various applications, including storing student information, managing a library system, and managing employee records.

What is an Array of Structures?

An array of structures (AoS) is a collection of multiple structures stored in a single variable. Each element in the array represents an individual structure. AoS allows for easier data management and processing in C by organizing related data in a structured manner.

Example 1: Storing Student Information

This program defines a structure for a student and uses an array of structures to store multiple students' information. Here's how the code works:

include 
struct Student {
    char name[50];
    int age;
    float grade;
};
int main() {
    struct Student students[3]; // Array of 3 Students
    // Input student information
    for (int i  0; i 

In this example, a structure named Student is defined to store the name, age, and grade of each student. An array of three Student structures is created to hold the information of three students. The program then prompts the user to input the details of each student and displays the information stored in the array.

Example 2: Managing a Library System

This program defines a structure for a book and uses an array of structures to manage a small library system. The code is as follows:

include 
struct Book {
    char title[100];
    char author[50];
    int year;
};
int main() {
    struct Book library[5]; // Array of 5 Books
    // Input book information
    for (int i  0; i  5; i  ) {
        printf("Enter title: ");
        scanf("%s", library[i].title);
        printf("Enter author: ");
        scanf("%s", library[i].author);
        printf("Enter year: ");
        scanf("%d", library[i].year);
    }
    // Display book information
    printf("
Book Information:
");
    for (int i  0; i  5; i  ) {
        printf("%d. Title: %s, Author: %s, Year: %d
", i   1, library[i].title, library[i].author, library[i].year);
    }
    return 0;
}

Here, a structure named Book is defined to store the title, author, and year of each book. An array of five Book structures is created to hold the information of five books. The program prompts the user to input details for each book and displays the information stored in the array.

Example 3: Employee Records

This program defines an employee structure and manages multiple employee records using an array of structures. Here's the code:

include 
struct Employee {
    int id;
    char name[50];
    float salary;
};
int main() {
    struct Employee employees[3]; // Array of 3 Employees
    // Input employee information
    for (int i  0; i  3; i  ) {
        printf("Enter ID: ");
        scanf("%d", employees[i].id);
        printf("Enter name: ");
        scanf("%s", employees[i].name);
        printf("Enter salary: ");
        scanf("%f", employees[i].salary);
    }
    // Display employee information
    printf("
Employee Information:
");
    for (int i  0; i  3; i  ) {
        printf("%d. ID: %d, Name: %s, Salary: %.2f
", i   1, employees[i].id, employees[i].name, employees[i].salary);
    }
    return 0;
}

In this example, a structure named Employee is defined to store the employee's ID, name, and salary. An array of three Employee structures is created to hold the information of three employees. The program prompts the user to input details for each employee and displays the information stored in the array.

Explanation

In these examples, a structure is defined to represent a specific entity (e.g., student, book, employee). An array of that structure is created to hold multiple instances of that entity. The program then takes input from the user to populate the array and displays the contents afterward. You can modify the number of records or the structure fields according to your needs!

Conclusion

Using array of structures in C provides an efficient way to manage and process related data. By organizing data in a structured manner, you can easily manipulate and display information for various applications, such as student records, library management, and employee records. Experimenting with different structures and arrays allows you to optimize your data management and improve the functionality of your applications.