0% found this document useful (0 votes)
138 views

CTSD-1 Practical

This document contains instructions and an index for a laboratory manual on computational thinking for structure design. The index lists 11 experiments involving arrays, functions, sorting, searching, and linked lists.

Uploaded by

Nayeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

CTSD-1 Practical

This document contains instructions and an index for a laboratory manual on computational thinking for structure design. The index lists 11 experiments involving arrays, functions, sorting, searching, and linked lists.

Uploaded by

Nayeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

FACULTY OF ENGINEERING & TECHNOLOGY

BACHELOR OF TECHNOLOGY

Computational Thinking for Structure Design -2


(303105151)
2nd SEMESTER
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Laboratory Manual

COMPUTATIONAL THINKING FOR STRUCTURE DESIGN -2 PRACTICAL BOOK


COMPUTER SCIENCE AND ENGINEERING DEPARTMENT
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

PREFACE

It gives us immense pleasure to present the first edition of Computational


Thinking for Structure Design -1 for the B.Tech. 1st year students for PARUL
UNIVERSITY.

The Fundamental of Programming theory and laboratory courses at PARUL


UNIVERSITY, WAGHODIA, VADODARA are designed in such a way that students
develop the basic understanding of the subject in the theory classes and then try their
hands on the computer learnt during the theoretical sessions.
This book is emphatically not focused on “the syntax of C”. Understanding the
fundamental ideals, principals, and techniques is the essence of a good programmer.
Only well-designed code has a chance of becoming part of a correct, reliable, and
maintainable system. Also, “the fundamentals” are what last: they will still be essential
after today’s language and tools have evolved or been replaced.
We acknowledge the authors and publishers of all the books which we have
consulted while developing this Practical book. Hopefully this Computational Thinking
for Structure Design -1 willserve the purpose for which it has been developed.

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

Instructions to students

Every student should obtain a copy of laboratory Manual.


1. Dress Code: Students must come to the laboratory wearing.
i. Trousers,
ii. half-sleeve tops and
iii. Leather shoes. Half pants, loosely hanging garments and slippers are not allowed.
2. To avoid injury, the student must take the permission of the laboratory staff before
handling anymachine.
3. Students must ensure that their work areas are clean and dry to avoid slipping.
4. Do not eat or drink in the laboratory.
5. Do not remove anything from the computer laboratory without permission.
6. Do not touch, connect or disconnect any plug or cable without your
lecturer/laboratorytechnician’s permission.
All students need to perform the practical/p

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

CERTIFICATE

This is to certify that

Mr./Ms.................................................................................................. with

enrolment no ..................................................... has successfully completed

his/her laboratory experiments in the Computational Thinking for Structure

Design -2 (303105151) from the department of...........................................................


during the academic year ................................

Date of Submission:......................... Staff In charge:...........................

Head of Department:...........................................

Enrollment No: Page No:0


INDEX

Faculty of Engineering & Technology


303105151 - Computational Thinking for Structured Design-2
Class: 2nd Sem Subject: - Computational Thinking for Structured Design-2
A.Y. 20 -20 Subject Code: 303105151

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) Write a to display present date and time


using c language
b) Write a c program to demonstrate
pre-processor directives
2 i) Macros
ii) Conditional Compilation

1. Write a Program in C that uses functions to


perform following operations
1. Reading a Complex Number
2. Writing a Complex Number
3. Addition of 2 Complex Number
4. Multiplication of 2 Complex Number
2. Write a C program to store records of n
students based on roll_no, name, gende
3 and 5 subject
1. Calculate percentage of each studen
using 5 subjects
2. Display the student list according to
their percentages

4 Write a C program to store n employee records


based on
EMP_ID,EMP_NAME,EMP_DEPTID,EMP_PHNO,E
MP_SALARY and display all the details of
employees using EMP_NAME in sorted order

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

1. Write a c program to implement selection Sort &


Bubble sort
5

2. Write a C program to reverse the elements within


