For 1. and2.: ??Prompt the user toenter an integer X in the range of [1,1010]; ?
ID: 3619214 • Letter: F
Question
For 1. and2.:
??Prompt the user toenter an integer X in the range of [1,1010];
?? When the user press “Enter” your program has togenerate and print on the screen X random numbers;
?? Prompt the user to press a key to run the sorting algorithm;
?? Starting with a new line print out the ordered X numbers;
?? On a separate row print the time taken to run theexperiment.
For3.:
?? clear screen
?? Prompt the user to type the name of a .txt file. The file willcontain two sequences.
Each sequence may contain numbers and/or letters. The elements ofthe sequence will be separated by a “blank”. Eachsequence will be given in a separate line.;
?? Read the sequences from the .txt file. Apply the Longest CommonSubsequence Algorithm;
?? Print in the input file the longest common subsequence found byyour software.
Explanation / Answer
//Header filesection
#include"stdafx.h"
#include<iostream>
#include<cstdlib>
using namespacestd;
inta[50];
//functiondefinitions
voidmerge(int,int,int);
void merge_sort(intlow,int high)
{
intmid;
if(low<high)
{
mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}//endmerge_sort
void merge(int low,intmid,int high)
{
inth,i,j,b[50],k;
h=low;
i=low;
j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(k=low;k<=high;k++)a[k]=b[k];
}//endmerge
voidmain()
{
intnum,i;
cout<<endl<<endl;
cout<<"Please Enter NUMBER OF ELEMENTS"<<endl;
cin>>num;
cout<<endl;
//Generatingrandom numbers
for(i=1;i<=num;i++)
{
a[i]=1+rand()%100;
}
//Displayingbefore sorting
cout<<"Before sorting:";
for(inti=0;i<num;i++)
cout<<a[i]<<"";
//function callto sort
merge_sort(1,num);
//DisplayingAfter sorting
cout<<"Sortedlist:"<<endl;
for(i=1;i<=num;i++)
cout<<a[i]<<" ";
//pause systemfor a while
system("pause");
}//endmain
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.