Practical File Questions:
Subject: DAA
Sub Teacher: Bindu Aryal
1. Write a c program for implementing Greedy Approach:-Fractional
Knapsack Problem.
2. Write a c program for implementing Dynamic programming: 0/1 knapsack
problem.
3. Write a c program for implementing Binary search.
4. Write a c program for implementing Min-max finding.
5. Write a c program for Bubble sort.
6. Write a c program for implementing insertion sort.
7. Write a c program for implementing selection sort.
8. Write a c program for implementing Quick sort.
9. Write a c program for Fibonacci series.
10.Write a c program for GCD.
11.Write a Program to implement the concept of Backtracking: N-queen
Problem.
The End
1. Write a c program for implementing Greedy Approach:-Fractional
Knapsack Problem.
# include<stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity) {
float x[20], tp = 0;
int i, j, u;
u = capacity;
for (i = 0; i < n; i++)
x[i] = 0.0;//initializng all objects x[i] value 0 at first
for (i = 0; i < n; i++) {
if (weight[i] > u)
break;
else {
x[i] = 1.0;//object completly inserted
tp = tp + profit[i];
u = u - weight[i];
}
}
if (i < n)
x[i] = u / weight[i];// calculating portion of weight required
tp = tp + (x[i] * profit[i]);//calculating portion of profit with
// weight to add in tp
printf("\nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%f\t", x[i]);
printf("\nMaximum profit is:- %f", tp);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
printf("\nEnter the no. of objects:- ");
scanf("%d", &num);
printf("\nEnter the wts and profits of each object:- ");
for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}
printf("\nEnter the capacityacity of knapsack:- ");
scanf("%f", &capacity);
// calculating the ratio of p and w
for (i = 0; i < num; i++) {
ratio[i] = profit[i] / weight[i];
}
//sorting the ratio(p/w) in descending order
for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
//sorting the weight in descending order according to ratio
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
//sorting the profit in descending order according to ratio
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
knapsack(num, weight, profit, capacity);
return(0);
}
2. Write a c program for implementing Dynamic programming: 0/1 knapsack
problem.
//0/1 knapsack using dynamic Approach
#include<stdio.h>
#include<conio.h>
void knapsack(int [],int [],int,int);
int main()
{
int p[20],w[20],i,j,n,capacity;
printf("Enter the number of objects=");
scanf("%d",&n);
printf("Enter the capacity of knapsack=");
scanf("%d",&capacity);
for(i=1;i<=n;i++)
{
printf("Enter weight and profit of objects");
scanf("%d",&w[i]);
scanf("%d",&p[i]);
}
knapsack(p,w,n,capacity);
getch();
return 0;
}
// function knapsack
void knapsack(int p[],int w[],int n,int capacity)
{
int k[20][20],i,j;//2D array for table
//This is a table we fill value at ith row and jth column of table
// and last value is our Answer.
for(i=0;i<=n;i++)
{
for(j=0;j<=capacity;j++)
{
if (i==0||j==0)
{
k[i][j]=0;
}
else if (j<w[i])
{
k[i][j]=k[i-1][j];// filling above value
}
else
{
if(k[i-1][j]>k[i-1][j-w[i]]+p[i])
{
k[i][j]=k[i-1][j];//filling above value
}
else
{
k[i][j]=k[i-1][j-w[i]]+p[i];// fill second term if above
//is not larger.
}
}
}
}
// for Displaying Result
for(i=0;i<=n;i++)
{
for(j=0;j<=capacity;j++)
{
printf("%d,",k[i][j]);
}
printf("\n");
}
printf("\n Maximum possible profit=%d",k[n][capacity]);
}
3. Write a c program for implementing Binary search.
//1.Program for Binary Search
#include<stdio.h>
#include<conio.h>
void binary_search(int list[], int lo, int hi, int key)
{
int mid;
if (lo > hi)
{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found in index=%d\n",mid);
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}
}
int main()
{
int key, size, i;
int list[25];
printf("Enter size of a list: ");
scanf("%d", &size);
printf("Enter elements in sorting order:\n");
for(i = 0; i < size; i++)
{
scanf("%d",&list[i]);
}
printf("\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);
4. Write a c program for implementing Min-max finding.
//Min max finding
#include<stdio.h>
#include<conio.h>
int max, min;
int a[100];
void maxmin(int low, int high)
{
int max1, min1, mid;
if(low==high)
{
max = min = a[low];
}
else
{
if(low == high-1)
{
if(a[low] <a[high])
{
max = a[high];
min = a[low];
}
else
{
max = a[low];
min = a[high];
}
}
else
{
mid = (low+high)/2;
maxmin(low, mid);
max1 = max; min1 = min;
maxmin(mid+1, high);
if(max <max1)
max = max1;
if(min > min1)
min = min1;
}
}
}
int main ()
{
int i, num;
printf ("\nEnter the total number of numbers : ");
scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=1;i<=num;i++)
scanf ("%d",&a[i]);
max = a[0];
min = a[0];
maxmin(1, num);
printf ("Minimum element in an array : %d\n", min);
printf ("Maximum element in an array : %d\n", max);
return 0;
}
5. Write a c program for Bubble sort.
//Bubble Sort
#include <stdio.h>
int main()
{
int n, num[100], i, j, temp;
/* get the number of entries */
printf("Enter your input for n:");
scanf("%d", &n);
/* get the input data */
for (i = 0; i < n; i++)
scanf("%d", &num[i]);
/* sort the given data in ascending order */
for (i = 0; i < n-1; i++)
{
for (j = i + 1; j < n; j++)
{
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
/* data in ascending order */
printf("Ascending Order:\n");
for (i = 0; i < n; i++)
printf("%d\n", num[i]);
/* data in descending order */
printf("\nDescending Order:\n");
for (i = n-1; i >= 0; i--)
printf("%d\n", num[i]);
return 0;
}
6. Write a c program for Insertion sort.
//insertion sort
#include<stdio.h>
int main()
{
int n,i,j,temp,min,key;
int arr[100];
printf("Enter the size of an array:\n");
scanf("%d",&n);
printf("Enter the elements for an array:");
for(i=0;i<n;i++)
{
scanf("%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 sorted list-----");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
}
7. Write a c program for implementing selection sort.
//selection sort
#include<stdio.h>
int main()
{
int a[20],n,i,min_pos,j,temp;
printf("Enter how many numbers:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++) //last single element sorted automatically
{
min_pos=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min_pos])
min_pos=j;
}
if(min_pos!=i)
{
temp=a[i];
a[i]=a[min_pos];
a[min_pos]=temp;
}
}
//Displaying result
for(i=0;i<n;i++)
{
printf("a[%d]=%d\n",i,a[i]);
}
return 0;
}
8. Write a c program for implementing Quick sort.
9. Write a c program for Fibonacci series.
#include <stdio.h>
int fibonacci(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
//calling the fibonacci() recursively till we get to the base conditions
else
{
// recursively calling the fibonacc() function and then adding them
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
int main()
{
int num;
printf("Enter the number of elements to be in the series : ");
scanf("%d", &num);
int i;
for (i = 0; i < num; i++)
{
printf("%d, ", fibonacci(i));
}
return 0;
}
10.Write a c program for GCD.
#include <stdio.h>
int gcd(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1, n2));
return 0;
}
int gcd(int n1, int n2)
{
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
11.Write a Program to implement the concept of Backtracking: N-queen
Problem.
//NQueen using Backtracing problem:
#include<stdio.h>
#include<math.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)
{
//checking column and digonal conflicts
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1; //no conflicts
}
//function to check for proper positioning of queen
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}