c program 9
c program 9
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
• 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].course.courseName.
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).
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", student.rollNumber);
printf("Name: %s\n", student.name);
printf("Department: %s\n", student.department);
printf("Couse Name: %s\n", student.course.courseName);
printf("Year of Joining: %d\n", student.yearOfJoining);
}
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].course.courseName); // store
course name in
printf("Enter Year of Joining:");
scanf("%d", &students[studentCount].yearOfJoining);
studentCount++;
OUTPUT:
CONCLUSION:
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.