How to Use C Program to Store Information of 10 Students Using Array
If you are a student in the computer science or programming field, then you must be familiar with C programming language. C is a low-level programming language that is widely used in system software, embedded systems, and other areas requiring direct access to hardware.
In this article, we will discuss how to use C program to store information of 10 students using array. The array is a data structure that can hold a fixed number of elements of the same data type. Using the array, we can easily store and retrieve the information of multiple students.
Introduction to Array in C
In C, the array is a collection of the same data type values stored in contiguous memory locations. The first element of the array is indexed by 0, the second element is indexed by 1, and so on. It is declared by specifying the data type followed by the name of the array and the number of elements in the square brackets.
Here is an example of an integer array that can store 5 integers:
“`c
int myArray[5];
“`
Storing Information of 10 Students Using Array
To store information of 10 students using array, we need to declare an array of structures. A structure is a user-defined data type that can hold different data types under a single name. In this case, we will use a structure to hold the information of each student such as name, roll number, and marks.
“`c
struct student {
char name[50];
int rollno;
float marks;
};
“`
We can then declare an array of structures to hold the information of 10 students:
“`c
struct student myStudents[10];
“`
We can then use a loop to enter the information of 10 students into the array of structures. Here is an example of how to do it:
“`c
for(int i=0; i<10; i++) {
printf("Enter the name of student %d: ", i+1);
scanf("%s", myStudents[i].name);
printf("Enter the roll number of student %d: ", i+1);
scanf("%d", &myStudents[i].rollno);
printf("Enter the marks of student %d: ", i+1);
scanf("%f", &myStudents[i].marks);
}
```
Retrieving Information of 10 Students Using Array
We can easily retrieve the information of 10 students using the array of structures. We can use a loop to iterate through the array and print the information of each student. Here is an example of how to do it:
“`c
for(int i=0; i<10; i++) {
printf("Name of student %d: %s\n", i+1, myStudents[i].name);
printf("Roll number of student %d: %d\n", i+1, myStudents[i].rollno);
printf("Marks of student %d: %.2f\n", i+1, myStudents[i].marks);
}
```
Conclusion
In this article, we have discussed how to use C program to store information of 10 students using array. We have learned about the basics of array and structure in C and how to use them to store and retrieve multiple values of different data types. By using the array of structures, we can easily manage and access the information of multiple students.