Array of structures
In C, an array of structures allows us to store multiple instances of a structure in one collection, making it convenient to handle related data sets (like student records, employee information, etc.) where each entry has the same structure. Let's go through how to use an array of structures step-by-step.
Define the Structure
Let's start by defining a structure to represent a student with name
, rollNumber
, and marks
.
int rollNumber;
float marks;
};
Here:
name
is an array to hold the student's name.rollNumber
is an integer to store the student's roll number.marks
is a floating-point number for the student's score.
Declare an Array of Structures
To store multiple students, we declare an array of struct Student
. For example:
#include <string.h>
#define NUM_STUDENTS 5 // Define the number of students
struct Student {char name[50];
int rollNumber;
float marks;
};
int main() {
struct Student students[NUM_STUDENTS]; // Array of Student structures
// Input student data and print it
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
fgets(students[i].name,50,stdin);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Marks: ");
scanf("%f", &students[i].marks);
getchar();
}
// Display student data
printf("\nStudent Information:\n");
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("Student %d: Name = %s, Roll Number = %d, Marks = %.2f\n", i + 1, students[i].name, students[i].rollNumber, students[i].marks);
}
return 0;
}
In this code:
students
is an array ofstruct Student
withNUM_STUDENTS
elements.- We use a
for
loop to input data for each student. Eachstudents[i]
is an individualStudent
structure where we can access fields using dot notation, such asstudents[i].name
. - Another loop displays the information for each student.
Sorting Array of Structures (Example)
Let’s expand on this example by sorting the students based on their marks.
In this code:
- We define a function
sortStudentsByMarks
that takes thestudents
array and its size as parameters. - Using a nested loop, we sort the students based on their
marks
in descending order. We swap structures using a temporaryStudent
variable.
Summary
- An array of structures allows us to manage multiple records with a consistent format.
- We can access each structure's members using dot notation (e.g.,
students[i].name
). - Using loops, we can easily sort or process each element in the array of structures.
This approach is beneficial when working with collections of structured data
Example Programs
// Define the structure
struct Vehicle {
char model[50];
int year;
float price;};
// Function to accept input for vehicles
void inputVehicles(struct Vehicle v[], int n) {
for (int i = 0; i < n; i++) {
printf("\nEnter details of vehicle %d:\n", i + 1);
printf("Model: ");
scanf(" %[^\n]", v[i].model); // read string with spaces
printf("Year: ");
scanf("%d", &v[i].year);
printf("Price: ");
scanf("%f", &v[i].price);
}
}
// Function to sort vehicles in descending order based on price
void sortByPriceDescending(struct Vehicle v[], int n) {
struct Vehicle temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i].price < v[j].price) {
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}
}
}
// Function to display vehicles
void displayVehicles(struct Vehicle v[], int n) {
printf("\n%-20s %-10s %-10s\n", "Model", "Year", "Price");
printf("---------------------------------------------\n");
for (int i = 0; i < n; i++) {
printf("%-20s %-10d %-10.2f\n", v[i].model, v[i].year, v[i].price);
}
}
struct Vehicle vehicles[10];
inputVehicles(vehicles, 10);
sortByPriceDescending(vehicles, 10);
printf("\nVehicles sorted in descending order by price:\n");
displayVehicles(vehicles, 10);
return 0;
}
Define a structure that can describe a hotel with the following members: name, address, grade, average room charge, and number of rooms. Write a function to print out hotels of a given grade, sorted in order of their average room charges. ( University Question)
Comments
Post a Comment