Rishi Raj DSA
Rishi Raj DSA
Rishi Raj DSA
1.)Write a program for using Dynamic Functions (malloc(), calloc(), realloc() and free()) functions.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int size, choice, i;
printf("Enter the size of the array: ");
scanf("%d", &size);
// Dynamically allocating memory for the array using malloc()
arr = (int *)malloc(size * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed. Exiting...\n");
return 1;
}
// Inputting elements into the array
printf("Enter %d elements:\n", size);
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Performing operations on the array
printf("\nOperations Menu:\n");
printf("1. Print the array\n");
printf("2. Double each element of the array\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Printing the array
printf("The array is: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
break;
case 2:
// Doubling each element of the array
for (i = 0; i < size; i++) {
arr[i] *= 2;
}
printf("Array elements doubled successfully.\n");
break;
default:
printf("Invalid choice!\n");
}
// Freeing dynamically allocated memory using free()
free(arr);
return 0;
}
BCA – 205 Data structure 1323572
BCA – 205 Data structure 1323572
if (index != -1) {
printf("Element found at index: %d\n", index);
} else {
printf("Element not found in the array.\n");
}
return 0;
}
BCA – 205 Data structure 1323572
return (i + 1);
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}
BCA – 205 Data structure 1323572
// l is for left index and r is right index of the sub-array of arr to be sorted
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// Same as (l+r)/2, but avoids overflow for large l and r
int m = l + (r - l) / 2;
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}