Search This Blog

Simplicity is the ultimate sophistication.” — Leonardo da Vinci
Contact me: sreramk360@gmail.com

Friday 27 March 2015

employee record program



Program to manipulate and store employee records
 

/*
* problem 3
* employee record program
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define NILL 0
#define Set_NULL(employeeRec) (*((unsigned long long int*)&employeeRec) = NILL)
#define Check_If_Null(employeeRec) ( *((unsigned long long int*)&employeeRec) == NILL )
struct EMPLOYEE;
typedef struct EMPLOYEE Employee;

struct EMPLOYEE
{
    char name[50];
    float Salary;
    char designation[100];
    unsigned int empNo;
};

Employee getRecord()
{
    Employee emp;
    printf("\nenter the employee details: ");
    printf("\nenter name: "); fflush(stdin);
    gets(emp.name); fflush(stdin);
    printf("\nenter the salary of the employee: ");
    scanf("%f", &emp.Salary); fflush(stdin);
    printf("\nenter the designation of the employee: ");
    scanf("%s", emp.designation); fflush(stdin);
    printf("\nenter the employee number: ");
    scanf("%d", &emp.empNo);
    return emp;
}

An example program for control statements and conditional statements



An example program for control statements and conditional statements


/*
* An example program for control statements and conditional statements
* problem: write a program to solve the following problem:
*    statement: Let there be an integer array A with n elements. Lets say that
*    we choose m numbers from the n numbers (a number can be chosen twice) and add them
*    to obtain the value x. So algorithmically determine the number of combinations
*    possible to bring about the sum x. Note: the values can be repeated.
**** By K Sreram
*/

program to count the number of prime numbers from 1 to limit



Program to count the number of prime numbers from 1 to limit
 

/*
* program to count the number of prime numbers from 1 to limit
*/
#include <stdio.h>
#include <conio.h>

int checkPrime(int val)
{ // tests if the given number is prime or not
    int i;
    if(val == 1 || val == 0)
        return 0;
    for(i=2; i<=val/2; ++i)
        if((val%i) == 0)
                return 0;
    return 1;
}

int noOfPrimes(int lim)
{// this calls the function checkPrime to test if the number is prime
    // if it is prime, 'count' is incremented.
    int i, count = 0;
    printf("\nlist of prime numbers from 2 to %d", lim);
    for(i=2; i<=lim; ++i)
        if(checkPrime(i))
            {
                ++count;
                printf("\n%d", i);
            }
    return count;
}

int main() /// the runtime complexity of this program is O(N^2)
{
    int lim;
    printf("enter the limit:");
    scanf("%d", &lim);
    printf("number of prime numbers from 2 to %d is %d", lim, noOfPrimes(lim));
    return 0;
}




copyright (c) 2015 K Sreram.
About my blog
 
 

infix to post-fix conversion program




Infix to post-fix conversion program

