How To Write a C Program to Creating hash table w.r.t employee age, display, delete and find average of salaries between the given age limits in C Programming Language ?
#include <stdio.h>Tags: Hash table and operations on it, hash table in operating system, hash tables java, hash tables data structures, hash tables python, hash tables tutorial, hash tables powershell, hash tables ppt, hash tables in sql server.
#include <stdlib.h>
#include <string.h>
#define SIZE 10
struct hash {
char name[20];
float salary;
int age;
struct hash* ptr;
};
een
struct hash *DICT[SIZE];
void makenull()
{
int i;
for(i=0; i<SIZE; i++)
{
DICT[i]=NULL; }
}
int hash(int age)
{
return(age%SIZE);
}
void insert()
{
char nam[20];
float sal;
int ag,i,index;
struct hash *oldptr;
printf("Enter the employee name\n");
scanf("%s",nam);
printf("Enter the salary\n");
scanf("%f", &sal);
printf("Enter the age\n");
scanf("%d",&ag);
index=hash(ag);
oldptr = DICT[index];
DICT[index]= (struct hash *) malloc(sizeof (struct hash));
// DICT[index]->name[20]=nam[20];
strcpy(DICT[index]->name, nam);
DICT[index]->salary=sal;
DICT[index]->age=ag;
DICT[index]->ptr=oldptr;
}
void display()
{
int index,age;
struct hash *head;
printf("Enter the employee age to display\n");
scanf("%d",&age);
index=hash(age);
head= DICT[index];
while (head != NULL)
{
printf("Name: %s\n",head->name);
printf("Age: %d\n", head->age);
printf("Salary: %f\n\n", head->salary);
head = head->ptr;
}
}
void delete()
{
int index,age;
char ename[20];
struct hash *head, *oldhead;
printf("Enter the employees' age to be deleted\n");
scanf("%d",&age);
index=hash(age);
head=DICT[index];
printf("Enter employee name to be deleted\n");
scanf("%s",ename);
while ( head != NULL )
{
if (! strcmp (head->name, ename) )
{
oldhead=head->ptr;
DICT[index]=oldhead;
free(head);
}
head=head->ptr;
}
}
void avg()
{
int age1,age2, index1;
struct hash *head;
float asal;
printf("Calculating average of salaries\n");
printf("Enter range of employee age\n");
printf("From (Enter starting age)\n");
scanf("%d", &age1);
printf("To (Enter ending age)\n");
scanf("%d", &age2);
while (age1 <= age2)
{
index1=hash(age1);
head=DICT[index1];
while( head !=NULL)
{
asal=asal+head->salary;
head=head->ptr;
}
age1++;
}
printf("The average of salary is: %f", asal);
}
int main ()
{
int num,i,choice;
printf("Enter the number of employee details you want to enter\n");
scanf("%d",&num);
makenull();
for(i=0; i<num; i++)
{
insert();
}
printf("What do you want to do now\n");
do {
printf("Enter your choice\n");
printf("1->Display\n2->Search and Delete\n3->Find average salary\n0->Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: display();
break;
case 2: delete();
break;
case 3: avg();
break;
case 0: break;
}
} while ( choice != 0);
return 0;
}
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount