Ad Code

Responsive Advertisement

DS File handling practice question using C language

 Questions 

 1) Write a C program to read the data from the file “employee.txt” which contains empno and empname and sort the data on names alphabetically (use strcmp) using Bubble Sort.

employee.txt file =>

1 Raj 

2 Add 

3 Dog

Output will be =>

2 Add 

3 Dog 

1 Raj 

------------------------------------------

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

struct employee

{

    int id;

    char name[30];

};

int main()

{

    FILE *ptr;

    struct employee emp[10];

    int i = 0, j, size;

    char ch;

    ptr = fopen("employee.txt", "r");

    while (ch != EOF)

    {

        fscanf(ptr, "%d %s", &emp[i].id, &emp[i].name);

        ch = fgetc(ptr);

        i++;

        printf(" done \n");

    }

    size = i;

    bubbleSort(emp, size);

    ptr = fopen("employee1.txt", "w");

    for (i = 0; i < size; i++)

    {

        fprintf(ptr, "%d %s \n", emp[i].id, emp[i].name);

        printf("print done \n");

    }

    fclose(ptr);

    getch();

    return 0;

}

void bubbleSort(struct employee arr[], int size)

{

    struct employee temp;

    int i, j;

    for (i = 1; i < size; i++)

    {

        for (j = 0; j < size - i; j++)

        {

            if (strcmp(arr[j].name, arr[j + 1].name) > 0)

            {

                temp = arr[j];

                arr[j] = arr[j + 1];

                arr[j + 1] = temp;

            }

        }

    }

}

===================================================================

 2) Write a C program to read the data from the file “person.txt” which contains personno and personage and sort the data on age in ascending order using insertion Sort / Selection Sort.

person.txt file =>

1 25

2 16

3 22

4 27

Output will be =>

1 16

2 22

3 25

4 27

-----------------------------------------------------------------

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

    FILE *fptr;

    int p_no[10], p_age[10];

    int i, size;

    char ch;

    fptr = fopen("person.txt", "r");

    if (fptr == NULL)

    {

        printf("Error opening file \n");

        return 1;

    }

    i = 0;

    while (ch != EOF)

    {

        fscanf(fptr, "%d", &p_no[i]);

        fscanf(fptr, "%d", &p_age[i]);

        i++;

        ch = fgetc(fptr);

    }

    size = i - 1;

    insertionSort(p_age, size);

    fptr = fopen("p.txt", "w");

    if (fptr == NULL)

    {

        printf("Error file cannot open\n");

    }

    for (i = 0; i <= size; i++)

    {

        fprintf(fptr, "%d %d\n", p_no[i], p_age[i]);

        printf("done\n");

    }

    fclose(fptr);

    getch();

    return 0;

}

void insertionSort(int arr[], int size)

{

    int i, j, key;

    for (i = 1; i < size; i++)

    {

        key = arr[i];

        for (j = i - 1; j >= 0 && arr[j] > key; j--)

        {

            arr[j + 1] = arr[j];

        }

        arr[j + 1] = key;

    }

}

=====================================================
3.Write a C program to read the data from the file “person.txt” which contains person no,
name and person age and sort the data on age in ascending order using merge Sort.

merge Sort : 
answer =>


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define Max_Len 20

struct person
{
    int per_no;
    char name[Max_Len];
    int age;
};

void mergeSort(struct person arr[], int low, int high);
void merge(struct person arr[], int low, int mid, int high);

int main()
{
    FILE *fptr;
    int i, size;
    struct person p1[Max_Len];
    fptr = fopen("01_person.txt", "r");

    if (fptr == NULL)
    {
        printf("File not Found !");
        return 0;
    }

    for (i = 0; !feof(fptr); i++)
    {
        fscanf(fptr, "%d %s %d", &p1[i].per_no, &p1[i].name, &p1[i].age);
        printf("%d %s %d \n", p1[i].per_no, p1[i].name, p1[i].age);
    }

    size = i;

    mergeSort(p1, 0, size - 1);

    printf("\nSorted Array is : \n");
    for (i = 0; i < size; i++)
    {
        printf("%d %s %d \n", p1[i].per_no, p1[i].name, p1[i].age);
    }

    fptr = fopen("01_person.txt", "a");
    fprintf(fptr, "\n Sorted Records is :\n");
    for (int j = 0; j < size; j++)
    {
        fprintf(fptr, "%d %s %d \n", p1[j].per_no, p1[j].name, p1[j].age);
    }

    fclose(fptr);
    getch();
    return 0;
}

void mergeSort(struct person arr[], int low, int high)
{
    int mid;
    if (low < high)
    {
        mid = (low + high) / 2;
        mergeSort(arr, low, mid);
        mergeSort(arr, mid + 1, high);
        merge(arr, low, mid, high);
    }
}

void merge(struct person arr[], int low, int mid, int high)
{
    int i, j, k;
    struct person cpy[Max_Len];
    i = low;
    j = mid + 1;
    k = 0;

    while (i <= mid && j <= high)
    {
        if (arr[i].age < arr[j].age)
        {
            cpy[k++] = arr[i++];
        }
        else
        {
            cpy[k++] = arr[j++];
        }
    }

    while (i <= mid)
    {
        cpy[k++] = arr[i++];
    }
    while (j <= high)
    {
        cpy[k++] = arr[j++];
    }

    for (i = low, k = 0; i <= high; k++, i++)
    {
        arr[i] = cpy[k];
    }
}

===========================================================

4.Write a C program to read the data from the file “student.txt” which contains rollno, name
and age and sort the data on age in ascending order using quick Sort.

quick Sort.

answer => 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define Max_Len 20

struct student
{
    int roll_no;
    char name[Max_Len];
    int age;
};

// Function Declaration
void quickSort(struct student arr[], int low, int high);
int partitionArray(struct student arr[], int low, int high);

int main()
{
    FILE *fp;
    int i, size;
    struct student s1[Max_Len];

    fp = fopen("02_student.txt", "r");

    if (fp == NULL)
    {
        printf("File does not exist\n");
        return 1;
    }
    for (i = 0; !feof(fp); i++)
    {
        fscanf(fp, "%d %s %d", &s1[i].roll_no, &s1[i].name, &s1[i].age);
        printf("%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);
    }
    size = i;

    quickSort(s1, 0, size - 1);

    printf("Sorted Array is : \n");
    for (i = 0; i < size; i++)
    {
        printf("%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);
    }

    fp = fopen("02_student.txt", "a");
    fprintf(fp, "\n Sorted Elements is : \n");
    for (i = 0; i < size; i++)
    {
        fprintf(fp, "%d %s %d\n", s1[i].roll_no, s1[i].name, s1[i].age);
    }
    fclose(fp);
    getch();
    return 0;
}
void quickSort(struct student arr[], int low, int high)
{
    int j;

    if (low < high)
    {
        j = partitionArray(arr, low, high);
        quickSort(arr, low, j - 1);
        quickSort(arr, j + 1, high);
    }
}

int partitionArray(struct student arr[], int low, int high)
{
    int i, j;
    struct student pivot, temp;

    pivot = arr[low];
    i = low + 1;
    j = high;

    do
    {
        while (arr[i].age < pivot.age && i <= high)
        {
            i++;
        }

        while (arr[j].age > pivot.age && j >= low)
        {
            j--;
        }

        if (i < j)
        {
            temp = arr[j];
            arr[j] = arr[i];
            arr[i] = temp;
        }

    } while (i < j);

    arr[low] = arr[j];
    arr[j] = pivot;
    return j;
}

=============================================================

Post a Comment

0 Comments

Ad Code

Responsive Advertisement