0% found this document useful (0 votes)
24 views27 pages

Final DSA Exp 1,2,3

The document contains multiple C programming experiments demonstrating statistical calculations (mean, median, mode), matrix operations (addition and multiplication), search algorithms (linear and binary), and sorting algorithms (bubble, insertion, selection). Each section includes code snippets, user input prompts, and sample outputs. The experiments illustrate fundamental programming concepts and data manipulation techniques.

Uploaded by

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

Final DSA Exp 1,2,3

The document contains multiple C programming experiments demonstrating statistical calculations (mean, median, mode), matrix operations (addition and multiplication), search algorithms (linear and binary), and sorting algorithms (bubble, insertion, selection). Each section includes code snippets, user input prompts, and sample outputs. The experiments illustrate fundamental programming concepts and data manipulation techniques.

Uploaded by

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

Experiment 1

INPUT:
For Mean:
#include <stdio.h>
double calculateMean(int array[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += array[i];
}
return (double)sum / n;
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int array[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
double mean = calculateMean(array, n);
printf("Mean of the array is: %.2f\n", mean);
return 0;
}
OUTPUT:
Enter the number of elements in the array: 4
Enter 4 elements:
Element 1: 14
Element 2: 18
Element 3: 22
Element 4: 35
Mean of the array is: 22.25

For Median:
INPUT:
#include <stdio.h>
void sortArray(int array[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
double calculateMedian(int array[], int n) {
sortArray(array, n);
if (n % 2 == 0) {
return (array[n / 2 - 1] + array[n / 2]) / 2.0;
} else {
return array[n / 2];
}
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int array[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
double median = calculateMedian(array, n);
printf("Median of the array is: %.2f\n", median);
return 0;
}

OUTPUT:
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 15
Element 2: 35
Element 3: 24
Element 4: 18
Element 5: 12
Median of the array is: 18.00
For Mode:

INPUT:
#include <stdio.h>
int calculateMode(int array[], int n) {
int maxValue = 0, maxCount = 0;
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (array[j] == array[i]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
maxValue = array[i];
}
}
return (maxCount > 1) ? maxValue : -1; // Return -1 if there is no mode
}
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int array[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
}
int mode = calculateMode(array, n);
if (mode != -1) {
printf("Mode of the array is: %d\n", mode);
} else {
printf("No mode found in the array.\n");
}
return 0;
}

OUTPUT:
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 24
Element 2: 28
Element 3: 56
Element 4: 47
Element 5: 15
No mode found in the array.

Enter the number of elements in the array: 5


Enter 5 elements:
Element 1: 14
Element 2: 25
Element 3: 35
Element 4: 14
Element 5: 14
Mode of the array is: 14

INPUT:

#include <stdio.h>
#include <math.h>
int main() {
int n, i;
float data[100], sum = 0, mean, variance = 0, std_deviation;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 0 || n > 100) {
printf("Invalid number of elements.\n");
return 1;
}
printf("Enter the elements :\n");
for (i = 0; i < n; ++i) {
scanf("%f", &data[i]);
sum += data[i];
}
mean = sum / n;
for (i = 0; i < n; ++i)
variance += pow(data[i] - mean, 2);
variance /= n;
std_deviation = sqrt(variance);
printf("Mean = %f\n", mean);
printf("Variance = %f\n", variance);
printf("Standard Deviation = %f\n", std_deviation);
return 0;
}

OUTPUT:
Enter the number of elements: 4
Enter the elements :
12
15
22
35
Mean = 21.000000
Variance = 78.500000
Standard Deviation = 8.860023

//2 MATRIX ADDITION


#include<stdio.h>

int main(){
int i,n,m,arr1[50][50],arr2[50][50],sum[50][50];
printf("Enter the no of rows:");
scanf("%d", &m);
printf("Enter the no of columns:");
scanf("%d", &n);
printf("First mat is:\n");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
scanf("%d", &arr1[i][j]);
}
}

printf("Second mat is:\n");