/*
 infix to postfix conversion program
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <process.h>
#include <ctype.h>
#include <strings.h>
enum BOOL {FALSE, TRUE };
typedef int BOOL;

struct NODE;
typedef struct NODE Node;
typedef Node* PtrNode;
typedef PtrNode Stack;
typedef char DataType;

struct NODE{
    DataType data;
    PtrNode next;
};

BOOL IsEmpty(Stack s)
{
    return ((s!=NULL)&&(s->next == NULL));
}
Stack NewStack()
{
    Stack s = malloc(sizeof(Node));
    s->next = NULL;
    return s;
}

polynomial multiplication and addition implementation



Implementation of polynomial multiplication and addition using linked list


/*
   polynomial multiplication and addition implementation
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum BOOL {FALSE, TRUE};
typedef int BOOL; // the more obvious choice would be char, but int is more
                  // time efficient as it uses a basic memory unit optimal for the
                  // particular processor design, by default

struct NODE;
typedef struct NODE Node;
typedef Node* PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef List Polynomial;
struct polyTerm {
    int degree; // the power of the taken term
    int coeff; // the coefficient of the term
};
typedef struct polyTerm DataType; // this is a default data type

struct NODE{ // single link list node
    DataType data;
    PtrNode next;
};
// vital ones>>>>>>>>>>>>>>>>>>>>>>>>>
PtrNode NewPoly() // returns the List's header
{
    PtrNode tempNode =  malloc(sizeof(Node));
    tempNode->next = NULL;
    return tempNode;
}
Position NewNode()
{
    // to do: add code to create the other parts of
    // the memory to be allocated.
    return malloc(sizeof(Node)); // for old compilers,
    // explicit type conversion like (Node) is needed to be
    //used.
}

implementation of bubble sort algorithm in C



Implementation of bubble sort algorithm in C
 

/*
* implementation of bubble sort algorithm in C
* By K Sreram
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum BOOL {FALSE, TRUE};
typedef int BOOL;

struct NODE;
typedef struct NODE Node;
typedef Node* PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef int DataType;

struct NODE{ // single link list node
    DataType data;
    PtrNode next;
};

PtrNode NewList() // returns the List's header
{
    PtrNode tempNode =  malloc(sizeof(Node));
    tempNode->next = NULL;
    return tempNode;
}
Position NewNode() // creates a new node
{
    return malloc(sizeof(Node));
}

void Delete(PtrNode node)// deletes the node
{
    free(node);
}
BOOL IsLast(List L, Position pos) // checks id the given position is last in the list
{
    return (pos->next == NULL);
}

Program to implement the solution of josephus problem in a form of a game: using circular liked list.



Program to implement the solution of Josephus problem in a form of a game:  using circular liked list


/*
* Program to implement the solution of josephus problem in a form of a game
* using circular liked list.
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum BOOL {FALSE, TRUE};
typedef int BOOL;
struct NODE_dc;
typedef struct NODE_dc Node_dc;
typedef Node_dc* PtrNode_dc;
typedef PtrNode_dc List_dc;
typedef PtrNode_dc Position_dc;

struct Player{
    int index;
    char name[50];
};
typedef struct Player DataType; // this is a default data type

struct NODE_dc{ // single link list node
    DataType data;
    PtrNode_dc next;
    PtrNode_dc previous;
};

PtrNode_dc NewList_dc() // returns the List's header
{
    PtrNode_dc tempNode =  malloc(sizeof(Node_dc));
    tempNode->next = NULL;
    tempNode->previous = NULL;
    return tempNode;
}
Position_dc NewNode_dc()
{
    return malloc(sizeof(Node_dc));
}

void Delete_dc(PtrNode_dc node)
{
    free(node);
}
//***************************************************

implementation example of double linked list (set union and intersection operations)



Implementation example of double linked list (set union and intersection operations)
 

/*
* implementation example of double linked list (set union and intersection operations)
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

enum BOOL {FALSE, TRUE};
typedef int BOOL;
struct NODE_d;
typedef struct NODE_d Node_d;
typedef Node_d* PtrNode_d;
typedef PtrNode_d List_d;
typedef PtrNode_d Position_d;
typedef int DataType; // this is a default data type

struct NODE_d{ // single link list node
    DataType data;
    PtrNode_d next;
    PtrNode_d previous;
};

PtrNode_d NewList_d() // returns the List's header
{
    PtrNode_d tempNode =  malloc(sizeof(Node_d));
    tempNode->next = NULL;
    tempNode->previous = NULL;
    return tempNode;
}
Position_d NewNode_d()
{
    return malloc(sizeof(Node_d)); // for old compilers,
    // explicit type conversion like (Node) is needed to be
    //used.
}

void Delete_d(PtrNode_d node)
{
    free(node);
}

BOOL IsLast_d(List_d L, Position_d pos)
{
    return (pos->next == NULL);
}

BOOL IsEmpty_d(List_d L)
{
    return (L->next == NULL);
}

void Insert_d ( List_d L, DataType data, Position_d pos)
{
    Position_d tempPos;
    tempPos = NewNode_d(); // creates and allocates memory space
    if(tempPos == NULL)
    {
        fprintf(stderr, "error: request for allocation of memory using function \"malloc\" rejected");
        return;
    }
    tempPos->data = data;
    tempPos->next = pos->next;
    tempPos->previous = pos;
    pos->next = tempPos;
    if(!IsLast_d(L, tempPos))
        tempPos->next->previous = tempPos;
}


Position_d Find_d(List_d L, DataType element)
{
    Position_d tempPos = L->next;
    while(tempPos!=NULL && tempPos->data!=element)
        tempPos = tempPos->next;
    return tempPos; // this return NULL by default when it fails to find the
                    // element
}

void DisplayElement_d(DataType element)
{
    printf("%d\n", element);
}
void Display_d (List_d L)
{

    Position_d pos = L->next;
    if(IsEmpty_d(L))
        return;
    while (pos != NULL)
        {
            DisplayElement_d(pos->data);
            pos = pos->next;
        }
}

List_d setIntersect(List_d L1, List_d L2) // this function runs at O(N^2) or to be more precise O(MN)
{
    Position_d pos_top, tempPos;
    List_d L = NewList_d();
    pos_top = L;
    tempPos = L1->next;
    while(tempPos != NULL)
    {
        if(Find_d(L, tempPos->data)==NULL && Find_d(L2, tempPos->data) != NULL)
        {
            Insert_d(L, tempPos->data, pos_top);
            pos_top = pos_top->next;
        }
        tempPos = tempPos->next;
    }

    return L;
}

List_d setUnion(List_d L1, List_d L2) // this function runs at O(N^2) or to be more precise O(MN)
{
    Position_d pos_top, tempPos;
    List_d L = NewList_d();
    pos_top = L;
    tempPos = L1->next;
    while (tempPos != NULL)
    {
        if(Find_d(L, tempPos->data) == NULL)
            { // there is a possibility that an element can be repeated in the same list
                Insert_d(L, tempPos->data, pos_top);
                pos_top = pos_top->next;
            }
        tempPos = tempPos->next;
    }
    tempPos = L2->next;
    while(tempPos != NULL)
    {
        if(Find_d(L, tempPos->data) == NULL)
            { // incrementation takes place only if the data record is not found
                Insert_d(L, tempPos->data, pos_top);
                pos_top = pos_top->next;
            }
        tempPos = tempPos->next;
    }
    return L;
}
void getElements(List_d L, const char* A)
{
    unsigned int i, n;
    int data; Position_d top = L;
    printf("enter the number of elements for %s", A);
    scanf("%d", &n);
    printf("enter the elements of the set %s:", A);
    for(i=0; i<n; i++)
    {
        scanf("%d", &data);
        Insert_d(L, data, top);
        top = top->next;
    }
}
int main()
{
    List_d L1 = NewList_d();
    List_d L2 = NewList_d();
    List_d L_union, L_intersection;
    getElements(L1, "A");
    getElements(L2, "B");
    L_union = setUnion(L1, L2);
    printf("\nunion of A and B:\n");
    Display_d(L_union);
    getch();
    L_intersection = setIntersect(L1, L2);
    printf("\nintersection of A and B:\n");
    Display_d(L_intersection);

    getch();
    return 0;
}




 copyright (c) 2015 K Sreram.
About my blog
 
 

Single linked list general implementation, with header node (employee records)



Single linked list general implementation, with header node (employee records)
 

/*
*  Single linked list general implementation, with header node (employee records)
*/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <strings.h>
enum BOOL {FALSE, TRUE};
typedef int BOOL; // the more obvious choice would be char, but int is more
                  // time efficient as it uses a basic memory unit optimal for the
                  // particular processor design, by default

