Experiment Onwards - Merged
Experiment Onwards - Merged
EXPERIMENT NO. 1
Aim of the experiment: To create a text file, to write data to it, and then read and display the
content from the file.
Software Required:
Theory:
File handling is a crucial concept in C programming that allows users to store data in a file and
retrieve it later. The two most common functions used in file handling are:
• fopen (): used to open a file.
• fprintf (), fputs (): used to write data into a file.
• fscanf (), fgets (): used to read data from a file.
• fseek(), rewind(): used to move to a specific location in a file.
There are two modes for file operations:
1. Write mode (“w”): if the file exists, it overwrites the data. If the file does not exist, it
creates a new file.
2. Read mode (“r”): it reads the data from the file.
Program Code:
1
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Results/ Output:
Conclusion:
In this experiment, we explored the file handling process in C. In this experiment, we successfully
implemented a C program to create a text file, write data to it, and then read and display the
contents of the file in the console. Through this, we learned how to manage files in C, ensuring
efficient data storage and retrieval.
2
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
EXPERIMENT NO. 2
Aim of the experiment: To merge the contents of two files and write into third file.
Software Required:
Theory:
File handling is an essential feature in C programming that allows programs to perform operations
such as creating, reading, writing, and modifying files stored on disk. The advantages of file
merging are:
• Data Consolidation: Merging files allows combining multiple data sources into one,
making it easier to handle large datasets.
• Efficient Memory Use: Since the program reads and writes one character at a time,
memory usage is minimal, even with large files.
• Real-World Application: This technique is used in various real-world scenarios, such as
consolidating logs, data streams, and backups.
Program Code:
3
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
Conclusion:
This experiment demonstrates the use of file handling in C to merge the contents of two files into
a third file. The program effectively uses file pointers and functions like fgetc() and fputc() to
perform reading and writing operations, highlighting how multiple files can be processed
simultaneously in C. This experiment provides a solid foundation for working with file
input/output in more complex real-world applications, such as log processing, file merging, and
data management.
4
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
EXPERIMENT NO. 3
Aim of the experiment: To copy the contents of one file to another, demonstrating reading and
writing file operations.
Software Required:
Theory:
File handling is one of the most critical operations in C programming, enabling the program to
create, modify, read, and write files. A file is essentially a space on the disk where data is stored.
File operations allow persistent storage of data, unlike variables which only retain information
while the program is running. The basic operations related to file handling are:
• Opening a File: Files are opened using the fopen() function. You need to specify the file
name and the mode (read, write, append, etc.) in which the file should be opened.
• Reading from a File: The fgetc() function is used to read a single character from a file.
Each time it’s called, it reads the next character in the file.
• Writing to a File: The fputc() function is used to write a single character to a file.
• Closing a File: Once file operations are completed, it is important to close the file using
fclose() to release the resources associated with the file.
Program Code:
5
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
Conclusion:
In this experiment, we successfully implemented a program that copies the contents of one file to
another using basic file handling operations in C. The experiment demonstrated the use of
important file I/O functions such as fopen(), fgetc(), fputc(), and fclose(), providing a clear
understanding of how to open, read from, write to, and close files in C.
Through this program, we learned how to handle files by reading data character-by-character from
a source file and writing it to a destination file. This method can be extended to process larger files
efficiently, as it reads and writes data sequentially without consuming a lot of memory.
6
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
EXPERIMENT NO. 4
Aim of the experiment: To create a structure to store information about a student (name, age, and
grade) and implement functions to input and display student data. To implement an array of
structures to manage a list of students, including operations to sort the list based on grades.
Software Required:
Theory:
Structures in C allow the grouping of different data types under a single entity. This feature enables
the management of related data, such as student records, by creating custom data types that can
handle complex data structures like arrays of structures. A structure can be declared using the struct
keyword. Here, name is an array of characters (a string), age is an integer, and grade is a floating-
point number. This structure holds all the necessary information about a student. Sorting the list
based on grades requires comparing the grade field of each student and rearranging the array
accordingly.
Program Code:
7
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
8
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
Conclusion:
This experiment demonstrates how structures in C can be used to manage complex data, such as
student information. By using arrays of structures and implementing functions to input, display,
and sort data, we can efficiently manage and manipulate records of multiple students. Sorting the
list by grades gives us a real-world use case for managing student data in educational applications,
showcasing the power of advanced C programming concepts like structures and array
manipulation.
9
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
EXPERIMENT NO. 5
Aim of the Experiment: To implement a structure containing a union that can store different
types of employee data (full-time, part-time) based on employee type.
Software Required:
Theory:
In C programming, structures and unions are both user-defined data types that allow the grouping
of different data types. While structures store all member variables together, unions allow different
data types to share the same memory space, storing only one value at a time. This feature can be
used to optimize memory usage when handling different types of data for different categories of
employees, such as full-time and part-time employees.
A structure in C is a collection of variables of different data types grouped together under a single
name. Each member of the structure is stored at a different memory location.
A union is similar to a structure in syntax but differs in how it stores data. A union allows different
data members to share the same memory space, meaning only one member can hold a value at any
time. The size of the union is determined by the largest member.
Program Code:
10
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/ Output:
11
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Conclusion:
This experiment demonstrates the usage of structures and unions in C programming to efficiently
store and manage different types of employee data. The union ensures that memory is used
optimally by storing only the relevant details for full-time or part-time employees, while the
structure organizes the data logically. This technique can be applied in real-world scenarios for
managing diverse employee records in various industries.
12
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
EXPERIMENT NO. 6
Aim of the Experiment: To develop a program to sort an array using pointer notation instead of
array indexing.
Software Required:
Theory:
Pointers are used to traverse and manipulate the elements of the array, making the code more
flexible and efficient in certain low-level operations. Pointers can be incremented and
decremented, allowing them to point to different elements of the array without needing to use array
indices. Instead, the pointer moves across the array by incrementing its address. To sort the array,
you compare elements using pointers and swap them accordingly by dereferencing the pointer
addresses. This avoids the need for array indexing and directly manipulates the memory location
of each element.
Program Code:
13
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
Conclusion:
This program demonstrates how to sort an array using pointer notation instead of array indexing.
It applies a simple sorting algorithm (Selection Sort) to arrange the elements of an array in
ascending order. The sorting logic compares elements at different memory addresses using
dereferencing and swaps them if necessary. This is done in a nested loop to ensure that every
element is compared to the others and positioned correctly. By directly manipulating the memory
addresses using pointers, the program effectively demonstrates how sorting can be achieved
without relying on traditional array notation, showcasing C's ability to interact closely with system
memory.
14
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Experiment No. 7
Aim of the Experiment: To manage a dynamic list of students using pointers to structures and
perform operations like adding, deleting and modifying student records.
Software Required:
Theory:
Structures allow grouping different data types (e.g., int, char, float) under one entity, making it
easier to manage related data, like a student record. Structures allow the bundling of related data
types. Dynamic memory allocation (using functions like malloc() and realloc()) enables creating
flexible, resizable arrays or lists that can grow or shrink at runtime, optimizing memory usage
when managing dynamic datasets. The program dynamically allocates memory for storing student
data. This is necessary because the number of students is not fixed, and memory allocation must
change as students are added or deleted.
Program Code:
15
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
16
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
17
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Conclusion:
This program efficiently manages student records using dynamic memory and a structure-based
approach. It demonstrates how to perform CRUD (Create, Read, Update, Delete) operations on
student data with proper memory management techniques. The use of functions for each operation
ensures code modularity, reusability, and clarity. By storing student information in a structure, the
program neatly organizes complex data and provides an easy way to expand the structure’s
functionality. The program is scalable as it can handle any number of students dynamically, only
limited by system memory.
18
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Experiment No. 8
Aim of the Experiment: To implement a singly linked list with operations to insert, delete, and
traverse nodes.
Software Required:
Theory:
A linked list is a dynamic data structure where elements (nodes) are connected using pointers,
providing efficient memory use and easy insertion and deletion without shifting elements, unlike
arrays. A singly linked list data structure, is a collection of nodes where each node contains two
parts: an integer data value and a pointer to the next node in the list. New nodes can be added to
the end of the linked list and nodes can also be deleted based on their data value. The entire linked
list can be traversed to display the stored data.
Program Code:
19
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
20
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
21
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Conclusion:
This program provides an efficient implementation of a singly linked list in C, demonstrating the insertion,
deletion, and traversal of nodes. By using dynamic memory allocation (malloc()), the program allocates
memory only when new nodes are added, avoiding memory wastage. Inserting or deleting nodes in a linked
list is handled efficiently without the need to shift elements, unlike in arrays. This makes linked lists highly
suitable for dynamic datasets. Linked lists allow the list to grow or shrink as needed, making them more
flexible than fixed-size arrays.
22
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Experiment No. 9
Aim of the Experiment: To create a circular linked list where the last node points to the first node,
and implement traversal to display the list in circular fashion.
Software Required:
Theory:
A circular linked list, is a variation of the singly linked list where the last node points back to the
head, forming a circular structure. A new node can be added at the end of the circular linked list.
If the list is empty, the new node becomes the head and points to itself. If the list already contains
nodes, the last node is updated to point to the newly added node, which in turn points to the head.
This allows continuous traversal from any node in the list. Memory for each node is allocated
dynamically using malloc(), allowing the list to grow as needed. This ensures efficient memory
use since nodes are only created when necessary.
Program Code:
23
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
24
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Conclusion:
Circular linked lists are especially useful in applications where continuous looping through data is required.
By having the last node point back to the head, circular linked lists enable endless traversal without needing
a null pointer to signify the end. This feature is useful for scenarios requiring repeated iteration through a
dataset. The program's structure is scalable, allowing easy insertion of nodes at the end of the list and
providing a flexible way to manage dynamic data without the fixed size constraints of arrays.
25
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Experiment No. 10
Aim of the Experiment: To implement stack and queue using linked list.
Software Required:
Theory:
A stack follows the Last-In-First-Out (LIFO) principle. The push() function adds a node to the top
of the stack, and pop() removes the top node. The linked list allows for dynamic stack growth and
shrinkage. Some of the important commands are:
A queue follows the First-In-First-Out (FIFO) principle. The queue uses two pointers (front and
rear) for insertion and deletion. Some of the important commands are:
Program Code:
26
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
27
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
28
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Result/Output:
29
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
30
Department of ECE, DSATM
Ability Enhancement Course 23CECE37
Conclusion:
This program effectively demonstrates the use of linked lists to implement a stack and a queue with dynamic
memory allocation. By using linked lists, the program can handle any number of elements in the stack and
queue, limited only by available system memory. This makes it more flexible than array-based
implementations, which have a fixed size. The use of separate functions for each operation (push, pop,
enqueue, dequeue) ensures code modularity, readability, and reusability. These implementations are useful
in various applications, including memory management, scheduling tasks, and handling expression
evaluation.
31
Department of ECE, DSATM