a given range in a sorted list. Example : input : 10
9 1 2 4 3 4 6 7 8 10 3 8
output: 1 2 8 7 6 4 4 3 9 10 the sorted list of given
array elements is 1 2 3 4 4 6 7 8 9 10 , after reversing
the elements with in the range 3 and 8 is 1 2 8 7 6 4 4
3 9 10
a) Write a c program to implement Insertion
sort & Quick sort
6 b) Write a c program to sort the given n
integers and perform following operations
i) Find the products of every two odd
position elements
ii) Find the sum of every two even position
elements
Explanation:Input
:9
1 98 354 7 26
Output: 3 15 35 63
6 10 14
The sorted list of given input is 1 2 3 4 5 6 7 8
9, the product of alternative odd position
elements
is 1*3 = 3,3*5=15,5*7=35... and the sum of
two even position elements 2+4 =6,4+6=10

1.Write a C Program to implement Merge Sort.


7
a) Write a c program to sort in ascending order
and reverse the individual row elements of an
mxn matrix
8 input : 3 4
1423
7 8 10 9
6352
output:
4321
10 9 8 7
6532
b) Write a c program to sort elements in row wise
and print the elements of matrix in Column major
order
Input: 3 4
Enrollment No: Page No:0
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
1423
7 8 10 9
6352
Output:
172
283
395
4 10 6
Explanation:
The sorted matrix according to the conditions is
1234
7 8 9 10
2356
after sorting matrix the elements as to be printed
in column major order
172
283
395
4 10 6

9 a) Write a c program to perform linear Search


b) Write a c program to perform binary search

10 Write a c program to Create a single Linked list and


perform Following Operations

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

11 a) Write a program to Reverse a singly Linked list.


b) Write a c program to check whether the created
linked list is palindrome or not

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
Write a c program to Create a Circular Linked list
12 and perform Following Operations

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

14 Write a c program to Create a Circular DoubleLinked


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

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

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: ");

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


{

printf("%d ", arr[i])

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.

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

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");
}

// Increase or decrease the number of subarraysint choice;


printf("\nEnter 1 to increase the number of subarrays, 2 to decrease: ");scanf("%d",
&choice);
if (choice == 1) {
// Increase the number of subarraysint newRows;
printf("Enter the number of additional rows: ");
scanf("%d", &newRows);

matrix = (int **)realloc(matrix, (rows + newRows) * sizeof(int *));for (i = rows; i


< rows + newRows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

matrix = (int **)realloc(matrix, (rows + newRows) * sizeof(int *));for (i = rows; i


< rows + newRows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));}
rows
+= newRows;
} else if (choice == 2) {
// Decrease the number of subarraysint
removeRows;
printf("Enter the number of rows to remove: ");
scanf("%d", &removeRows);
if (removeRows <= rows) {
matrix = (int **)realloc(matrix, (rows - removeRows) * sizeof(int *));rows -=
removeRows;
} else {
printf("Error: Cannot remove more rows than present.\n");
}
} else {
printf("Invalid choice.\n");
}
// Display the modified matrix
printf("\nModified Matrix:\n");for (i =
0; i < rows; i++) {
for (j = 0; j < cols; j++) { printf("%d\t",
matrix[i][j]);
}
printf("\n");}
// Deallocate memory
for (i = 0; i < rows; i++) {free(
matrix[i]);-
}
free(matrix);
return 0;
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
ii) elements in the 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");
}
// 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) {

// Increase the number of elementsint


newCols;
printf("Enter the number of additional columns: ");
scanf("%d", &newCols);
for (i = 0; i < rows; i++) {
matrix[i] = (int *)realloc(matrix[i], (cols + newCols) * sizeof(int));
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
cols += newCols;
} else if (choice == 2) {
// Decrease the number of elementsint
removeCols;
printf("Enter the number of columns to remove: ");
scanf("%d", &removeCols);
if (removeCols <= cols) { for (i =
0; i < rows; i++) {
matrix[i] = (int *)realloc(matrix[i], (cols - removeCols) * sizeof(int));
}
cols -= removeCols;
} else {
printf("Error: Cannot remove more columns than present.\n");
}
} else {
printf("Invalid choice.\n");
}
// Display the modified matrix
printf("\nModified Matrix:\n");for (i =
0; i < rows; i++) {
for (j = 0; j < cols; j++) { printf("%d\t",
matrix[i][j]);
}
printf("\n");
}
// Deallocate memory
for (i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix)
return 0;
}
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
Faculty of Engineering & Technology 303105151 -
Computational Thinking for Structured Design-2

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)

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