for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
scanf("%d", &arr2[i][j]);
}

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


for(int j=0; j<n; j++){
sum[i][j]=arr1[i][j] + arr2[i][j];

}
}

printf("Sum is:\n");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){

printf("%d\t", sum[i][j]);
}
printf("\n");
}
return 0;
}

OUTPUT:

Enter the no of rows:2


Enter the no of columns:3
First mat is:
14
26
38
Second mat is:
35
79
58
Sum is:
4 9 9
15 8 16

MATRIX MULTIPLICATION
#include<stdio.h>
int main()
{
int i,j,k,c,r,c1,r1;
printf("enter the number of rows:");
scanf("%d",&r);
printf("enter the number of column:");
scanf("%d",&c);
int arr1[r][c];
printf("enter the elements of matrix\n");
for(i=0;i<r;i++){
for(j=0;j<c;j++){
scanf("%d",&arr1[i][j]);
}
} printf("\n");
printf("enter the number of rows:");
scanf("%d",&r1);
printf("enter the number of column:");
scanf("%d",&c1);
int arr2[r1][c1];
printf("enter the elements of matrix\n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
scanf("%d",&arr2[i][j]);
}
}
if(c!=r1)
{
printf("matrix cannot be multiplied");
}
int arr[r][c1];
int cr=c;
for(i=0;i<r;i++){
for(j=0;j<c1;j++){
arr[i][j]=0;
for(k=0;k<cr;k++){
arr[i][j]=arr[i][j]+arr1[i][k]*arr2[k][j];
}
}
}
printf("\n");
for(i=0;i<r;i++){
for(j=0;j<c1;j++){
printf("%d\t",arr[i][j]);
}
printf("\n");
}

return 0;
}

OUTPUT:

enter the number of rows:2


enter the number of column:3
enter the elements of matrix
13
25
36

enter the number of rows:3


enter the number of column:2
enter the elements of matrix
12
45
78

27 33
59 73

Experiment No-2
Linear Search Binary Search -Integer
#include <stdio.h>

void linearSearch(int arr[], int size, int no) {


int i;
for (i = 0; i < size; i++) {
if (arr[i] == no) {
printf("No found at location: %d\n", i + 1);
return;
}
}
printf("No not found\n");
}
void binarySearch(int arr[], int no, int lb, int ub) {
if (lb > ub) {
printf("Given no is not found\n");
return;
} else {
int mid = (lb + ub) / 2;
if (arr[mid] == no) {
printf("No found at position: %d\n", mid + 1);
} else if (arr[mid] > no) {
binarySearch(arr, no, lb, mid - 1);
} else {
binarySearch(arr, no, mid + 1, ub);
}
}
}

int main() {
int arr[50], size, no, choice;

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements of array: ");


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

do {
printf("\nChoose a search method:\n");
printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter number to be searched: ");
scanf("%d", &no);
linearSearch(arr, size, no);
break;
case 2:
printf("Enter number to be searched: ");
scanf("%d", &no);
binarySearch(arr, no, 0, size - 1);
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 3);

return 0;
}

Output
Choose a search method:
1. Linear Search
2. Binary Search
3. Exit
Enter your choice: 1
Enter number to be searched: 4
No not found

Choose a search method:


1. Linear Search
2. Binary Search
3. Exit
Enter your choice: 2
Enter number to be searched: 7
No found at position: 3

Choose a search method:


1. Linear Search
2. Binary Search
3. Exit
Enter your choice: 3
Exiting program.
String
#include <stdio.h>
#include <string.h>

// Function to perform linear search on an array of strings


int linearSearch(char arr[][20], int size, char* key) {
for (int i = 0; i < size; i++) {
if (strcmp(arr[i], key) == 0) {
return i; // Return the index if the key is found
}
}
return -1; // Return -1 if the key is not found
}

void sortArray(char arr[][20], int size) {


char temp[20];
for (int i = 0; i < size - 1; i++) {
for (int j = i + 1; j < size; j++) {
if (strcmp(arr[i], arr[j]) > 0) {
strcpy(temp, arr[i]);
strcpy(arr[i], arr[j]);
strcpy(arr[j], temp);
}
}
}
}

