CTSD-1 Practical
CTSD-1 Practical
BACHELOR OF TECHNOLOGY
Laboratory Manual
PREFACE
Instructions to students
CERTIFICATE
Mr./Ms.................................................................................................. with
Head of Department:...........................................
Date
Sr. Date of Marks
of
No Experiment Title Page Perfor out 0f Sign
Asses
. No. mance
sment 10
To From
1) Write a c program to increase or decrease the
existing size of an 1D array?
1 2) Write a c program on 2D array to Increase
& Decrease i) No of subarrays
ii) elements in the subarrays
A. Insertion At Beginning
B. Insertion At End
C. Insertion After a particular node
D. Insertion Before a particular node
E. Insertion at specific position
F. Search a particular node
G. Return a particular node
H. Deletion at the beginning
I. Deletion at the end
J. Deletion after a particular node
K. Deletion before a particular node
L. Delete a particular node
M. Deletion at a specific position
A. Insertion At Beginning
B. Insertion At End
C. Insertion After a particular node
D. Insertion Before a particular node
E. Insertion at specific position
F. Search a particular node
G. Return a particular node
H. Deletion at the beginning
I. Deletion at the end
J. Deletion after a particular node
K. Deletion before a particular node
L. Delete a particular node
M. Deletion at a specific position
13
Write a c program to Create a Circular single Linked
list and perform Following Operations
A. Insertion After a particular node
B. Insertion Before a particular node
C. Search a particular node
D. Return a particular node
E. Deletion before a particular node
F. Delete a particular node
Practical 1
1) Write a c program to increase or decrease theexisting size of an 1D array?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *array, size, new_size, i; printf("Enter the
size of the array: ");scanf("%d", &size);
array = (int *)malloc(size * sizeof(int));
printf("Enter the elements of the array: ");for (i = 0;
i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Enter the new size of the array: ");
scanf("%d", &new_size);
if (new_size > size)
{
array = (int *)realloc(array, new_size * sizeof(int));for (i =
size; i < new_size; i++)
{
array[i] = 0;
}
}
else if (new_size < size)
{
array = (int *)realloc(array, new_size * sizeof(int));
}
printf("The new array is: ");
free(array);
return 0;
}
Enrollment No: Page No:0
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
Explanation : This C program prompts the user to input the size of an array, then takesthe elements of the
array as input. After that, it asks for a new size for the array and reallocates memory accordingly. If the new
size is greater than the original size, the additional elements are initialized to 0. If the new size is less than
the original size, thearray is simply reallocated without initializing the new elements. Finally, it prints the
elements of the modified array and frees the allocated memory.
Here's a brief breakdown of the program:
1. array: Pointer to the dynamically allocated array.
2. size: Variable to store the original size of the array.
3. new_size: Variable to store the new size of the array.
4. i: Loop variable.
The program uses dynamic memory allocation functions (malloc and realloc) to createand resize the array.
It also uses free to release the allocated memory when done.
A few points to note:
The program initializes additional elements to 0 only when the new size is greaterthan the original
size.
When decreasing the size of the array, the elements beyond the new size are notexplicitly set to 0.
This may result in unpredictable values in those locations.
Overall, the program is a simple example of dynamic memory allocation andreallocation in C.
i) No of subarrays
#include <stdio.h>
#include <stdlib.h>int
main() {
int **matrix, rows, cols, i, j;
// Initialize the 2D array
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
matrix = (int **)malloc(rows * sizeof(int *));for (i = 0;
i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
// Initialize the elements of the matrix printf("Enter the
elements of the matrix:\n");for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) { scanf("%d",
&matrix[i][j]);
}
}
// Display the original matrix
printf("Original Matrix:\n"); for (i
= 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
, printf("%d\t", matrix[i][j]);
}
printf("\n");
}
#include <stdio.h>
#include <stdlib.h> int
main() {
int **matrix, rows, cols, i, j;
// Initialize the 2D array printf("Enter the
number of rows: ");scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
matrix = (int **)malloc(rows * sizeof(int *));for (i = 0;
i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
// Initialize the elements of the matrix printf("Enter the
elements of the matrix:\n");for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) { scanf("%d",
&matrix[i][j]);
}
}
// Display the original matrix
printf("Original Matrix:\n"); for (i
= 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Increase or decrease the number of elements in subarraysint choice;
printf("\nEnter 1 to increase the number of elements, 2 to decrease: ");scanf("%d",
&choice);
if (choice == 1) {
Practical 2
2.(a)
#include<stdio.h>
#include<conio.h> Int
main(){
printf(“%s”, DATE );
printf(“%s”, TIME );
}
(B)
#include <stdio.h>
// Macro definition
#define MAX(x, y) ((x) > (y) ? (x) : (y))
// Conditional
Compilation#define
DEBUG 1
int main()
{int a,b;
printf("Enter the value of
A&B:\n");scanf("%d
%d",&a,&b);
// Using macro
printf("Maximum of %d and %d is: %d\n", a, b, MAX(a, b));
// Using conditional
compilation#if DEBUG
printf("Debugging information is
enabled.\n");#else
printf("Debugging information is
disabled.\n");#endif
return 0;}
Enrollment No: Page No:0
Enrollment No: Page No:0
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
Practical 3
1. #include <stdio.h>
#include <conio.h>
struct complex
{
float real, imag;
}a, b, c;
struct complex read(void); void
write(struct complex);
struct complex add(struct complex, struct complex);struct
complex mul(struct complex, struct complex);
void main ()
{
clrscr();
printf("Enter the 1st complex number\n ");a = read();
write(a);
printf("Enter the 2nd complex number\n");b =
read();
write(b); printf("Addition\n
");c = add(a, b);
write(c);
printf("Multiplication\n");c =
mul(a, b);
write(c);
getch();
}
struct complex read(void)
{
struct complex t;
printf("Enter the real part\n");
scanf("%f", &t.real);
printf("Enter the imaginary part\n");
scanf("%f", &t.imag);
return t;
}
void write(struct complex a)
{
printf("Complex number is\n");
printf(" %.1f + i %.1f", a.real, a.imag);printf("\n");
}
struct complex add(struct complex p, struct complex q)
{
struct complex t;
t.real = (p.real + q.real); t.imag =
(p.imag + q.imag);return t;
}
struct complex mul(struct complex p, struct complex q)
{
struct complex t;
t.real=(p.real * q.real) - (p.imag * q.imag);
t.imag=(p.real * q.imag) + (p.imag *
q.real);return t;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
printf("Roll
No\tName\tGender\tPercentage\n");for (int i
= 0; i < n; i++) {
printf("%d\t%s\t%c\t%.2f%%\n", students[i].roll_no, students[i].name,
students[i].gender,students[i].percentage);
}
}
int main() {
int n;
printf("Enter the number of
students: ");scanf("%d", &n);
// Free allocated
memoryfree(students);
return 0;
}
Practical 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{int n;
printf("Enter the number of
employees: ");scanf("%d", &n);
Practical 5
a) #include <stdio.h>
void swap(int *xp, int *yp) {int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n) {for (int i = 0; i < n-
1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) if (arr[j] <
arr[min_idx])
min_idx = j; swap(&arr[min_idx],
&arr[i]);
}
}
int arr[n];
printf("Enter %d elements:\n", n);for (int i = 0;
i < n; i++)
scanf("%d", &arr[i]);
selectionSort(arr, n);
printf("Array sorted using Selection Sort: \n");printArray(arr, n);
bubbleSort(arr, n);
printf("Array sorted using Bubble Sort: \n");printArray(arr,
n);
return 0;
}
Practical 6
(a) #include <stdio.h>
// Function prototypes
void insertionSort(int arr[], int n);
void quickSort(int arr[], int low, int
high);int partition(int arr[], int
low, int high); void swap(int *a, int
*b);
void printArray(int arr[], int n);
int
main()
{int n;
printf("Enter the number of
elements: ");scanf("%d", &n);
int arr[n];
printf("Enter the
elements:\n");for (int i = 0;
i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Original
array: ");
printArray(arr, n);
printf("\n\nApplying Insertion
Sort...\n");insertionSort(arr, n);
printf("Array after Insertion
Sort: ");printArray(arr, n);
printf("\n\nApplying Quick
Sort...\n");quickSort(arr, 0, n -
1);
printf("Array after Quick
Sort: ");printArray(arr, n);
return 0;
// Function to perform
Insertion Sortvoid
insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n;
i++) {key =
arr[i];
j = i - 1;
arr[j + 1] = key;
}
}
swap(&arr[i + 1],
&arr[high]);return (i +
1);
}
// Function to print an
array void printArray(int
arr[], int n) {
for (int i = 0; i < n;
i++) {printf("%d ",
arr[i]);
}
printf("\n");
}
(b) #include
<stdio.h>#include
<stdlib.h>
// Function to perform
bubble sortvoid
bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1;
j++) {if (arr[j] > arr[j +
1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main()
{int n;
printf("Enter the number of
integers: ");scanf("%d", &n);
bubbleSort(arr, n);
printf("Sorted array:
"); for (int i = 0; i <
n; i++) {
printf("%d ", arr[i]);
Enrollment No: Page No:0
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
}
print("\n");
return 0;
Practical 7
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
Practical 8
#include <stdio.h>
#include <stdlib.h>
int main() {
int m, n;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
int **matrix = (int **)malloc(m * sizeof(int *));
for (int i = 0; i < m; i++) {
matrix[i] = (int *)malloc(n * sizeof(int));
}
return 0;
}
#include <stdio.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
printf("Output:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", matrix[j][i]);
}
printf("\n");
}
return 0;
}
Practical 9
(a)
#include <stdio.h>
if (index != -1) {
printf("Element %d found at index %d.\n", key, index);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
if (index != -1) {
printf("Element %d found at index %d.\n", key, index);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}