{
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;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
2.

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

// Define a structure for student


recordstruct Student {
int roll_no;
char
name[50];
char gender;
float subjects[5];
float percentage;
};

// Function to calculate percentage for a student


void calculatePercentage(struct Student
*student) {
float total = 0;
for (int i = 0; i < 5; i++) {
total += student->subjects[i];
}
student->percentage = total / 5.0;
}

// Function to display student list according to their


percentages void displayStudentsByPercentage(struct
Student *students, int n) {
// Sort students by percentage in descending order using
bubble sortfor (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].percentage < students[j + 1].percentage) {
// Swap students
struct Student temp =
students[j];students[j] =
students[j + 1]; students[j + 1]
= temp;
}
}

// Display sorted student list

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

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);

// Allocate memory for an array of students


struct Student *students = (struct Student *)malloc(n * sizeof(struct Student));

// Input student records


for (int i = 0; i < n; i++)
{
printf("Enter details for student %d:\n", i +
1);printf("Roll No: ");
scanf("%d",
&students[i].roll_no);
printf("Name: ");
scanf("%s",
students[i].name);
printf("Gender: ");
scanf(" %c", &students[i].gender);
printf("Enter marks for 5
subjects:\n");for (int j = 0; j < 5;
j++) {
printf("Subject %d: ", j + 1);
scanf("%f",
&students[i].subjects[j]);
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

// Calculate percentage for the student


calculatePercentage(&students[i]);
}

// Display student list sorted by


percentage
displayStudentsByPercentage(students,
n);

// Free allocated
memoryfree(students);
return 0;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

Practical 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define structure for employee


recordstruct Employee {
int ID;
char
NAME[50];
int DEPT_ID;
char
PHNO[15];
float
SALARY;
};

// Function to swap two employee records


void swap(struct Employee *a, struct
Employee *b) {struct Employee temp =
*a;
*a = *b;
*b = temp;
}

// Function to perform bubble sort on employee records based


on NAMEvoid bubbleSort(struct Employee *arr, int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(arr[j].NAME,
arr[j+1].NAME) > 0) {swap(&arr[j],
&arr[j+1]);
}
}
}
}

int main()
{int n;
printf("Enter the number of
employees: ");scanf("%d", &n);

// Dynamically allocate memory for array of employees


struct Employee *employees = (struct Employee*) malloc(n * sizeof(struct Employee));
Enrollment No: Page No:0
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

// Input employee details


printf("Enter employee details (ID, NAME, DEPT_ID, PHNO,
SALARY):\n");for (int i = 0; i < n; i++) {
printf("Employee %d:\n", i + 1);
scanf("%d %s %d %s %f", &employees[i].ID, employees[i].NAME,
&employees[i].DEPT_ID,employees[i].PHNO,
&employees[i].SALARY);
}

// Sort employees by name using


bubble sortbubbleSort(employees, n);

// Display sorted employee details


printf("\nEmployee Details (Sorted by
Name):\n");printf(" \n");
printf("ID\tNAME\tDEPT_ID\tPHNO\tSAL
ARY\n");printf(" \n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t\t%d\t\t%s\t\t%.2f\n", employees[i].ID, employees[i].NAME,
employees[i].DEPT_ID,
employees[i].PHNO,
employees[i].SALARY);
}
// Free allocated
memory
free(employees);
return 0;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

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]);
}
}

void 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]);
}
void printArray(int arr[], int size) {for (int i = 0; i <
size; i++)
printf("%d ", arr[i]);
printf("\n");

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
b) #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]);
}
}

void 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]);
}

void printArray(int arr[], int size) {for (int i = 0;


i < size; i++)
printf("%d ", arr[i]);
printf("\n");

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
}