struct NODE;
struct EMPLOYEE;
typedef struct EMPLOYEE employee;
typedef struct NODE Node;
typedef Node* PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef employee DataType; // this is a default data type

struct EMPLOYEE{
    char SNo[12];
    char name[50];
    int age;
    char designation[50];
    float salary;
};

BOOL compareEquality(char* e1, char* e2)
{
    return (strcmp(e1, e2) == 0);
}

struct NODE{ // single link list node
    DataType data;
    PtrNode next;
};

BOOL IsLast(List L, Position pos)
{
    return (pos->next == NULL);
}
PtrNode NewList() // returns the List's header
{
    PtrNode tempNode =  malloc(sizeof(Node));
    tempNode->next = NULL;
    return tempNode;
}
Position NewNode()
{
    return malloc(sizeof(Node)); // for old compilers,
    // explicit type conversion like (Node) is needed to be
    //used.
}

void Delete(PtrNode node)
{
    free(node);
}

void Insert ( List L, DataType data, Position pos)
{
    Position tempPos;
    tempPos = NewNode(); // creates and allocates memory space
    if(tempPos == NULL)
    {
        fprintf(stderr, "error: request for allocation of memory using function \"malloc\" rejected");
        return;
    }
    tempPos->data = data;
    tempPos->next = pos->next;
    pos->next = tempPos;
}

Position FindPrevious(List L, char element[12]) // by default, this function does not check for the
{                                   // header node which never gets initialised

    Position tempPos = L;
    while(tempPos->next != NULL && !compareEquality(tempPos->next->data.SNo, element)) /// searches for the first occurrence of value 'element'
        tempPos = tempPos->next;
    return tempPos; // returns the node preceding the node containing the value 'element'
}

void DeleteElement(List L, char data[12])
{
    Position tempPos = FindPrevious(L, data); // by all chances, this cannot delete the list
    PtrNode tempNode;
    if(IsLast(L, tempPos)) // element not found
            return;
    tempNode = tempPos->next;
    tempPos->next = tempNode->next;
    Delete(tempNode);

}

Position Find(List L, char element[12])
{
    Position tempPos = L->next;
    while(tempPos!=NULL && !compareEquality(tempPos->data.SNo,element))
        tempPos = tempPos->next;
    return tempPos; // this return NULL by default when it fails to find the
                    // element
}

void DisplayElement(DataType element)
{
   printf("\n\nserial no:%s", element.SNo);
   printf("\nname:%s", element.name);
   printf("\nsalary:%f", element.salary);
   printf("\ndesignation:%s", element.designation);
   printf("\nage:%d", element.age);
}
void Display (List L)
{
    Position pos = L->next;
    if(pos == NULL)
        return;
    while (pos != NULL)
        {
            DisplayElement(pos->data);
            pos = pos->next;
        }
}
employee getRecord()
{
    employee e;
    printf("\n\nserial no:");
    gets(e.SNo);    fflush(stdin);
    printf("name:");
    gets(e.name);    fflush(stdin);
    printf("salary:");
    scanf("%f", &e.salary);    fflush(stdin);
    printf("designation:");
    gets(e.designation);    fflush(stdin);
    printf("age:");
    scanf("%d", &e.age);    fflush(stdin);
    return e;
}
int main()
{
    printf("employee record manipulation\n");
    List L = NewList();
    Position top = L;
    char data[12];
    char ch = '\0';
    while(ch != '5')
    {
        printf("\n1. enter record");
        printf("\n2. find record");
        printf("\n3. delete record");
        printf("\n4. display all records");
        printf("\n5. exit");
        ch = getch();
        if(ch == '1')
            {
                Insert(L, getRecord(), top);
                top = top->next;
            }
        else if (ch == '2')
            {
                printf("\nenter SNo.");
                gets(data);
                DisplayElement(Find(L, data)->data);
            }
        else if(ch=='3')
            {
                printf("\nenter SNo.");
                gets(data);
                DeleteElement(L, data);
                Display(L);
            }
        else if (ch == '4')
            Display(L);
        else if(ch == '5')
            break;
        else
            printf("\ninvalid command");

    }
    return 0;
}




copyright (c) 2015 K Sreram.
About my blog
 
 

Featured post

Why increasing complexity is not good?

“ Simplicity is the ultimate sophistication.” — Leonardo da Vinci Why is complicating things wrong ? - K Sr...