0% found this document useful (0 votes)
8 views

c program 9

Uploaded by

adityapro87
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

c program 9

Uploaded by

adityapro87
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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].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).

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", 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++;

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.

You might also like