int binarySearch(char arr[][20], int size, char* key) {


int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
int result = strcmp(arr[mid], key);

if (result == 0) {
return mid;
} else if (result < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

int main() {
int size, choice;

printf("Enter the number of elements in the array: ");


scanf("%d", &size);

char arr[size][20];
char key[20];

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


for (int i = 0; i < size; i++) {
printf("Element %d: ", i + 1);
scanf("%s", arr[i]);
}

do {
printf("\nChoose a search method:\n");
printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter the element to search for: ");
scanf("%s", key);
int linearIndex = linearSearch(arr, size, key);
if (linearIndex != -1) {
printf("The element is present at index %d.\n", linearIndex);
} else {
printf("The element is not present in the array.\n");
}
break;

case 2:
sortArray(arr, size);
printf("Enter the element to search for: ");
scanf("%s", key);
int binaryIndex = binarySearch(arr, size, key);
if (binaryIndex != -1) {
printf("The element is present at index %d.\n", binaryIndex);
} else {
printf("The element is not present in the array.\n");
}
break;

case 3:
printf("Exiting program.\n");
break;

default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 3);

return 0;
}
Output
Enter the number of elements in the array: 3
Enter the elements of the array:
Element 1: 9
Element 2: 4
Element 3: 7
Choose a search method:
1. Linear Search
2. Binary Search
3. Exit
Enter your choice: 1
Enter the element to search for: 4
The element is present at index 1.

Choose a search method:


1. Linear Search
2. Binary Search
3. Exit
Enter your choice: 2
Enter the element to search for: 6
The element is not present in the array.

Experiment No-3
3) a) Bubble sort, Insertion Sort, Selection sort
#include <stdio.h>

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


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

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


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

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


int i, j, temp;
for (j = 1; j < n; j++) {
temp = arr[j];
for (i = j - 1; i >= 0 && temp < arr[i]; i--) {
arr[i + 1] = arr[i];
}
arr[i + 1] = temp;
}
}

int main() {
int arr[100], n, choice;

printf("Enter size of array: ");


scanf("%d", &n);

printf("Enter elements of array: ");


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

printf("Unsorted array: ");


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

printf("\nChoose a sorting method:\n");


printf("1. Selection Sort\n");
printf("2. Bubble Sort\n");
printf("3. Insertion Sort\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
selectionSort(arr, n);
break;
case 2:
bubbleSort(arr, n);
break;
case 3:
insertionSort(arr, n);
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice.\n");
return 0;
}

printf("\nSorted array is: ");


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

return 0;
}
Output:
Case I
Enter size of array: 4
Enter elements of array: 4 6 2 1
Unsorted array: 4 6 2 1
Choose a sorting method:
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Exit
Enter your choice: 1.

Sorted array is: 1 2 4 6

Case II
Enter size of array: 3
Enter elements of array: 8 5 3
Unsorted array: 8 5 3
Choose a sorting method:
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Exit
Enter your choice: 2

Sorted array is: 3 5 8

Case III
Enter size of array: 3
Enter elements of array: 7 4 9
Unsorted array: 7 4 9
Choose a sorting method:
1. Selection Sort
2. Bubble Sort
3. Insertion Sort
4. Exit
Enter your choice: 3

Sorted array is: 4 7 9


3) b) Bubble Sort Strings
#include <stdio.h>
#include <string.h>

void bubbleSort(char arr[][50], int n) {


char temp[50];
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (strcmp(arr[j], arr[j+1]) > 0) {
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j+1]);
strcpy(arr[j+1], temp);
}
}
}
}

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

char arr[n][50];

printf("Enter the names:\n");


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

bubbleSort(arr, n);

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

return 0;
}

Output
Enter the number of names: 3
Enter the names:
Elle Bob Chris

Sorted names:
Bob
Chris
Elle

You might also like