C Program for Student Details Using Array

Today we are going to discuss a very frequently asked programming question involving arrays. C Program for student details using an array. This program is a very fun way of managing a database of students.

Mostly, this program is asked to test the managing skills of a student. It uses the concept of arrays which makes it very important.

Hence, without much delay, let’s get our coding brains to use and get started! 

A Little Background on Arrays:

By this time, everyone knows what an array is. We just tend to forget some of its key properties and definitions. Therefore, we keep coming back for some explanations before working on them.

We define arrays as a kind of data structure that stores a fixed-size sequential collection of elements of the same type. Additionally, an array is used to store a collection of data, however, it is more useful to consider it as a collection of variables of the same type.

Arrays possess many properties. In order to work with them, we need to remember some key points:

  1. An array can only have elements of the same data type.
  2. The system stores the elements of an array in subsequent memory locations.
  3. An array index starts with 0 and goes up to n-1 where n is the number of elements.
  4. In C programming, it supports multidimensional arrays. For example, a two-dimensional array.
  5. In the case of two-dimensional arrays, the system stores the elements row by row in subsequent memory locations.
  6. The array name refers to the address of the first element.
  7. Array size is always constant and not a variable.
  8. If we have to pass an array to a function, we can refer to it by defining a pointer to the specific array
  9. We generate the pointer to the first element of an array by the array name without index. 
  10. In C we have a function that can return an array.

Storing Information in an Array:

Since, our program deals with storing student details in an array, let us study the concept in a little depth. 

Array Declaration:

Usually, we declare an array as :data_type array_name[array_size]; 

For example, int Arr[100]; 

We also have another way,  : 

data_type  array_name[array_size] = {element1, element2,…….elementn};

For example, int Arr[3] = {1,2,3};

Both of these methods allocate some data to our array. In the first one, there is an empty memory space of size 100. In the second, the system allocates three spaces for three values. 

Assigning values to elements in an array:

We can assign values to an element in an array-like we assign value to a scalar variable. We just need to reference an individual element of an array by using the array name and the index inside the parentheses. After that, we use the assignment operator followed by the value. If we do not assign any value to an array empty, it has a null(or empty) value.

For instance, 

Arr[1]= 0;

Arr[56]=32; and so on.

We can also use iterations for assigning values to large arrays. Because individually assigning so many values is not feasible. It will only increase the workload and complexity of the program.

We can also take user input for assigning values and enter them through the help of a loop. 

For example,

printf(“Enter the size of array:”);

scanf(“%d”, n);

char Arr[n][20];

for (i = 0; i < n; i++)

    scanf(“%s”, array[i]);

In the above example, we have declared a two-dimensional array. Next, in the for loop, we enter the element up to n elements. This way the user has control over the size of arrays.

Thus, we have learned the basis of storing data to an array. This will help us in our c program for student details using array. 

Algorithm:

The program for student details using an array is a very lengthy set of codes. It will take time. Therefore, to avoid any confusion, the algorithm will help us to map the entire program. I have described the algorithm in a simple manner so it will not be difficult to understand.

  1. Start
  2. Define a structure student with variables for first name, roll number, and marks.
  3. Then, create a structure variable stu[10] which is an array.
  4. Define the main function.
  5. Initialize i=0 in a for loop which runs till 5 elements.
  6. Next, take user input for first name, roll number, and marks.
  7. Finally, define another loop for displaying information of the five students.
  8. Stop.

Program:

//C program for entering details in an array. 
#include <stdio.h>
struct student {
    char fName[50];
    int rollno;
    float marks;
} stu[10];
int main() {
    int i;
    printf("Enter information of students:\n");
    // storing information
    for (i = 0; i < 5; ++i) {
        stu[i].rollno = i + 1;
        printf("\nFor roll number%d,\n", stu[i].rollno);
        printf("Enter first name: ");
        scanf("%s", stu[i].fName);
        printf("Enter marks: ");
        scanf("%f", &stu[i].marks);
    }
    printf("Displaying Information:\n\n");
    // displaying information
    for (i = 0; i < 5; ++i) {
        printf("\nRoll number: %d\n", i + 1);
        printf("First name: ");
        puts(stu[i].fName);
        printf("Marks: %.1f", stu[i].marks);
        printf("\n");
    }
    return 0;
}

Program Explanation:

The code written above is one of the simplest approaches of association of student details with arrays. It uses a structure with basic elements for basic information of a student such as first name, roll number, and marks. It will take user input and display the information accordingly.

To understand the code better, I have listed a step by step procedure below. This explanation will provide an overview to the problem so that you can code for yourself. 

A structure in c is a user-defined data type which groups variables of different data type into a common one.

Since we have three types of information to enter into a single array, we define a structure for defining three variables related to a student into one student structure. 

Then, we create a stu[10] array variable which we will use to store these information.

After that, we move directly to the main function. In this function, we define a for loop for entering the student details into the structure array variable.

Similarly, we define another loop for displaying these details. 

Some Modifications:

Now, though this is a basic code for the problem there can be certain modifications made to make the program more useful.

Hence, some of them that you can try are listed below:

  1. You can make the user enter the details then given them the choice to search for a particular record by entering certain criteria.
  2. Also, you can increase the number of elements or ask the user to enter the size of the student record. 
  3. Define a switch case scenario in which the user can choose to search, delete, enter or display any record/records.
  4. Additionally, You can create a modified tabular view of the student details for visual appeal.

In other words, if you understand the program above, these modifications will be easy to execute. Don’t be afraid to enhance your skills and try them out. It will boost your coding ability and help you understand the concept better. 

Output:

The program we just executed gives the following output:

Output for array of student details
Output for the C Program for Student Details Using Array-1.
Output for array of student details
Output for the C Program for Student Details Using Array-2.

Ending note :

In conclusion, arrays are a very important and interesting concept in C Programming. It is a concept that is useful for most of the problems in coding. Therefore, the student details program is beneficial for your school-level or college-level coding. Additionally, it will also help you manage a huge amount of data.

Thus, this problem is also a very user interactive problem. And, it can be made more interactive with certain modifications. 

Practice all these programs and you might find your own code! . Also Have a look at the C program for file permission.

Still have doubts or queries? Just ask away. We are here to help.

See you in our next post!

Leave a Comment

Your email address will not be published. Required fields are marked *