Posts

lab12

 #include<stdio.h>  #include<math.h>  #include<stdlib.h>  int board[20],count;  int main()  {  int n,i,j;  void queen(int row,int n);  printf(" - N Queens Problem Using Backtracking -");  printf("\n\nEnter number of Queens:");  scanf("%d",&n);  queen(1,n);  return 0;  }  //function for printing the solution  void print(int n)  {  int i,j;  printf("\n\nSolution %d:\n\n",++count);  for(i=1;i<=n;++i)  printf("\t%d",i);  for(i=1;i<=n;++i)  {  printf("\n\n%d",i);  for(j=1;j<=n;++j) //for nxn board  {  if(board[i]==j)  printf("\tQ"); //queen at i,j position  else  printf("\t-"); //empty slot  }  }  }  /*funtion to check conflicts  If no conflict for desired postion returns 1 otherwise returns 0*/  int place(int row,int column)  {  int i;  for(i=1;i<=row-1;++i)  {...

lab11

 #include <stdio.h>  #include <stdlib.h>  #include <time.h>  // Merge two subarrays of arr[].  // First subarray is arr[l..m]  // Second subarray is arr[m+1..r]  void merge(int arr[], int l, int m, int r) {  int i, j, k;  int n1 = m - l + 1;  int n2 = r - m;  // Create temporary arrays  int L[n1], R[n2];  // Copy data to temporary arrays L[] and R[]  for (i = 0; i < n1; i++)  L[i] = arr[l + i];  for (j = 0; j < n2; j++)  R[j] = arr[m + 1 + j];  // Merge the temporary arrays back into arr[l..r]  i = 0;  j = 0;  k = l;  while (i < n1 && j < n2) {  if (L[i] <= R[j]) {  arr[k] = L[i];  i++;  } else {  arr[k] = R[j];  j++;  }  k++;  }  // Copy the remaining elements of L[], if any  while (i < n1) {  arr[k] = L[i];  i++;  k++;  }  36  Department of CSE (CY),...

lab10

 #include <stdio.h>  #include <stdlib.h>  #include <time.h>  // Function to partition the array and return the pivot index  int partition(int arr[], int low, int high) {  int pivot = arr[high]; // Pivot element  int i = (low - 1);     // Index of smaller element  for (int j = low; j <= high - 1; j++) {  // If current element is smaller than or equal to pivot  if (arr[j] <= pivot) {  i++; // Increment index of smaller element  // Swap arr[i] and arr[j]  int temp = arr[i];  arr[i] = arr[j];  arr[j] = temp;  }  }  // Swap arr[i + 1] and arr[high] (or pivot)  int temp = arr[i + 1];  arr[i + 1] = arr[high];  arr[high] = temp;  return (i + 1);  }  // Function to implement Quick Sort  void quickSort(int arr[], int low, int high) {  if (low < high) {  // pi is partitioning index, arr[pi] is now at right place  int p...

lab9

 #include <stdio.h>  #include <stdlib.h>  #include <time.h>  // Function to perform Selection Sort  void selectionSort(int arr[], int n) {  int i, j, minIndex, temp;  for (i = 0; i < n - 1; i++) {  minIndex = i;  for (j = i + 1; j < n; j++) {  if (arr[j] < arr[minIndex]) {  minIndex = j;  }  }  // Swap the found minimum element with the first element  temp = arr[minIndex];  arr[minIndex] = arr[i];  arr[i] = temp;  }  }  int main() {  int n, i;  clock_t start, end;  double cpu_time_used;  printf("Enter the number of elements (n): ");  scanf("%d", &n);  if (n < 5000) {  printf("Please enter a value of n greater than 5000.\n");  return 1;  }  int *arr = (int *)malloc(n * sizeof(int));  if (arr == NULL) {  printf("Memory allocation failed.\n");  return 1;  }  // Generate n random numbe...

lab8

 #include <stdio.h>  #include <stdbool.h>  #define MAX_SIZE 100  // Function to find subset with given sum  void subsetSum(int set[], int subset[], int n, int subSize, int total, int  nodeCount, int sum) {  if (total == sum) {  // Print the subset  printf("Subset found: { ");  for (int i = 0; i < subSize; i++) {  printf("%d ", subset[i]);  }  printf("}\n");  return;  } else {  // Check the sum of the remaining elements  for (int i = nodeCount; i < n; i++) {  subset[subSize] = set[i];  subsetSum(set, subset, n, subSize + 1, total + set[i], i + 1, sum);  }  }  }  int main() {  29  Department of CSE (CY), RNSIT          IV Semester          BCSL404 – ADA LABORATORY    Department of CSE (CY), RNSIT         30          int set[MAX_SIZE]; ...

lab7

 PROGRAM:  #include <stdio.h>  #include <stdlib.h>  // Structure to represent items  struct Item {  int value;  int weight;  double ratio; // Value-to-weight ratio for sorting  };  // Comparison function for sorting items based on ratio in descending order  int compare(const void *a, const void *b) {  struct Item *item1 = (struct Item *)a;  struct Item *item2 = (struct Item *)b;  double ratio1 = item1->ratio;  double ratio2 = item2->ratio;  if (ratio1 > ratio2) return -1;  else if (ratio1 < ratio2) return 1;  else return 0;  }  // Function to solve discrete Knapsack problem  void discreteKnapsack(struct Item items[], int n, int capacity) {  int i, j;  int dp[n + 1][capacity + 1];  // Initialize the DP table  for (i = 0; i <= n; i++) {  for (j = 0; j <= capacity; j++) {  if (i == 0 || j == 0)  dp[i][j] = 0;  else if...

lab6

 #include <stdio.h>  // Function to find maximum of two integers  int max(int a, int b) {  return (a > b) ? a : b;  }  // Function to solve 0/1 Knapsack problem  int knapsack(int W, int wt[], int val[], int n) {  int i, w;  int K[n + 1][W + 1];  // Build table K[][] in bottom-up manner  for (i = 0; i <= n; i++) {  for (w = 0; w <= W; w++) {  if (i == 0 || w == 0)  K[i][w] = 0;  else if (wt[i - 1] <= w)  K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);  else  K[i][w] = K[i - 1][w];  }  }  // K[n][W] contains the maximum value that can be put in a knapsack of  capacity W  return K[n][W];  }  int main() {  int val[100], wt[100]; // Arrays to store values and weights  int W, n; // Knapsack capacity and number of items  printf("Enter the number of items: ");  scanf("%d", &n);  printf("Enter the values and ...