int main() {int


n;
printf("Enter the number of elements: ");scanf("%d",
&n);

int arr[n];
printf("Enter %d elements:\n", n);for (int i = 0;
i < n; i++)
scanf("%d", &arr[i]);

printf("Original array: \n");


printArray(arr, n);

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;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

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;

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
}

// 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;

while (j >= 0 && arr[j] >


key) {arr[j + 1] = arr[j];
j = j - 1;
}

arr[j + 1] = key;
}
}

// Function to perform Quick Sort


void quickSort(int arr[], int low, int
high) {if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

// Function to partition the array for


Quick Sortint partition(int arr[], int low,
int high) {
int pivot =
arr[high];int i =
(low - 1);

for (int j = low; j <= high - 1;


j++) {if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
}

swap(&arr[i + 1],
&arr[high]);return (i +
1);
}

// Function to swap two


elementsvoid swap(int *a,
int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Function to print an
array void printArray(int
arr[], int n) {
for (int i = 0; i < n;
i++) {printf("%d ",
arr[i]);
}
printf("\n");
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

(b) #include
<stdio.h>#include
<stdlib.h>

// Function to swap two


integersvoid swap(int *a,
int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// 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);

int *arr = (int *)malloc(n *


sizeof(int));printf("Enter the
integers: ");
for (int i = 0; i < n;
i++) {scanf("%d",
&arr[i]);
}

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");

printf("Products of every two odd position


elements: ");for (int i = 0; i < n - 1; i += 2) {
if (arr[i] % 2 != 0 && (i+2)<n) {
printf("%d ", arr[i] * arr[i + 2]);
}
}
printf("\n");

printf("Sum of every two even position


elements: ");for (int i = 1; i < n - 1; i += 2) {
if (arr[i] % 2 == 0 && (i+2)<n ) {printf("%d ", arr
}
}
printf("\n");

// Free dynamically allocated memory


free(arr);

return 0;

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

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++;
}
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
Enrollment No: Page No:0
Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

void printArray(int A[], int size) {


int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Given array is \n");


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
return 0;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

Practical 8
#include <stdio.h>
#include <stdlib.h>

// Function to sort an array in ascending order


void sortArray(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] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

// Function to reverse an array


void reverseArray(int arr[], int n) {
int start = 0;
int end = n - 1;
while (start < end) {
// Swap arr[start] and arr[end]
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}

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));
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
for (int i = 0; i < m; i++) {
sortArray(matrix[i], n);
}
for (int i = 0; i < m; i++) {
reverseArray(matrix[i], n);
}
printf("Modified matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
for (int i = 0; i < m; i++) {
free(matrix[i]);
}
free(matrix);

return 0;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
(b)

#include <stdio.h>

void sortRow(int row[], int cols) {


// Bubble sort implementation to sort a row
for (int i = 0; i < cols - 1; i++) {
for (int j = 0; j < cols - i - 1; j++) {
if (row[j] > row[j + 1]) {
// Swap elements
int temp = row[j];
row[j] = row[j + 1];
row[j + 1] = temp;
}
}
}
}

int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int matrix[rows][cols];

// Read the matrix elements


printf("Enter the elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

// Sort each row of the matrix


for (int i = 0; i < rows; i++) {
sortRow(matrix[i], cols);
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
// Transpose the matrix

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;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2

Practical 9
(a)
#include <stdio.h>

// Function to perform linear search


int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Return the index if found
}
}
return -1; // Return -1 if not found
}
int main() {
int arr[] = {12, 45, 67, 23, 56, 89};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 56;

int index = linearSearch(arr, n, key);

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;
}

Enrollment No: Page No:0


Faculty of Engineering & Technology
303105151 - Computational Thinking for Structured Design-2
(b)
#include <stdio.h>

// Function to perform binary search


int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid; // Return the index if found
}

// If key is greater, ignore left half


if (arr[mid] < key) {
low = mid + 1;
}
// If key is smaller, ignore right half
else {
high = mid - 1;
}
}

return -1; // Return -1 if not found


}
int main() {
int arr[] = {12, 23, 34, 45, 56, 67, 78, 89};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 56;
int index = binarySearch(arr, 0, n - 1, key);

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;
}

Enrollment No: Page No:0

You might also like