Struct Point Files
Struct Point Files
Assignment:
1. Define a datatype for Employee with members Emp_Id,
Emp_Name, DOB, Age, Address, Dept, Basic_Salary,
Allowance[3], Deduction[2], Gross_Salary and Net_Salary.
Define DOB with members namely Day, Month and Year. Define
Address with membersnamely Door_No, Street, Area, City and
Pincode. Let the Allowance array consists of
Dearness_Allowance, HRA and Medical_Allowance. The
Deduction array consists of PFand Income_Tax. Write a C
program using structures to perform the following operations.
a. Write a function to create an employee database using an
array of structures with5 employees belonging to 2
departments by passing the structure name and number of
employees as arguments to the functions. Get the input from
users onlyfor Emp_Id, Emp_Name, DOB, Address, Dept
and Basic_Salary.
b. Write a function to find the Age of an employee using DOB
and the current Dateby passing DOB as argument.
c. Write a function for calculating Allowances using the
following
i. Dearness_Allowance = 42% of Basic_Salary
ii. HRA = 10% of Basic_Salary
iii. Medical_Allowance = 15% of Basic_Salary
d. Write a function for calculating the Gross_Salary as
Basic_Salary + Allowances
e. Write a function for calculating Deductions using the
following
i. PF = 12% of Basic_Salary
ii. Income_Tax = 20% of Gross_Salary
f. Write a function for calculating the Net_Salary as
Basic_Salary + Allowances -
Deductions
g. Write a function to search for an employee based on the
Emp_Id and displayhis/her payslip.
h. Write a function to display the department that pays the
highest salary to an employee
CODE:
#include <stdio.h> #include
<stdlib.h> #include
<string.h>
int main() {
int currentYear = 2024; // Current year
struct Employee employees[5]; // Array of employees
return 0;
}
SAMPLE OUTPUT:
2. Modify Qn 1 by using files to perform the following
a. Read the details namely Emp_Id, Emp_Name, DOB,
Address, Dept andBasic_Salary of 5 employees and store
them in a file.
b. Access the file sequentially to get the employee details for
computing Gross andNet salaries as mentioned in Qn 1.
c. Create 5 files consisting of the payslips of 5 employees.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Calculate allowances, deductions, gross salary, and net salary for each employee
for (i = 0; i < 5; i++) {
calculateAllowances(&employees[i]);
calculateGrossSalary(&employees[i]);
calculateDeductions(&employees[i]);
calculateNetSalary(&employees[i]);
}
return 0;
}
SAMPLE OUTPUT:
3.Write a C program to input multiple lines of text and to determine the
number of vowels,consonants, digits, whitespace characters and other
characters for each line and finally tofind the average number of vowels per
line, consonants per line etc. Store the multiple lines of text whose maximum
length is unspecified. Maintain a pointer to each string within a one-
dimensional array of pointers.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Function prototypes
void analyzeLine(char *line, int *vowels, int *consonants, int *digits, int
*whitespaces, int *others);
int main() {
char **lines = NULL;
char buffer[1024];
int lineCount = 0;
printf("Enter lines of text (Press Enter on a blank line to stop):\n"); while (1)
{
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
break;
}
if (buffer[0] == '\n') {
break;
}
totalVowels += vowels;
totalConsonants += consonants;
totalDigits += digits;
totalWhitespaces += whitespaces;
totalOthers += others;
if (lineCount > 0) {
calculateAverages(lineCount, totalVowels, totalConsonants, totalDigits,
totalWhitespaces, totalOthers);
}
return 0;
}
void analyzeLine(char *line, int *vowels, int *consonants, int *digits, int
*whitespaces, int *others) {
for (int i = 0; line[i] != '\0'; i++) { char
ch = line[i];
if (isalpha(ch)) {
if (strchr("AEIOUaeiou", ch)) {
(*vowels)++;
} else {
(*consonants)++;
}
} else if (isdigit(ch)) {
(*digits)++;
} else if (isspace(ch)) {
(*whitespaces)++;
} else {
(*others)++;
}
}
}
SAMPLE OUTPUT:
4.Write an interactive C program to maintain a list of names, addresses and
telephone numbers. Store the information as records in a file by representing
each record as a structure. Perform the following operations:
Note: Use fscanf and fprintf functions for reading and writing to the file.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
#define MAX_ADDRESS_LENGTH 100
#define MAX_PHONE_LENGTH 15
struct Record {
char name[MAX_NAME_LENGTH];
char address[MAX_ADDRESS_LENGTH]; char
phone[MAX_PHONE_LENGTH];
};
// Function prototypes
void addRecord(FILE *file);
void retrieveRecord(FILE *file, const char *name); void
listRecords(FILE *file);
int main() {
FILE *file = fopen("records.txt", "a+"); // Open file for appending and
reading
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
int choice;
char name[MAX_NAME_LENGTH];
do {
printf("\n1. Add a new record\n");
printf("2. Retrieve record by name\n");
printf("3. List all records\n"); printf("4.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addRecord(file);
break;
case 2:
printf("Enter name to retrieve: ");
scanf("%s", name); retrieveRecord(file,
name);
break;
case 3:
listRecords(file);
break;
case 4:
printf("Exiting...\n"); break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
fclose(file);
return 0;
}
if (!found) {
printf("Record not found for %s.\n", name);
}
}
printf("List of records:\n");
while (fscanf(file, "%s %s %s", record.name, record.address,
record.phone) != EOF) {
printf("Name: %s, Address: %s, Phone: %s\n", record.name,
record.address, record.phone);
}
}
SAMPLE OUTPUT:
5. Modify 2 by using fread and fwrite functions for reading and writing.
Perform the following operations:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EMPLOYEES 10
#define FILENAME "employee_details.bin"
fclose(fp);
}
if (found) {
fseek(fp, -sizeof(struct Employee), SEEK_CUR);
struct Employee emptyEmp = {0}; // Empty employee record
fwrite(&emptyEmp, sizeof(struct Employee), 1, fp);
} else {
printf("Employee not found with name %s\n", name);
}
fclose(fp);
}
fclose(fp);
}
int main() {
// Sample employee records
struct Employee employees[MAX_EMPLOYEES] = {
{101, "John Doe", {15, 5, 1990}, {"123", "Main St", "Downtown", "New
York", 10001}, "Sales", 5000.00},
{102, "Jane Smith", {20, 7, 1985}, {"456", "Elm St", "Uptown", "New
York", 10002}, "HR", 6000.00},
{103, "Michael Johnson", {10, 10, 1992}, {"789", "Oak St", "Midtown",
"New York", 10003}, "Sales", 5500.00},
{104, "Emily Davis", {5, 2, 1988}, {"101", "Pine St", "Downtown", "New
York", 10004}, "IT", 7000.00},
{105, "David Wilson", {25, 12, 1995}, {"202", "Maple St", "Suburb",
"New York", 10005}, "HR", 6200.00}
};
return 0;
}
SAMPLE OUTPUT:
Learning Outcome:
● Using structures
● Passing structures to a function
● Using pointers
● Using Files
I am able to adapt to the following best practices
● Modular programming and incremental programming
● Using user defined data types
● Multi-file program with user defined header file