hrt
hrt
hrt
Week 1:
int main() {
int *arr, size, new_size, i;
return 0;
Output:
Page | 2
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
Code:-
#include <stdio.h>
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("\nInitial Array:\n");
displayArray(arr, rows, cols);
decreaseSubarrays(&rows);
printf("\nAfter decreasing number of 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;
tzset();
localTime = localtime(¤tTime);
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)
Code:-
#include <stdio.h>
scanf("%d", &num);
result = SQUARE(num);
#if DEBUG_MODE == 6
Page | 8
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
#else
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;
Page | 10
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
struct complex c;
c.real = a.real + b.real;
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);
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)
#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;
};
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) {
int main() {
struct Student students[MAX_STUDENTS];
int n;
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);
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;
};
}
int main() {
int n;
Page | 17
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
printf("EMP_DEPTID: ");
scanf("%d", &employees[i].EMP_DEPTID);
while(getchar() != '\n');
printf("EMP_PHNO: ");
printf("EMP_SALARY: ");
scanf("%f", &employees[i].EMP_SALARY);
Page | 18
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
printf(" \n");
printf("EMP_ID\tEMP_NAME\tEMP_DEPTID\tEMP_PHNO\tEMP_SALARY\n");
printf(" \n");
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>
*xp = *yp;
*yp = temp;
}
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
Page | 21
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
swap(&arr[j], &arr[j+1]);
int main() {
int n;
int arr[n];
scanf("%d", &arr[i]);
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");
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>
*b = temp;
j = i - 1;
// Move elements of arr[0..i-1], that are greater than key, to one position ahead of their
current position
j--; }
Page | 27
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
arr[j + 1] = key;
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
Page | 28
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
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("\n");
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("\n");
quickSort(arr, 0, n - 1);
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>
*a = *b;
*b = temp;
}
min_idx = i;
min_idx = j;
}
}
Page | 32
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
int main() {
int arr[] = {9, 1, 9, 8, 3, 5, 4, 7, 2, 6};
printf("\n");
// Sort the array using selection sort (or any other sorting algorithm)
selectionSort(arr, n);
Page | 33
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
printf("\n");
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;
}
// Merge the temporary arrays back into arr[left..right]
int i = 0, j = 0, k = left;
arr[k] = Left[i];
i++;
} else {
arr[k] = Right[j];
j++;
Page | 36
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
}
k++;
}
arr[k] = Left[i];
i++;
k++;
arr[k] = Right[j];
j++;
k++;
}
Page | 37
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
}
int main() {
}
printf("\n");
mergeSort(arr, 0, n - 1);
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>
}
}
start++;
end--;
}
}
int main() {
int m, n;
printf("Enter the number of rows and columns: ");
scanf("%d", &matrix[i][j]);
}
Page | 41
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
}
// Reverse the elements of each row
reverseArray(matrix[i], n);
}
// Print the modified matrix
printf("Modified matrix:\n");
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) {
}
}
}}
int main() {
int rows, cols;
Page | 44
Faculty of Engineering & Technology
Computational Thinking for Structured Design
(303105151)
scanf("%d", &matrix[i][j]);
sortRow(matrix[i], cols);
}
// Transpose the matrix
printf("Output:\n");
for (int i = 0; i < cols; 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:
int main() {
int arr[] = {12, 45, 67, 23, 56, 89};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 56;
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)
int main() {
int arr[] = {12, 23, 34, 45, 56, 67, 78, 89};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 56;
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