Final DSA Exp 1,2,3
Final DSA Exp 1,2,3
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.
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
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("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:
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:
27 33
59 73
Experiment No-2
Linear Search Binary Search -Integer
#include <stdio.h>
int main() {
int arr[50], size, no, choice;
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
if (result == 0) {
return mid;
} else if (result < 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}
int main() {
int size, choice;
char arr[size][20];
char key[20];
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.
Experiment No-3
3) a) Bubble sort, Insertion Sort, Selection sort
#include <stdio.h>
int main() {
int arr[100], n, 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;
}
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.
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
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
int main() {
int n;
printf("Enter the number of names: ");
scanf("%d", &n);
char arr[n][50];
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