Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

hrt

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

Faculty of Engineering & Technology

Computational Thinking for Structured Design


(303105151)

Week 1:

(a) Write a c program to increase or decrease the existing size of an 1D array?


Code:-
#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr, size, new_size, i;

printf("Enter the size of the array: \n");


scanf("%d", &size);

arr = (int *)malloc(size * sizeof(int));

printf("Enter the elements of the array: \n");


for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the new size of the array: \n");


scanf("%d", &new_size);

// Reallocate the array


arr = (int *)realloc(arr, new_size * sizeof(int));

if (new_size > size) {


// Initialize the new elements of the array
for (i = size; i < new_size; i++) {
arr[i] = 0;
}
}

printf("The new array is: ");


for (i = 0; i < new_size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
Page | 1
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

return 0;

Output:

Page | 2
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(b) Write a c program on 2D array to Increase & Decrease


i) No of subarrays
ii) elements in the subarrays

Code:-
#include <stdio.h>

void increaseSubarrays(int *rows) {


(*rows)++;
}

void decreaseSubarrays(int *rows) {


(*rows)--;
}

void increaseElements(int *cols) {


(*cols)++;
}

void decreaseElements(int *cols) {


(*cols)--;
}

void displayArray(int arr[][100], int rows, int cols) {


printf("Array:\n");
for (int i = 0; i < rows; i++)

for (int j = 0; j < cols; j++) {


printf("%d ", arr[i][j]);
}
printf("\n");
}
}

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

int arr[100][100];
Page | 3
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

printf("Enter the elements of the array:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &arr[i][j]);
}
}

printf("\nInitial Array:\n");
displayArray(arr, rows, cols);

// Increase the number of subarrays


increaseSubarrays(&rows);
printf("\nAfter increasing number of subarrays:\n");
displayArray(arr, rows, cols);

// Decrease the number of subarrays


decreaseSubarrays(&rows);
printf("\nAfter decreasing number of subarrays:\n");
displayArray(arr, rows, cols);

// Increase the number of elements in subarrays


increaseElements(&cols);
printf("\nAfter increasing number of elements in subarrays:\n");
displayArray(arr, rows, cols);

// Decrease the number of elements in subarrays

decreaseSubarrays(&rows);
printf("\nAfter decreasing number of subarrays:\n");
displayArray(arr, rows, cols);

// Increase the number of elements in subarrays


increaseElements(&cols);
printf("\nAfter increasing number of elements in subarrays:\n");
displayArray(arr, rows, cols);

// Decrease the number of elements in subarrays


decreaseElements(&cols);
printf("\nAfter decreasing number of elements in subarrays:\n");
displayArray(arr, rows, cols);

return 0;
}
Page | 4
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 5
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 2:
(a) Write a to display present date and time using c language

Code:-
#include <stdio.h>

#include <time.h>

int main() {

time_t currentTime;
struct tm *localTime;

// Set the time zone to Indian Standard Time (IST)

setenv("TZ", "Asia/Kolkata", 1);

tzset();

// Get the current time


currentTime = time(NULL);

// Convert the current time to the local time

localTime = localtime(&currentTime);

// Display the date and time in Indian standards

printf("Current Indian Date and Time: %02d-%02d-%d %02d:%02d:%02d\n",


localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year + 1900,
localTime->tm_hour, localTime->tm_min, localTime->tm_sec);

Page | 6
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

return 0;
}

Output:

Page | 7
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(b) Write a c program to demonstrate pre-processor directives


i) Macros
ii) Conditional Compilation.

Code:-

#include <stdio.h>

// Define a macro for finding the square of a number


#define SQUARE(x) ((x) * (x))

// Define a conditional compilation flag


#define DEBUG_MODE 1
int main() {
int num;
int result;
// Taking input from the user

printf("Enter a number: ");

scanf("%d", &num);

// Using the SQUARE macro to calculate the square of a number

result = SQUARE(num);

printf("Square of %d is %d\n", num, result);


// Conditional compilation based on the DEBUG_MODE flag

#if DEBUG_MODE == 6

printf("Debug mode is enabled\n");

Page | 8
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

#else

printf("Debug mode is disabled\n");


#endif

return 0;
}

Output:

Page | 9
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 3:
(a) Write a C program that uses functions to perform the following Operations
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers

Code:-
#include<stdio.h>
struct complex{
int real;

int imaginary;

};
struct complex read(){
struct complex c;
//printf("Enter real part: ");
scanf("%d",&c.real);
//printf("Enter imaginary part: ");

scanf("%d",&c.imaginary);

return c;

void write(struct complex c) {


printf("%d+%di ",c.real,c.imaginary);
}
struct complex add(struct complex a, struct complex b)

Page | 10
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

struct complex c;
c.real = a.real + b.real;

c.imaginary = a.imaginary + b.imaginary;


return c;

struct complex mult(struct complex a, struct complex b){


struct complex c;
c.real = a.real *b.real - a .imaginary*b.imaginary;

c. imaginary = a.real*b.imaginary +a.imaginary*b.real;


return c;

void main(){
struct complex c1,c2, sum,m;
c1 = read();
c2 = read();

write(c1);

write(c2);
sum = add(c1, c2); printf("\n Addition is "); write(sum);

m=mult(c1,c2);

printf("\n Multiplication is ");


write(m); }

Page | 11
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 12
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(b) Write a c program to store records of n students based on roll_no, name,


gender and 5 subject
marks
v) calculate percentage each student using 5 subjects
vi)Display the student list according to their percentages
Code:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 50
#define MAX_NAME_LENGTH 50
struct Student {
int roll_no;
char name[MAX_NAME_LENGTH];

char gender;
float marks[5];
float percentage;
};

void calculatePercentage(struct Student *student) {


float totalMarks = 0;
for (int i = 0; i < 5; ++i) {
totalMarks += student->marks[i];

Page | 13
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

}
student->percentage = totalMarks / 5.0;
}
void sortByPercentage(struct Student *students, int n) {
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n - i - 1; ++j) {
if (students[j].percentage < students[j + 1].percentage) {

struct Student temp = students[j];


students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}

int main() {
struct Student students[MAX_STUDENTS];
int n;

printf("Enter the number of students: ");


scanf("%d", &n);
if (n > MAX_STUDENTS || n <= 0) {
printf("Invalid number of students.\n");
return 1;
}
Page | 14
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

for (int i = 0; i < n; ++i) {


printf("Enter details of 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 of 5 subjects: ");
for (int j = 0; j < 5; ++j) {
scanf("%f", &students[i].marks[j]);
}
calculatePercentage(&students[i]);
}
sortByPercentage(students, n);

printf("\nStudent list sorted by percentage:\n");


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

Page | 15
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

return 0;
}

Output:

Page | 16
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 4:
Write a C program to store n employee records based on
EMP_ID,EMP_NAME,EMP_DEPTID,EMP_PHNO,EMP_SALARY and
display all the details of
employees using EMP_NAME in sorted order.

Code:-

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Employee {

int EMP_ID;

char EMP_NAME[50];
int EMP_DEPTID;
char EMP_PHNO[15];

float EMP_SALARY;
};

// Function to compare employee names for sorting


int compareNames(const void *a, const void *b) {
return strcmp(((struct Employee *)a)->EMP_NAME, ((struct Employee *)b)-
>EMP_NAME);

}
int main() {

int n;

Page | 17
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

printf("Enter the number of employees: ");


scanf("%d", &n);

struct Employee *employees = malloc(n * sizeof(struct Employee));

for (int i = 0; i < n; ++i) {

printf("\nEnter details for employee %d:\n", i + 1);


printf("EMP_ID: ");
scanf("%d", &employees[i].EMP_ID);
printf("EMP_NAME: ");
scanf("%s", employees[i].EMP_NAME);

printf("EMP_DEPTID: ");

scanf("%d", &employees[i].EMP_DEPTID);

// Clear input buffer before reading phone number

while(getchar() != '\n');

printf("EMP_PHNO: ");

fgets(employees[i].EMP_PHNO, sizeof(employees[i].EMP_PHNO), stdin);


employees[i].EMP_PHNO[strcspn(employees[i].EMP_PHNO, "\n")] = '\0'; // Remove
newline

printf("EMP_SALARY: ");

scanf("%f", &employees[i].EMP_SALARY);

Page | 18
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

qsort(employees, n, sizeof(struct Employee), compareNames);

printf("\nEmployee details sorted by name:\n");

printf(" \n");

printf("EMP_ID\tEMP_NAME\tEMP_DEPTID\tEMP_PHNO\tEMP_SALARY\n");
printf(" \n");

for (int i = 0; i < n; ++i) {


printf("%d\t%s\t\t%d\t\t%s\t%.2f\n", employees[i].EMP_ID,
employees[i].EMP_NAME,

employees[i].EMP_DEPTID, employees[i].EMP_PHNO,
employees[i].EMP_SALARY);

free(employees);

return 0;

Page | 19
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 20
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 5:
(a) Write a c program to implement selection Sort & Bubble sort

Code:-
#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])

Page | 21
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

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

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

Page | 22
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

printArray(arr, n);
bubbleSort(arr, n);
printf("Array sorted using Bubble Sort: \n");

printArray(arr, n);

return 0;

Output:

Page | 23
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(b) Write a C program to reverse the elements within a given range in a sorted
list.
Code:-
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reverse_range(int arr[], int start, int end) {
while (start < end) {
swap(&arr[start-1], &arr[end-1]);
start++;
end--;
}
}
int main() {
int arr[100];
int n;
int start;
int end ;
printf("Enter n value");
scanf("%d",&n);
printf("ENter the elements in the array\n");
Page | 24
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
printf("Enter n start\n");
scanf("%d",&start);
printf("Enter n end\n");
scanf("%d",&end);
printf("Enter n start:%d\n",start);
printf("Enter n end:%d\n",end);
printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

reverse_range(arr, start, end);

printf("Reversed array: ");


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

Page | 25
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

return 0;
}
Output:

Page | 26
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 6:
(a) Write a c program to implement Insertion sort & Quick sort.
Code:-
#include <stdio.h>

// Function to swap two elements


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

*b = temp;

// Insertion sort implementation


void insertionSort(int arr[], int n) {
int i, key, j;

for (i = 1; i < n; i++) {


key = arr[i];

j = i - 1;

// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their
current position

while (j >= 0 && arr[j] > key) {


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

j--; }
Page | 27
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

arr[j + 1] = key;

// Partition function for Quick Sort

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {

// If current element is smaller than the pivot

if (arr[j] <= pivot) {

i++;

swap(&arr[i], &arr[j]);

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

return (i + 1);

// Recursive Quick Sort implementation


void quickSort(int arr[], int low, int high) {

if (low < high) {


// pi is partitioning index, arr[p] is now at right place

Page | 28
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

int pi = partition(arr, low, high);


// Recursively sort elements before and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);

int main() {

int arr[100];

int n ;

printf("Enter n ");
scanf("%d",&n);
printf("Enter the n Elements");
for(int i=0;i<n;i++)

scanf("%d",&arr[i]);

printf("Original array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

printf("\n");

// Sort array using Insertion Sort

Page | 29
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

insertionSort(arr, n);
printf("Sorted array (Insertion Sort): ");
for (int i = 0; i < n; i++) {

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

}
printf("\n");

// Sort array using Quick Sort

quickSort(arr, 0, n - 1);

printf("Sorted array (Quick Sort): ");

for (int i = 0; i < n; i++) {


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

printf("\n");

return 0;

Page | 30
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 31
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(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
Code:-
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;

*a = *b;

*b = temp;
}

void selectionSort(int arr[], int n) {


int i, j, min_idx;

// One by one move boundary of unsorted subarray

for (i = 0; i < n - 1; i++) {

// Find the minimum element in unsorted array

min_idx = i;

for (j = i + 1; j < n; j++) {


if (arr[j] < arr[min_idx]) {

min_idx = j;
}
}

Page | 32
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

// Swap the found minimum element with the first element


swap(&arr[min_idx], &arr[i]);
}

int main() {
int arr[] = {9, 1, 9, 8, 3, 5, 4, 7, 2, 6};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);

printf("\n");

// Sort the array using selection sort (or any other sorting algorithm)
selectionSort(arr, n);

printf("Sorted array: ");

for (int i = 0; i < n; i++) {

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


}

Page | 33
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

printf("\n");

int product_odd = 1; // Initialize product of odd elements to 1


int sum_even = 0; // Initialize sum of even elements to 0

for (int i = 0; i < n; i++) {

if (i % 2 == 0) { // If even position (0-based indexing)


sum_even += arr[i];
} else { // If odd position

product_odd *= arr[i];

}
printf("Product of every two odd position elements: %d\n", product_odd);
printf("Sum of every two even position elements: %d\n", sum_even);
return 0;

Page | 34
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 35
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 7:
Write a c Program to implement Merge Sort.
Code:-
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temporary arrays


int Left[n1], Right[n2];

// Copy data to temporary arrays


for (int i = 0; i < n1; i++) {
Left[i] = arr[left + i];

for (int j = 0; j < n2; j++) {

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

}
// Merge the temporary arrays back into arr[left..right]
int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {

if (Left[i] <= Right[j]) {

arr[k] = Left[i];
i++;

} else {
arr[k] = Right[j];

j++;

Page | 36
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

}
k++;
}

// Copy the remaining elements of Left[]


while (i < n1) {

arr[k] = Left[i];
i++;
k++;

// Copy the remaining elements of Right[]


while (j < n2) {

arr[k] = Right[j];
j++;

k++;
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {

int mid = left + (right - left) / 2;


// Sort first and second halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);

// Merge the sorted halves


merge(arr, left, mid, right); }

Page | 37
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

}
int main() {

int arr[] = {6, 5, 3, 1, 8, 7, 2, 4};

int n = sizeof(arr) / sizeof(arr[0]);


printf("Original array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);

}
printf("\n");

mergeSort(arr, 0, n - 1);

printf("Sorted array: ")

for (int i = 0; i < n; i++) {

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


}

printf("\n");
return 0;

Page | 38
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 39
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 8:
(a) Write a c program to sort in ascending order and reverse the individual row
elements of an mxn matrix
Code:-

#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;
Page | 40
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

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

// Dynamically allocate memory for the matrix


int **matrix = (int **)malloc(m * sizeof(int *));
for (int i = 0; i < m; i++) {
matrix[i] = (int *)malloc(n * sizeof(int));
}

// Read the elements of the matrix


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

Page | 41
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

// Sort each row in ascending order


for (int i = 0; i < m; i++) {
sortArray(matrix[i], n);

}
// Reverse the elements of each row

for (int i = 0; i < m; i++) {

reverseArray(matrix[i], n);
}
// Print the modified matrix

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

// Free dynamically allocated memory


for (int i = 0; i < m; i++) {

free(matrix[i]);

}
free(matrix);
return 0;

Page | 42
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 43
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(b) Write a c program to sort elements in row wise and print the elements of
matrix in Column major order.
Code:-
#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++) {

Page | 44
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

scanf("%d", &matrix[i][j]);

// Sort each row of the matrix

for (int i = 0; i < rows; i++) {

sortRow(matrix[i], cols);

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

Page | 45
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 46
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Week 9:

(a) Write a program to perform to linear search.


Code:-
#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;

// Perform linear search


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

Page | 47
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

Output:

Page | 48
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

(b) Write a program to perform binary search.


Code:-
#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;

// Check if key is present at mid


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;

// Perform binary search


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

Page | 49
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)

return 0;
}

Output:

Page | 50

You might also like