0% found this document useful (0 votes)
22 views9 pages

DAA EVA LAB 3

The document contains code for three programming tasks involving array manipulation, searching algorithms, and prime number checking. Task 1 includes functions for summing an array and solving the Tower of Hanoi, Task 2 implements linear and binary search along with bubble sort, and Task 3 checks for prime numbers in randomly generated arrays. Each task measures and outputs execution time for various input sizes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views9 pages

DAA EVA LAB 3

The document contains code for three programming tasks involving array manipulation, searching algorithms, and prime number checking. Task 1 includes functions for summing an array and solving the Tower of Hanoi, Task 2 implements linear and binary search along with bubble sort, and Task 3 checks for prime numbers in randomly generated arrays. Each task measures and outputs execution time for various input sizes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name ANKIT RAJ

SRN 02fe23bcs093

Division A

TASK 1
#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

int find_sum(int a[], int n) {

int sum = 0;

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

sum += a[i];

return sum;

void moveDisks(int n, char fromPeg, char toPeg, char auxPeg, int &moveCount) {

if (n == 1) {

cout << "Move disk 1 from " << fromPeg << " to " << toPeg << endl;

moveCount++;

return;

moveDisks(n - 1, fromPeg, auxPeg, toPeg, moveCount);

cout << "Move disk " << n << " from " << fromPeg << " to " << toPeg << endl;

moveCount++;

moveDisks(n - 1, auxPeg, toPeg, fromPeg, moveCount);

int main() {

const int SIZE = 1000;


const int ITER = 10000;

int a[SIZE];

srand(static_cast<unsigned int>(time(0)));

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

a[i] = rand() % 1000;

clock_t start = clock();

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

int sum = find_sum(a, SIZE);

clock_t end = clock();

double duration = static_cast<double>(end - start) / CLOCKS_PER_SEC;

cout << "Average time taken per iteration to sum array: " << duration / ITER << " seconds" << endl;

int numDisks;

cout << "Enter the number of disks for Tower of Hanoi: ";

cin >> numDisks;

int moveCount = 0;

clock_t hanoiStart = clock();

moveDisks(numDisks, 'A', 'C', 'B', moveCount);

clock_t hanoiEnd = clock();

double hanoiDuration = static_cast<double>(hanoiEnd - hanoiStart) / CLOCKS_PER_SEC;

cout << "Total moves made: " << moveCount << endl;

cout << "Time taken to solve Tower of Hanoi: " << hanoiDuration << " seconds" << endl;

return 0;

OUTPUT
TASK 2
#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

int linearSearch(int arr[], int n, int x) {

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

if (arr[i] == x) return i;

return -1;

int binarySearch(int arr[], int n, int x) {

int low = 0, high = n - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] == x) return mid;

if (arr[mid] < x) low = mid + 1;

else high = mid - 1;

return -1;

void bubbleSort(int arr[], int n) {

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

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

if (arr[j] > arr[j + 1]) {

swap(arr[j], arr[j + 1]);

void generateRandomArray(int arr[], int size) {


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

arr[i] = rand() % 1000; // Generates random numbers from 0 to 999

void measureTime(int (*searchFunc)(int[], int, int), int arr[], int n, int x, double& timeTaken) {

clock_t start = clock();

searchFunc(arr, n, x);

clock_t end = clock();

timeTaken = double(end - start) / CLOCKS_PER_SEC; // Time in seconds

int main() {

srand(time(0));

int inputSizes[] = {100, 500, 1000, 5000, 10000, 50000};

double linearSearchTimes[6];

double binarySearchTimes[6];

int arr[50000];

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

int size = inputSizes[i];

generateRandomArray(arr, size);

int x = arr[rand() % size]; // Randomly select an element to search for

measureTime(linearSearch, arr, size, x, linearSearchTimes[i]);

bubbleSort(arr, size);

measureTime(binarySearch, arr, size, x, binarySearchTimes[i]);

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

cout << "Input Size: " << inputSizes[i] << endl;

cout << "Linear Search Time: " << linearSearchTimes[i] << " sec" << endl;

cout << "Binary Search Time: " << binarySearchTimes[i] << " sec" << endl;
}

return 0;

OUTPUT
Input Size: 100

Linear Search Time: 0.0001 sec

Binary Search Time: 0.0000 sec

Input Size: 500

Linear Search Time: 0.0004 sec

Binary Search Time: 0.0000 sec

Input Size: 1000

Linear Search Time: 0.0010 sec

Binary Search Time: 0.0001 sec

Input Size: 5000

Linear Search Time: 0.0081 sec

Binary Search Time: 0.0003 sec

Input Size: 10000

Linear Search Time: 0.0145 sec

Binary Search Time: 0.0005 sec

Input Size: 50000

Linear Search Time: 0.0783 sec

Binary Search Time: 0.0014 sec


TASK 3 QUESTION 3

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

using namespace std;

bool isPrime(int num) {

if (num <= 1) return false;

if (num == 2 || num == 3) return true;

if (num % 2 == 0 || num % 3 == 0) return false;

for (int i = 5; i * i <= num; i += 6) {

if (num % i == 0 || num % (i + 2) == 0)

return false;

return true;
}

void generateRandomArray(int arr[], int size) {

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

arr[i] = rand() % 1000;

int main() {

srand(time(0));

int inputSizes[] = {100, 500, 1000, 5000, 10000, 50000};

int numSizes = sizeof(inputSizes) / sizeof(inputSizes[0]);

for (int j = 0; j < numSizes; j++) {

int size = inputSizes[j];

int* arr = new int[size];

generateRandomArray(arr, size);

clock_t start = clock();

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

bool result = isPrime(arr[i]);

clock_t end = clock();

double duration = static_cast<double>(end - start) / CLOCKS_PER_SEC;

cout << "Array Size: " << size << " - Time taken: " << duration << " seconds" << endl;

delete[] arr;

return 0;

OUTPUT
Array Size: 100 - Time taken: 3e-06 seconds
Array Size: 500 - Time taken: 8e-06 seconds

Array Size: 1000 - Time taken: 1.9e-05 seconds

Array Size: 5000 - Time taken: 9e-05 seconds

Array Size: 10000 - Time taken: 0.000161 seconds

Array Size: 50000 - Time taken: 0.000791 seconds

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy