0% found this document useful (0 votes)
6 views5 pages

CP Exp 18

The document outlines an assignment aimed at demonstrating the use of structures and unions to manage student data, including roll number, name, department, course, and year of joining. It provides a detailed algorithm for inputting student information, searching for a student by roll number, and printing the relevant data. Additionally, it includes sample C code implementing both a structure and a union for handling student data.

Uploaded by

sohanrai096
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)
6 views5 pages

CP Exp 18

The document outlines an assignment aimed at demonstrating the use of structures and unions to manage student data, including roll number, name, department, course, and year of joining. It provides a detailed algorithm for inputting student information, searching for a student by roll number, and printing the relevant data. Additionally, it includes sample C code implementing both a structure and a union for handling student data.

Uploaded by

sohanrai096
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
You are on page 1/ 5

ASSIGNMENT 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
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:

Structure

#include <stdio.h>
#include <string.h>

// Define a structure for student data


struct Student {
int rollNumber;
char name[100];
char department[50];
char course[50];
int yearOfJoining;
};

// Function to print student data


void printStudentData(struct Student s) {
printf("\nStudent Data:\n");
printf("Roll Number: %d\n", s.rollNumber);
printf("Name: %s\n", s.name);
printf("Department: %s\n", s.department);
printf("Course: %s\n", s.course);
printf("Year of Joining: %d\n", s.yearOfJoining);
}

int main() {
struct Student students[5]; // Array to store data for 5 students
int n = 5; // Total number of students
int rollNumberToSearch;

// Input student data


for (int i = 0; i < n; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
getchar(); // To consume the newline character left by scanf
printf("Name: ");
fgets(students[i].name, sizeof(students[i].name), stdin);
students[i].name[strcspn(students[i].name, "\n")] = 0; // Remove newline
character

printf("Department: ");
fgets(students[i].department, sizeof(students[i].department), stdin);
students[i].department[strcspn(students[i].department, "\n")] = 0;

printf("Course: ");
fgets(students[i].course, sizeof(students[i].course), stdin);
students[i].course[strcspn(students[i].course, "\n")] = 0;

printf("Year of Joining: ");


scanf("%d", &students[i].yearOfJoining);
getchar(); // To consume the newline character left by scanf
}

// Input roll number to search


printf("\nEnter roll number to search: ");
scanf("%d", &rollNumberToSearch);

// Search and print student data based on roll number


int found = 0;
for (int i = 0; i < n; i++) {
if (students[i].rollNumber == rollNumberToSearch) {
printStudentData(students[i]);
found = 1;
break;
}
}

if (!found) {
printf("No student found with roll number %d.\n", rollNumberToSearch);
}

return 0;
}

UNION

#include <stdio.h>
#include <string.h>

// Define a union to store the student data


union StudentUnion {
int rollNumber;
char name[100];
char department[50];
char course[50];
int yearOfJoining;
};

// Function to print student data (only demonstrates one data at a time)


void printStudentData(union StudentUnion s) {
printf("\nStudent Data:\n");
printf("Roll Number: %d\n", s.rollNumber);
printf("Name: %s\n", s.name);
printf("Department: %s\n", s.department);
printf("Course: %s\n", s.course);
printf("Year of Joining: %d\n", s.yearOfJoining);
}

int main() {
// Here, we use a union for one student's data
union StudentUnion student;

// Assign data to the union (but only one field can be used at a time)
student.rollNumber = 101;
strcpy(student.name, "John Doe");
strcpy(student.department, "Computer Science");
strcpy(student.course, "B.Tech");
student.yearOfJoining = 2020;

// Print data (we can only access one field at a time)


printStudentData(student);

return 0;
}
Output:

Input
Output

You might also like