Question

Write a C++ program that does the following : Accepts a positive integer ( n )...

Write a C++ program that does the following :

Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following

1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20

2. In the second function : Using recursion, Search for a char ( 80 ) in the array using sequential search and at the end display number of comparisons it makes.

3. In the third function : Sort the original array using selection Sort and at the end display the number of swaps it makes. Display elements of the array.

4. In the fourth function : Sort the original array using insertion Sort and at the end display the number of comparisons it makes.

5. In the fifth function : Sort the original array using Quick Sort and at the end display the number of recursion calls it makes. Use the next to the middle value as a pivot value.

6. In the sixth function : Sort the original array using Quick Sort and at the end display the number of recursion calls it makes. Use the first value as a pivot value. Display elements of the array.

7. In the last function : Sort the original array using Quick Sort and at the end display the number of recursion calls it makes. Use the last value as a pivot value. Display elements of the array.

8. For each of the preceding steps ( 2 thru 7 ), calculate and print the CPU time before each step starts and after each completed step then calculate actual time for the completion of each step. Time should be displayed in seconds and milliseconds Display elements of the array. Display the first 20 elements If the size is > 20

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <iostream>

using namespace std;
void displyArray(char arr[],int size){
int i;
if(size>=20){
cout<<"\n";
for(i=0;i<size;i++){
cout<<arr[i]<<" ";
}
}else{
cout<<"array size is less than 20";
}
  
}

//sequential search using recursion
int searchNumber(char arr[],int start,int size,char c){
int noOfComparision=0;
int found=0;
if(start<size && found==0 ){
if(arr[start]=='c'){

found=1;
cout<<"no of Comaparistion is to search char(80) is ="<<noOfComparision;
}else{
noOfComparision=noOfComparision+1;
searchNumber(arr,start+1,size,c);//recursive call
}
}
return found;
  
}

int sectionsort(char arr[],int size){
  
int i, j, min_index,swapCount=0;
  
// One by one move boundary of unsorted subarray
for (i = 0; i < size; i++)
{
// Find the minimum element in unsorted array
min_index = i;
for (j = i+1; j < size; j++)
if (arr[j] < arr[min_index])
min_index = j;
  
// Swap the found minimum element with the first element
  
char temp=arr[min_index];
arr[min_index]=arr[i];
arr[i]=temp;

swapCount=swapCount+1;
}
cout<<"\n";
cout<<"sorting using selection sort ";
for(i=0;i<size;i++){
cout<<arr[i]<<" ";
}
return swapCount;
}


//insertion sorting
int insertionSort(char arr[], int arr_size){
int count=0;
int i;
//chechking the boundry
if(arr_size > 1){
int size = arr_size;
for(int i = 1; i < size; ++i){
int j = i - 1;
int key = arr[i];
//comparing
while(j >= 0 && arr[j] > key){
arr[j+1] = arr[j];
--j;
}
arr[j+1] = key;
count=count+1;//counting no of swaps
}
}
cout<<"\n";
cout<<"sorting using insertion sort ";
for(i=0;i<arr_size;i++){
cout<<arr[i]<<" ";
}
return count;
}



int partition(char a[], int l, int r)
{
int p, i, j, t;
p = l;
i = l;
j = r;
while(i<j)
{
while((a[ i ] <= a[ p ]) && (i<r))
{
i++;
}
while(a[ j ] > a[ p ])
{
j--;
}
if(i<j)
{
t=a[ i ];
a[ i ] = a[ j ];
a[ j ] = t;
}
}
t=a[ p ];
a[ p ] = a[ j ];
a[ j ] = t;
return j;
}
//partion for taking last elemtn as pivot element

int lastpartition(char a[],int l,int r)
{
int j,temp,i=l;

for(j=l;j<r-1;j++)
{
//swap values if a[j]<=a[r-1](i.e. pivot)
if(a[j]<=a[r-1])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
i++;
}
}
//place pivot at its position by swapping
temp=a[i];
a[i]=a[r-1];
a[r-1]=temp;
return i;
}
//quick sort for first element as a pivot element
void quicksortFirstpivot(char a[], int l, int r)
{ int i;
int s;
if(l < r)
{
s=partition(a,l,r);
quicksortFirstpivot(a, l, s-1);
quicksortFirstpivot(a, s+1, r);
}

  
}
//quick sort for last element as a pivot element
void quicksortlastpivot(char a[], int l, int r)
{ int i;
int s;
if(l < r)
{
s=lastpartition(a,l,r);
quicksortlastpivot(a, l, s-1);
quicksortlastpivot(a, s+1, r);
}

  
}



