0% found this document useful (0 votes)
7 views10 pages

C Program for Student Data Management

Uploaded by

adityapro87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

C Program for Student Data Management

Uploaded by

adityapro87
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXP-18

AIM: -
Demonstrate structure and union to specify data on students like Roll-number,
Name, Department, Course, Year of Joining and Print the data of student according
to roll number
APPARATUS: - Computer Machine, C software

THEORY:
The program aims to manage student information by using structures and unions in
C. It allows the user to input multiple student records, storing details such as roll
number, name, department, course, and year of joining. The user can then retrieve
and display a student’s information based on their roll number.

Key Concepts
1. Structures:
o A structure in C is a user-defined data type that allows grouping of different data
types
under a single name. Structures help to model complex data more effectively.
o In this program, a Student structure is defined to hold various details about a
student,
allowing us to treat the student's attributes as a single entity.

2. Unions:
o A union is similar to a structure in that it can hold multiple data types, but it only
allocates
enough memory for the largest member. This means that only one member can
contain a value at a time.
o In this program, a CourseInfo union is defined to store either the course name or
course
code, though we primarily use the course name.
3. Dynamic Data Management:
o The program can handle multiple student records, making it scalable and
applicable to scenarios like school management systems or student databases.

Program Structure
1. Include Required Libraries

• The stdio.h library is included for input and output functions, while string.h is
included for string
handling functions (although not used in this specific code).

2. Union Definition
• The CourseInfo union can hold a course name as a string or a course code as an
integer.
3. Structure Definition

• The Student structure includes:


o rollNumber: Unique identifier for each student.
o name: Student's name.
o department: The department the student belongs to.
o course: Union to hold course information.
o yearOfJoining: The year the student joined.
4. Function to Print Student Details

• This function takes a Student structure as input and prints the details in a readable
format.
5. Main Function Logic

1. Initialization:
o An array of Student structures is declared to hold up to 100 student records.
o An integer studentCount keeps track of the number of students added.
2. Input Loop:
o The program prompts the user to enter student details in a loop.
o After entering each student’s details, the user can decide whether to add more
records.
3. Search for Student:
o After collecting student data, the user can enter a roll number to search for a
specific student.
o The program checks the list of students for a match and prints the corresponding
details using the printStudentDetails function.
4. Output:
o If a student is found, their details are printed; otherwise, a message indicates that
no student with the given roll number was found.

ALGORITHM:
1. Start.
2. Define a Union CourseInfo:
o Define a character array courseName[50] for the course name.
o Define an integer courseCode (optional, if needed for different representation).
3. Define a Structure Student:
o Declare an integer variable rollNumber.
o Declare a character array name[50].
o Declare a character array department[50].
o Declare a variable of type CourseInfo (union).
o Declare an integer variable yearOfJoining.
4. Initialize Variables:
o Declare an array of Student structures to hold multiple records (e.g.,
students[100]).
o Declare an integer variable studentCount initialized to 0.
o Declare an integer variable rollNumberToFind.
5. Input Loop:
o Repeat the following steps until the user decides to stop:
1. Prompt the user to enter the roll number.
2. Read and store the roll number in the current
students[studentCount].rollNumber.
3. Prompt the user to enter the name.
4. Read and store the name in students[studentCount].name.
5. Prompt the user to enter the department.
6. Read and store the department in students[studentCount].department.
7. Prompt the user to enter the course name.
8. Read and store the course name in students[studentCount].[Link].
9. Prompt the user to enter the year of joining.
10. Read and store the year of joining in students[studentCount].yearOfJoining.
11. Increment studentCount by 1.
12. Ask the user if they want to add another student (if 'y', continue; otherwise, exit
the loop).

6. Search for Student:


o Prompt the user to enter a roll number to search.
o Read and store the roll number in rollNumberToFind.
7. Search Loop:
o Initialize a variable found to 0 (indicating not found).
o Loop through the students array from 0 to studentCount - 1:
1. If students[i].rollNumber equals rollNumberToFind:
▪ Call the function to print student details.
▪ Set found to 1 (indicating found).
▪ Break the loop.
8. Check If Found:
o If found is still 0 after the loop, print "Student not found".
End.

PROGRAM CODE:

include<stdio.h>
#include<string.h>
//Define a union to hold course information
union CourseInfo
{
char courseName[50];
int courseCode; //Optional if you need a code representation
};
///Define a structure for student data
struct Student
{
int rollNumber;
char name[50];
char department[50];
union CourseInfo course; //Using union to store course info
int yearOfJoining;
};
//Functions to print student details
void printStudentDetails(struct Student student)
{
printf("\nStudent Details:\n");
printf("Roll Number: %d\n", [Link]);
printf("Name: %s\n", [Link]);
printf("Department: %s\n", [Link]);
printf("Couse Name: %s\n", [Link]);
printf("Year of Joining: %d\n", [Link]);
}
int main()
{
struct Student students[100]; //Array to hold up to 100 students
int studentCount=0;
int rollNumberToFind,i;
//Input student data
char choice;
do
{
printf("Enter Roll Number:");
scanf("%d", &students[studentCount].rollNumber);
printf("Enter Name:");
scanf("%[^\n]s",students[studentCount].name);
printf("Enter Department:");
scanf("%[^\n]s", students[studentCount].department);
printf("Enter Course Name:");
scanf("%[^\n]s",students[studentCount].[Link]); // store
course name in
printf("Enter Year of Joining:");
scanf("%d", &students[studentCount].yearOfJoining);
studentCount++;

printf("Do you want to add another student?(y/n):");


scanf("%c", &choice);
}
while (choice == 'y' || choice =='Y');
//Input roll number to find student details by roll number
printf("\nEnter Roll Number to search for:");
scanf("%d", &rollNumberToFind);
//Search and print student detail by roll number
int found =0;
for(i=0;i, studentCount; i++)
{
if(students[i].rollNumber == rollNumberToFind)
{
printStudentDetails(students[i]);
found=1;
break;
}
}
if(!found)
{
printf("Student with Roll Number %d not found.\n",
rollNumberToFind);
}
return 0;
}

OUTPUT:

CONCLUSION:

In this program, we demonstrated the use of struct and union in C programming to


efficiently manage and display student data. The struct was used to create a custom
data type to hold details like roll number, name, department, course, and year of
joining. This allowed for easy grouping and access to each student's information.
The union, on the other hand, was used to save memory by storing the student's roll
number in the same memory space as other variables, ensuring that the same
memory location could hold different data at different times.

By using a combination of struct and union, we could effectively represent a


student's information in a compact form. We also implemented functionality to
print the student data by searching through the roll number, showcasing how the
structure can be accessed and displayed based on a unique identifier.

This approach highlights the flexibility and efficiency of C's data structures,
offering a solution that not only stores the data but also allows for efficient
retrieval based on user input (like the roll number). This exercise also emphasizes
the importance of memory management in C, where unions help to minimize
memory usage while still allowing access to different fields of data.

Common questions

Powered by AI

The use of structures and unions in the student program demonstrates memory efficiency by grouping related data with structures and selectively allocating memory with unions. Structures enable managing complex data types efficiently by treating student attributes as a single unit, reducing potential programming errors and improving readability. Unions, by allocating memory only for the largest member, conserve memory by allowing multiple variables to share the same location. This efficiency is significant in systems with memory limitations, such as embedded systems, where reducing memory usage can enhance performance .

The program facilitates student information retrieval using unique identifiers like roll numbers through a linear search algorithm. Upon user request, it prompts for a roll number, iterates through the array of structures comparing each entry’s roll number with the input, and calls a function to print student details if a match is found. This approach ensures efficient data retrieval, taking advantage of roll numbers as unique identifiers to pinpoint a specific student’s information quickly. It simplifies the retrieval process, making it intuitive for users and effective for verification purposes without extensive computation .

The input loop and user prompts in the program enhance user interaction by providing a structured and intuitive interface for entering multiple student records. The loop continuously asks for inputs for each field, such as roll number, name, department, course name, and year of joining, allowing users to input as many records as necessary until they choose to stop. This ensures that the data is entered consistently and completely for each student, promoting data integrity. Clear prompts reduce input errors, while the provision to exit or continue improves user control over the data entry process, contributing to a user-friendly experience .

To handle larger datasets and improve efficiency, the student data program could be modified to use dynamic data structures such as linked lists or arrays allocated with dynamic memory functions like malloc, allowing it to grow beyond the static 100 record limit. Implementing binary search or hashing instead of linear search would improve lookup times. Furthermore, substituting unions with structures for course information would enable storing both course names and codes simultaneously, enhancing versatility. Introducing database integration could future-proof the application, allowing efficient data handling and retrieval for large datasets .

Using unions to store course information in a student record management system implies a trade-off between memory efficiency and versatility. With a union, only one of its members (course name or course code) can hold a value at any given time, which can conserve memory if course information needs are flexible. However, this also means that if both course name and code are needed simultaneously, the union will not suffice, leading to potential information loss. Thus, careful consideration is required to ensure that program requirements align with the memory-saving capabilities of unions .

Using structures to print student details rationalizes program design by encapsulating related attributes within a single data type, simplifying access and modification of student information. This encapsulation makes the program more readable and maintainable since changes in student detail representation require modifications in fewer code segments. It enhances modularity, as functions dealing with student data can interact smoothly with these structured entities. This approach minimizes redundancy, potential errors, and increases clarity of the program’s purpose and logic, benefiting long-term program maintenance .

The program handles dynamic data management by allowing multiple student records to be input, stored, and retrieved using an array of structure instances up to a set limit (100 records in this case). This approach provides flexibility to scale the number of student records without altering the underlying program structure significantly. The primary benefit is that it accommodates varying amounts of student data efficiently, making it applicable for real-world scenarios such as educational databases or management systems. This setup supports batch processing of data and simplifies searching for and displaying data based on unique identifiers like roll numbers .

Structures in C allow various data types to be grouped together under a single name, making it possible to represent related information as a single entity. For managing student data, a structure can store multiple elements like roll number, name, department, and year of joining as a collective, ensuring each attribute of the student's information is separately and simultaneously accessible. Unions, however, are similar to structures but differ as they allocate memory to the largest member and can only store one type of data at a time, thereby saving memory. In the program, a union is used to store either a course name or course code at any given time .

The program employs a straightforward linear search function to retrieve student data. The search involves iterating through the array of Student structures, comparing each student's rollNumber with the user-provided rollNumberToFind. If a match is found, the program calls the printStudentDetails function to display the student's data. This ensures accuracy by explicitly checking each record until a match is met. The use of unique roll numbers as search keys prevents duplication errors and enhances search accuracy. Additionally, incorporating a 'found' flag and conditional checks ensures that if no match is found, the user is informed with a 'Student not found' message, maintaining robust error handling .

The student management program's primary limitation concerning data scalability is its static allocation of storage for only 100 student records, which may be insufficient for larger institutions. Additionally, it uses a simple linear search to find records, which may become inefficient as data volume increases. Adaptability issues arise from the use of unions, as they restrict the simultaneous storage of course name and code, limiting flexibility. The fixed data structure design also means any requirement changes, like additional data fields, would necessitate significant code modification .

You might also like