Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CODE: In the following C program, we use srand function to assign random values to the integer array a[ ] with 100 hundred valuesusing a

CODE:

In the following C program, we use srand function to assign random values to the integer array a[ ] with 100 hundred valuesusing a for loop.

Thn, we ask the user to enter choice of operation. Based on the choice, the switch statement executesthe respective function.

1. Linear search - key tosearch is read from user. Then in the functioin, using afor loop, the entire array is traversed in search of the key. When found, loop breaks and index at which key is found is displayed.

2.Binary search - three variables- beg, last and mid are used for the beginning, last and middle index of the array. Then bubble sort is called to sort the array as binary seach works on sorted array only. then a while loop is used until beg<=last. In the loop, the mid value is compared with key. If found then shoew output and break. If less than key then beg = mid+1 otherwise last = mid-1. Everytime we find the mid index.

3. selection sort- a min index is selected (say fiest value) then all other values are compared with it. if the current value is smaller than min they swap.

4. Bubble sort- array is traversed i = 0 to 100 and then inside this for loop from j=0 to 100-i-1. Inside this loop, it is tested if current value is greater than next value. If yes thenswap the two.

CODE:

#include #include #include void bubsort(int a[]) { int i,j,temp; for(i=0; i<100; i++) { for(j=0; j<100-i-1; j++) { if(a[j] > a[j+1]) //if current value > next value { //swap the values temp =a[j]; a[j] = a[j+1]; a[j+1] = temp; } } }

}

void selsort(int a[]) { int i,j,temp,min; for(i=0; i<100; i++) //to traverse thearray { min = i; //initialising first index as minimum for(j= i+1; j<100; j++) // comparing all values with current if(a[j] < a[min]) //if next element is less than minimum min = j; //then it becomes the minimum //swapping the values at minimum and current index temp = a[i]; a[i] = a[min]; a[min] = temp; } }

void linsearch(int a[], int key) { int i; for(i=0; i<100; i++) { if(a[i] == key) { printf(" %d found at index %d", key, i); return; } } printf(" Key not found!"); }

void binsearch(int a[], int key) { int beg, last, mid; beg = 0; last = 99; //initializing begining and lst index mid = (beg+last)/2; //middle index bubsort(a); //sorting is necessary before searching

while(beg<=last) { if(a[mid] == key) { printf(" %d found at index %d of sorted array", key, mid); return; } else if(a[mid] < key) beg = mid+1; //update beg to mid + 1 else last = mid-1; //update last to mid-1 mid = (beg+last)/2; } printf(" Key not found!"); }

int main() { int a[100],i, choice; int key; time_t t; /* Intializes random number generator */ srand((unsigned) time(&t)); printf("Randomly assigned array: "); for(i=0; i<100; i++) { a[i] = (rand() % 100)+1; printf("%d ", a[i]); } printf(" 1.Linear search 2.Binary search 3.Selection sort 4.Bubble sort "); printf("Enter your choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the key to search: "); scanf("%d", &key); linsearch(a,key); break; case 2: printf("Enter the key to search: "); scanf("%d", &key); binsearch(a,key); break; case 3: selsort(a); printf(" Sorted array: "); for(i=0; i<100; i++) printf("%d ", a[i]); break; case 4: bubsort(a); printf(" Sorted array: "); for(i=0; i<100; i++) printf("%d ", a[i]); break; default: printf("Invalid Input!"); } return 0; }

Preform task:

Above is the code. The code is correct, and it runs perfectly switch the librarys form

#include

#include

#include

to

#include

#include

#include

This change in librarys would require for some of the operations to be changed as well.

ex:

printf();

to

cout<<" ";

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago