Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE: In the following C program, we use srand function to assign random values

ID: 3730310 • Letter: C

Question

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<<" ";

Explanation / Answer

#include <iostream>

#include<cstdlib>

#include <ctime>

using namespace std;

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)

{

cout <<" "<< key <<"found at index "<< i;

// 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)

{

cout <<" "<< key << "found at index "<< mid <<" of sorted array";

//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!");

cout<<" 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: ");

cout<<"Randomly assigned array: "<<endl;

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

{

a[i] = (rand() % 100)+1;

printf("%d ", a[i]);

}

cout<<" 1.Linear search 2.Binary search 3.Selection sort 4.Bubble sort "<<endl;

//printf(" 1.Linear search 2.Binary search 3.Selection sort 4.Bubble sort ");

cout<<"Enter your Choice: "<<endl;

// printf("Enter your choice: ");

// scanf("%d", &choice);

cin>>choice;

switch(choice)

{

case 1: cout<<"Enter the key to search: ";

//scanf("%d", &key);

cin>>key;

linsearch(a,key);

break;

case 2: cout<<"Enter the key to search: ";

cin>>key;

binsearch(a,key);

break;

case 3: selsort(a);

//printf(" Sorted array: ");

cout<<" Sorted array: "<<endl;

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

cin>>a[i];

// printf("%d ", a[i]);

break;

case 4: bubsort(a);

//printf(" Sorted array: ");

cout<<" Sorted array: "<<endl;

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

cin>>a[i];

// printf("%d ", a[i]);

break;

default: cout<<"Invalid Input!";//printf("Invalid Input!");

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote