Professional Documents
Culture Documents
Okok
Okok
#include
<stdlib.h>
return;
}
for (int i = 0; i < (*oldSize < newSize ? *oldSize : newSize); i++) {
newArray[i] = (*arr)[i];
free(*arr);
*arr = newArray;
*oldSize = newSize;
int main() {
arr[i] = i + 1;
int oldSize = 5;
9|Page
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
for (int i = 0; i < oldSize; i++) {
9|Page
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
}
printf("\n");
int newSize = 8;
printf("\n");
newSize = 3;
printf("\n");
free(arr);
return 0;
OUTPUT:-
10 P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
code:-
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int **arr,i,j,row,colum;
printf("enter the no of rows and colums:\n");
scanf("%d%d",&row,&colum);
arr=(int**)malloc(row*sizeof(int*));
for(i=0;i<row;i++)
{
*(arr+i)=(int*)calloc(colum,sizeof(int));
}
for(i=0;i<row;i++)
{
printf("enter values for row: %d\n",i); for(j=0;j<colum;j+
+)
{
scanf("%d",(arr[i]+j));
}
}
printf("\n the data inside the 2D matrix:\n");
for(i=0;i<row;i++)
11 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
for(j=0;j<colum;j++)
12 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
{
printf("%d",*(arr[i]+j));
}
printf("\n");
}
int oldrow=row;
printf("enter new row
size"); scanf("%d",&row);
arr=(int**)realloc(arr,row);
if(oldrow<row)
{
for(i=oldrow;i<row;i++)
{
*(arr+i)=(int**)calloc(colum,sizeof(int));
}
for(i=0;i<row;i++)
{
*(arr+i)=(int*)calloc(colum,sizeof(int));
}
for(i=0;i<row;i++)
{
printf("enter values for row %d",i);
for(j=0;j<colum;j++)
{
scanf("%d",*(arr[i]+j));
}
}
printf("\n the data inside the 2D matrix:\n");
for(i=0;i<row;i++)
13 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
{
for(j=0;j<colum;j++)
{
printf("%d",(arr[i]+j));
}
printf("\n");
}
}
}
output:
14 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 02
1. Write a Code to display present date and time using c
language Code:-
#include
int main()
return 0;
Output:-
15 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
2. Write a c program to demonstrate pre-processor directives
i) Macros
code:-
i) Macros
#include <stdio.h>
#define PI 3.14159
int main()
scanf("%f", &radius);
return 0;
Output:-
16 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
ii) Conditional Compilation:-
#include <stdio.h>
#define A 6
int main()
#ifdef A
#endif
return 0;
Output:-
17 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 03
1. 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 img;
};
Complex getData()
Complex c;
scanf("%d",&c.real);
scanf("%d",&c.img);
return c;
void putData(Complex x)
printf("\n%d+%di",x.real,x.img);
Complex result;
result.real=c1.real+c2.real;
result.img=c1.img+c2.img;
18 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
return result;
((c1.img*c2.img))*(-1); result.img=(c1.img*c2.img)
int main()
Complex var1,var2;
var1=getData();
var2=getData();
putData(var1);
putData(var2);
Complex addition;
addition=add(var1,var2);
putData(addition);
Complex multiply;
multiply=product(var1,var2);
putData(multiply);
19 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Output:-
20 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
2. Write a c program to store records of n students based on roll_no, name, gender and 5 subject marks.
i) Calculate percentage each student using 5 Subjects. ii)Display the student list according to their percentages.
Code:-
#include <stdio.h>
#define MAX_SUBJECTS 5
struct Student {
int roll_no;
char name[50];
char gender;
int marks[MAX_SUBJECTS];
float percentage;
};
void readStudent(struct Student *student) {
scanf("%d", &student->roll_no);
scanf("%s", student->name);
>gender);
int total_marks = 0;
{ total_marks += student->marks[i];
}
21 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
student->percentage = (float)(total_marks /MAX_SUBJECTS*100)/100;
*a = *b;
*b = temp;
int main() {
int n;
scanf("%d", &n);
readStudent(&students[i]);
calculatePercentage(&students[i]);
0; i < n; i++) {
22 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
printf("%d\t%s\t%c\t", students[i].roll_no, students[i].name, students[i].gender);
printf("\t%.2f%%\n", students[i].percentage);
return 0;
Output:-
23 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 04
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;
};
>emp_name);
int main() {
int num_employees, i;
scanf("%d", &num_employees);
n", MAX_EMPLOYEES);
return 1;
}
24 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
struct Employee employees[num_employees];
printf("EMP_ID: ");
scanf("%d", &employees[i].emp_id);
printf("EMP_NAME: ");
printf("EMP_DEPTID: ");
scanf("%d", &employees[i].emp_deptid);
printf("EMP_PHNO: ");
printf("EMP_SALARY: ");
scanf("%f", &employees[i].emp_salary);
}
compareEmployees);
employees[i].emp_id, employees[i].emp_name,
employees[i].emp_deptid, employees[i].emp_phno,
employees[i].emp_salary);
return 0;
25 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Output:-
26 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 05
Output:-
27 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Bubble sort:-
#include <stdio.h>
void main()
{ int n=6;
int arr[]={5,8,1,2,9,11,10};
int i;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
int j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(arr[j]>arr[j+1])
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
28 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Output:-
29 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
2. Write a C program to reverse the elements within a given range in a sorted
list 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 76 4 4 3 9 10.
Code:-
#include <stdio.h>
int main() {
scanf("%d", &size);
int list[size];
scanf("%d", &list[i]);
if (pos1 < 1 || pos1 > size || pos2 < 1 || pos2 > size) {
size); return 1;
pos1--;
pos2--;
if (pos1 == pos2) {
} else {
temp = list[pos1];
list[pos1] = list[pos2];
list[pos2] = temp;
30 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
printf("The modified list is:\n");
31 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
for (i = 0; i < size; i++) {
}
printf("\n");
return 0;
Output:-
32 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 06
1. Write a c program to implement Insertion Sort & Quick sort.
Insertion
sort:- Code:-
#include <stdio.h>
int main()
{
int arr[]={2,4,5,9,8,6,7,1};
int n=8,i,j;
int key;
printf("Array before sorting : ");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
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;
}
printf("\n\nArray After sorting : "); for(i=0;i<n;i+
+)
printf("%d ",arr[i]);
}
Output:-
33 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Quick sort:-
#include<stdio.h>
void partition(int
int arr[]={4,9,7,10,1,3,6};
int i,n=7;
printf("Before:");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
partition(arr,0,n-
1); printf("\n
After:");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
int Pi;
if(low<high)
Pi=quicksort(arr,low,high);
partition (arr,low,Pi-1);
partition(arr,Pi+1,high);
int i,j;
i=low;
34 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
j=high;
while(i<j)
i++;
j--;
if(i<j)
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
int temp=arr[i];
arr[i]=arr[high];
arr[high]=temp;
return i;
35 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Output:-
36 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
2. 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: 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.
Code:-
#include<stdio.h>
int temp =
arr[j]; arr[j] =
arr[j+1];
arr[j+1] = temp;
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
// Sorting the
array
bubbleSort(arr, n);
printf("Product: ");
37 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
long long product_odd = 1;
38 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
product_odd = arr[i]*arr[i+2];
if(i+2<n){
product_odd=1;
printf("\nSum:");
if(i+2<n){
printf("%lld ",sum_even);
sum_even=0;
return 0;
Output:-
39 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 07
Write a C Program to implement Merge Sort.
Code:-
#include <stdio.h>
int i = 0, j = 0, k = left;
arr[k] = Left[i];
i++;
} else {
arr[k] = Right[j];
j++;
} k+
+;
arr[k] = Left[i]; i+
+;
k++;
{ arr[k] =
Right[j]; j++;
k++;
40 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
}
printf("\n");
int main() {
int n = sizeof(arr) /
sizeof(arr[0]); printf("Unsorted
mergeSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
41 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
Output:-
42 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 08
1. Write a c program to sort in ascending order and reverse the individual row elements of an mxn matrix
Code:-
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(&arr[i], &arr[minIndex]);
}
}
}
void reverseArray(int arr[], int n) {
for (int i = 0; i < n / 2; i++) {
swap(&arr[i], &arr[n - i - 1]);
}
}
void sortAndReverseRows(int matrix[][100], int m, int n) {
for (int i = 0; i < m; i++) {
selectionSort(matrix[i], n); // Sort the row in ascending order
reverseArray(matrix[i], n); // Reverse the sorted row elements
}
}
void printMatrix(int matrix[][100], int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int m, n;
printf("Enter the number of rows (m):\n
"); scanf("%d", &m);
printf("Enter the number of columns (n):\n ");
43 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
scanf("%d", &n);
int matrix[m]
[100];
printf("Enter the matrix elements:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
sortAndReverseRows(matrix, m, n);
printf("Modified matrix:\n");
printMatrix(matrix, m, n);
return 0;
}
Output:-
44 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
2. 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 swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(&arr[i], &arr[minIndex]);
}
}
}
void printMatrixColumnMajor(int matrix[][100], int m, int n) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int m, n;
printf("Enter the number of rows (m):\n
"); scanf("%d", &m);
printf("Enter the number of columns (n):\n ");
scanf("%d", &n);
int matrix[m][100];
printf("Enter the matrix elements:\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++) {
selectionSort(matrix[i], n);
}
printf("Matrix in column-major order after sorting rows:\n");
printMatrixColumnMajor(matrix, m, n);
45 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
return 0;
}
Output:-
46 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL - 09
1. Write a c program to perform linear Search.
Code:-
#include <stdio.h>
int linearSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int x;
printf("Enter the element to search:\n ");
scanf("%d", &x);
int result = linearSearch(arr, n,
x); if (result == -1) {
printf("Element is not present in array\n");
} else {
printf("Element is present at index %d\n", result);
}
return 0;
}
Output:-
47 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
2. Write a c program to perform binary search.
Code:-
#include <stdio.h>
int binarySearch(int arr[], int left, int right, int x) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
}
if (arr[mid] < x) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x;
printf("Enter the element to search:\n ");
scanf("%d", &x);
int result = binarySearch(arr, 0, n - 1,
x); if (result == -1) {
printf("Element is not present in array\n");
} else {
printf("Element is present at index %d\n", result);
}
return 0;
}
Output:-
48 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
PRACTICAL – 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
#include <stdio.h>
#include <stdlib.h>
49 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
struct Node* newNode = createNode(value);
newNode->next = *head;
*head = newNode;
}
51 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
return;
}
if (position == 1) {
insertAtBeginning(head, value);
return;
}
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range.\n");
return;
}
insertAfter(temp, value);
}
54 |P a g e
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
if (position <= 0) {
printf("Invalid position.\n");
return;
}
if (position == 1) {
deleteAtBeginning(head);
return;
}
struct Node* temp = *head;
for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL)
{ printf("Position out of range.\n");
return;
}
deleteAfter(temp);
}
int main() {
struct Node* head = NULL;
return 0;
}
OUTPUT:
412 | P a g
Faculty of engineering and technology
Computational Thinking and Structured Design-2(303105151)
Enrollment no:-2303031460233
413 | P a g