Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Chandigarh Engineering College

Department of computer science and engineering


BACHELOR OF TECHNOLOGY
(Computer Science and Engineering)

DAA Lab File

(BTCS 405-18)

NAME: NISHTHA SINGH

ROLLNO: 1902168

SEMESTER: 4th

GROUP: 4V1

UNDER THE GUIDANCE OF

MS KAMLINDER KAUR

(ASSISTANT PROFESSOR)

NISHTHA SINGH (1902168)


PRACTICAL: 1

AIM: Program to find prime numbers out of given list.

PROGRAM:

#include<iostream>

using namespace std;

void prime(int num)

int div=0;

for(int i=1;i<=num;i++)

if(num%i==0)

div++;

if(div==2)

cout<<num<<endl; }

int main(){

cout<<"Enter range:";

int lowerLimit, upperLimit;

cin>>lowerLimit>>upperLimit;

cout<<"Prime numbers between "<<lowerLimit<<" and "<<upperLimit<<" are:


"<<endl;

for(int i=lowerLimit;i<=upperLimit;i++)

prime(i);

return 0;

NISHTHA SINGH (1902168)


}

OUTPUT:

PRACTICAL: 2

AIM: Program to apply linear search.

PROGRAM:

#include <iostream>

using namespace std;

void linearSearch(int a[], int n) {

int temp = -1;

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

if (a[i] == n) {

cout << "Element found at position: " << i + 1 << endl;

temp = 0;

break;

if (temp == -1) {

NISHTHA SINGH (1902168)


cout << "No Element Found" << endl;

int main() {

int arr[5];

cout << "Please enter 5 elements of the Array" << endl;

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

cin >> arr[i];

cout << "Please enter an element to search" << endl;

int num;

cin >> num;

linearSearch(arr, num);

return 0;

OUTPUT:

NISHTHA SINGH (1902168)


PRACTICAL: 3

AIM: Program to apply binary search.

PROGRAM:

#include <iostream>

using namespace std;

int binarySearch(int arr[], int left, int right, int x) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == x) {

return mid;

} else if (arr[mid] < x) {

left = mid + 1;

} else {

right = mid - 1;

return -1;

int main() {

int myarr[10];

int num;

int output;

cout << "enter 5 elements" << endl;

NISHTHA SINGH (1902168)


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

cin >> myarr[i];

cout << "enter an element to search" << endl;

cin >> num;

output = binarySearch(myarr, 0, 9, num);

if (output == -1) {

cout << "Not Found" << endl;

} else {

cout << "Found at position: " << output << endl;

return 0;

OUTPUT:

NISHTHA SINGH (1902168)


PRACTICAL: 4

AIM: Program to write insertion sort.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int i,j,n,temp,a[30];

cout<<"Enter the number of elements:";

cin>>n;

cout<<"\nEnter the elements\n";

for(i=0;i<n;i++)

cin>>a[i];

for(i=1;i<=n-1;i++)

temp=a[i];

j=i-1;

while((temp<a[j])&&(j>=0))

a[j+1]=a[j];

j=j-1;

NISHTHA SINGH (1902168)


}

a[j+1]=temp;

cout<<"\nAfter insertion sort\n";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

return 0;

OUTPUT:

NISHTHA SINGH (1902168)


PROGRAM: 5

AIM: Program to write selection sort.

PROGRAM:

#include<iostream>

using namespace std;

int main()

int i,j,n,loc,temp,min,a[50];

cout<<"Enter the number of elements:";

cin>>n;

cout<<"\nEnter the elements\n";

for(i=0;i<n;i++)

cin>>a[i];

for(i=0;i<n-1;i++)

min=a[i];

loc=i;

for(j=i+1;j<n;j++)

if(min>a[j])

NISHTHA SINGH (1902168)


{

min=a[j];

loc=j;

temp=a[i];

a[i]=a[loc];

a[loc]=temp;

cout<<"\nAfter selection sort\n";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

return 0;

OUTPUT:

NISHTHA SINGH (1902168)


PRACTICAL: 6

AIM: Program to apply Fractional knapsack problem.

PROGRAM:

#include <bits/stdc++.h>

using namespace std;

struct Item {

int value, weight;

Item(int value, int weight)

: value(value), weight(weight)

};

bool cmp(struct Item a, struct Item b)

double r1 = (double)a.value / a.weight;

double r2 = (double)b.value / b.weight;

return r1 > r2;

double fractionalKnapsack(struct Item arr[], int N, int size)

sort(arr, arr + size, cmp);

int curWeight = 0;

double finalvalue = 0.0;

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

NISHTHA SINGH (1902168)


if (curWeight + arr[i].weight <= N) {

curWeight += arr[i].weight;

finalvalue += arr[i].value;

else {

int remain = N - curWeight;

finalvalue += arr[i].value

* ((double)remain

/ arr[i].weight);

break;

return finalvalue;

int main()

int N = 60;

Item arr[] = { { 120, 10 },

{ 280, 40 },

{ 130, 20 },

{ 130, 24 } };

int size = sizeof(arr) / sizeof(arr[0]);

NISHTHA SINGH (1902168)


cout << "Maximum profit earned = "

<< fractionalKnapsack(arr, N, size);

return 0;

OUTPUT:

NISHTHA SINGH (1902168)

You might also like