0% found this document useful (0 votes)
5 views8 pages

Practical8 2024 PF

Uploaded by

Jahnavi Shah
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)
5 views8 pages

Practical8 2024 PF

Uploaded by

Jahnavi Shah
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/ 8

FR.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


Academic Term: September -December 2024
Subject Name: Programming Fundamentals

Course Code: ESC11CEL03


Class: F.E. (Sem –I)

Name of Student Tanmay Mahajan


Lab Experiment No. 8 Roll No. 10671
Date of Performance: Date of Submission:
Expt. Title a) Write a program to create a structure Employee with members as
employee id, name, salary, date of joining and age. Sorted it on
the basis of name in alphabetical order.
b) Write a program to create a structure Employee with members as
employee id, name, salary, date of joining and age. Sorted it on
the basis of Salary in descending order.

CO Mapping: CO4

Problem Definition: Write a program to create a structure Employee with members as employee id, name,
salary and date_of_joining and age. Use nested structure to get the date of joining of an employee. Read
information for ‘n’ employees. Display employee details in the format
Name EMP_ID Salary Date of Joining Age

Objective of the Experiment


1. Understanding Structures.
2. Understanding nested structure.
Theory:
A structure is a collection of variables under a single name. These variables can be of different types, and each
has a name which is used to select it from structure. Therefore, structure is a convenient way of grouping
together several pieces of related information.
Declaring Structures:

struct structname struct structname


{ {
datatype member1; datatype member1;
datatype member2; …. datatype member2; ….
} structure_var1,structure_var2, …. ; OR };
struct struct_name structure_var1,structure_var2,
….;
Example: struct student
struct student {
{ char name[10];
char name[10]; int age;
int age; };
} s1,s2; struct student s1,s2;

Accessing single structure element:

struct_var.member=value;
Example:
s1.name=”Alice”;

Suggested Algorithm Alternate Algorithm


[Due weightage in grading to be given for students
suggesting alternate algorithm]
1. Start
2. Declare a structure to store date of joining Algorithm for Sorting Employee Data by
3. Declare structure ‘employee’ to store id, name,
salary and date of joining and age. Name
4. Allocate memory space to store information for
employees by: employee e[SIZE] ; 1. Start
Where SIZE is symbolic constant. Begin the program.
5. Read data for 3 employees.
6. Display employee details. 2. Input Number of Employees
7. Sort as per alphabetical order.
○ Input the number of employees, n.
3. Input Employee Details

○ Create an array of Employee


structures.
○ For each employee from 1 to n:
■ Input the id, name,
salary,
date_of_joining, and
age.
4. Sort Employees by Name

○ Use the sortEmployees function


to sort the array of employees based
on their name using string
comparison (strcmp).
5. Output Sorted Employee Details

○ Print the details of each employee


after sorting by name.
6. End
End the program.
Source code for the implementation.
#include <stdio.h>
#include <string.h>

struct Employee {
int id;
char name[50];
float salary;
char date_of_joining[11];
int age;
};

void sortEmployees(struct Employee employees[], int n) {


struct Employee temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (strcmp(employees[i].name, employees[j].name) > 0) {
temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
}

int main() {
int n;
printf("Enter number of employees: ");
scanf("%d", &n);

struct Employee employees[n];


for (int i = 0; i < n; i++) {
printf("Enter details for employee %d\n", i + 1);
printf("ID: ");
scanf("%d", &employees[i].id);
printf("Name: ");
scanf("%s", employees[i].name);
printf("Salary: ");
scanf("%f", &employees[i].salary);
printf("Date of Joining (YYYY-MM-DD): ");
scanf("%s", employees[i].date_of_joining);
printf("Age: ");
scanf("%d", &employees[i].age);
}

sortEmployees(employees, n);

printf("\nEmployees sorted by name:\n");


for (int i = 0; i < n; i++) {
printf("ID: %d, Name: %s, Salary: %.2f, Date of Joining: %s,
Age: %d\n",
employees[i].id, employees[i].name, employees[i].salary,
employees[i].date_of_joining, employees[i].age);
}

return 0;
}
b) Definition: Write a program to create a structure Employee with members as
employee id, name, salary, date of joining and age. Sort it on the basis of Salary in
descending order.

Source code for the implementation.


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

struct Date {
int day;
int month;
int year;
};

struct Employee {
int id;
char name[50];
float salary;
struct Date joiningDate;
int age;
};
void sortEmployeesBySalary(struct Employee employees[], int n) {
struct Employee temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (employees[i].salary < employees[j].salary) {
temp = employees[i];
employees[i] = employees[j];
employees[j] = temp;
}
}
}
}

int main() {
int n = 3;
struct Employee employees[3] = {
{1, "Alice", 50000, {1, 1, 2020}, 30},
{2, "Bob", 60000, {1, 2, 2019}, 28},
{3, "Charlie", 55000, {1, 3, 2021}, 25}
};

sortEmployeesBySalary(employees, n);

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


printf("ID: %d, Name: %s, Salary: %.2f, Joining Date:
%02d/%02d/%04d, Age: %d\n",
employees[i].id, employees[i].name, employees[i].salary,
employees[i].joiningDate.day,
employees[i].joiningDate.month, employees[i].joiningDate.year,
employees[i].age);
}

return 0;
}
The program was tested for different sets of inputs.
Program is working is SATISFACTORY NOT SATISFACTORY ( Tick appropriate outcome)

Evaluation:

On time Completion and Knowledge of the topic Implementation and Total (10)
Submission (2) (4) Output (4)

Date & Signature of teacher:

You might also like