//Qucik sort for middle element as pivot element

int quicksortMiddle(char arr[],int low,int high)
{
if(low>=high)
return 0;

int mid=(low+high)/2;
int pivot=arr[mid];
int i=low,j=high;
int temp;
while(i<j)
{
if(arr[i]>=pivot && arr[j]<=pivot)
{
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
i++;
j--;
}
else
{
i++;
}
}
quicksortMiddle(arr,low,mid);
quicksortMiddle(arr,mid+1,high);
}
int main()
{
int i,n;
int position=0;
int flag=0;
cout<<"Enter positive integer";
cin>>n; //reading input from user
char arr[n];
  
//generating random numbers and convert them into char
//store the charectors into arr[]
for (i = 0; i < n; i++) {
int num = (rand() % (126 - 33 + 1)) + 33;
char c=(char)num;
arr[i]=c;

}
//method for dispalying charectors in array
displyArray(arr,n);
//searching the element in the array
int res=searchNumber(arr,0,n,char(80));
if(res==0){
cout<<"\n"<<"char(80) not found";
}
  
//selection sorting array call
cout<<"\n"<<"swapCount in selection sort is= "<<sectionsort(arr,n);
  
//insertion corting call
cout<<"\n"<<"swapCount in insertion sort ="<<insertionSort(arr,n);
  
//Quick sort: 1st element as pivot element
quicksortFirstpivot(arr,0,n-1);
cout<<"\n";
cout<<"Quick sort: 1st element as pivot element ";
for(i=0;i<n;i++){
cout<<arr[i]<<" ";
}
//Qucik sort :last element as pivot value
quicksortlastpivot(arr,0,n-1);
cout<<"\n";
cout<<"Qucik sort :last element as pivot value ";
for(i=0;i<n;i++){
cout<<arr[i]<<" ";
}
//quicksort :Middle element as pivot element
quicksortMiddle(arr,0,n-1);
cout<<"\n";
cout<<"quicksort :Middle element as pivot element ";
for(i=0;i<n;i++){
cout<<arr[i]<<" ";
}
}nter positive integer20 v%b82 char (80) not found sorting using selection sort % + 1 2 2 3 5 7 8 E M R S b e j swapCount in s

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that does the following : Accepts a positive integer ( n )...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • 7eatng that function Write a C++ program that does the following: Fill an array of 123...

    7eatng that function Write a C++ program that does the following: Fill an array of 123 elements using srand) and rand) with random numbers between 150 and 667. Fill an array of 123 elements with random numbers between 150 and 667. Using a loop. Fill an array of 123 elements with random numbers between 150 and 667. Using a for loop Use a void function to fill an array of 123 elements with random numbers between 150 and 667, Possible...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Write a C++ program that does the following : 1. Accepts array size from the keyboard....

    Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...

  • - Write a program that performs several operations on an array of positive ints. The program...

    - Write a program that performs several operations on an array of positive ints. The program should prompt the user for the size of the array (validate the size) and dynamically allocate it. Allow the user to enter the values, do not accept negative values in the array. - Your program should then define the functions described below, call them in order, and display the results of each operation: a) reverse: This function accepts a pointer to an int array...

  • Please include all .cpp/.h files if neccesary, this is in C++ Sort an array of 10,000...

    Please include all .cpp/.h files if neccesary, this is in C++ Sort an array of 10,000 elements using the quick sort algorithm as follows: a- sort the array using pivot as the middle element of the array. b- Sort the array using pivot as the median of the fist,last and middle elements of the array . c- sort the array using pivot as the middle element of the array.However ,when the size of any sub list reduces to less than...

  • Write a C++ program that will create an array with a given number of elements. Use...

    Write a C++ program that will create an array with a given number of elements. Use size = 10 for demonstration purposes, but write the program where the size of the array can be changed by changing the value of one constant. Fill the array with random numbers between 0 and 100. Write the program to perform the following operations: 1. Find and display the largest value in the array. 2. Find and display the smallest value in the array....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT