Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ I'm having trouble with the second question. Thank you! Here is the code I have for the first one: #include #include #include using namespace

C++ I'm having trouble with the second question. Thank you! image text in transcribed

image text in transcribed

Here is the code I have for the first one:

#include

#include

#include

using namespace std;

//sort function to n sort colors given in array A[]

void sort(string A[],int n)

{

int i,j;

string temp;

for(i=0;i

{

for(j=0;j

{

if(A[j]>A[j+1])

{

temp=A[j];

A[j]=A[j+1];

A[j+1]=temp;

}

}

}

}

void print(string A[],int n)

{

cout

for(int i=0;i

{

cout

}

}

//linear search function

bool linear_search(const string A[],int n,string colour,int &count)

{

int i=0;

count=0;

while(i

{

count++;

if(A[i]==colour)

{

return(true);

}

i++;

}

return(false);

}

//binary search function

bool binary_search(const string A[],int n,string colour,int &count)

{

int l=0,r=n-1,m;

count=0;

while(l

{

count++;

m=l+(r-1)/2;

if(A[m]==colour)

{

return(true);

}

else if(A[m]>colour)

{

r=m-1;

}

else

{

l=m+1;

}

}

return(false);

}

//resize function

string *resize(string *A,int n)

{

string *col;

col=(string*)malloc(n*sizeof(string));

for(int i=0;i

{

col[i]=A[i];

}

return(col);

}

//main driver function to use above functions

int main()

{

string A[50],colour;

int i=-1,n,count;

do

{

i++;

cout

//cin>>A[i];

getline(cin,A[i]);

}while(A[i]!="#");

n=i;

//print(A,n);

//sort(A,n);

//cout

//print(A,n);

do

{

cout

getline(cin,colour);

if(colour!="#")

{

if(linear_search(A,n,colour,count))

{

cout

}

else

{

cout

}

}

if(colour!="#")

{

if(binary_search(A,n,colour,count))

{

cout

}

else

{

cout

}

}

}while(colour!="#");

}

1. In this problem, you will write a program (colour1.cc) that determines if a colour can be found in a given list of colours, (a) Write the function: void sort(string AC, int n) which sorts the array A of n elements in ascending oder. You may use any sorting algorithm you learned from CPSC 1620. b) Write the function: bool liner erch(const string AC, int n, string colour, int kcount); which returns true if the string stored in colour can be found in the array A of n elements, and false otherwise. Use the linear search algorithm. The count parameter should return the mber of comparisons made to array elements. (c) Write the function: bool binary.search(const string A int, string colour, int count) which returns true if the string stored in colour can be found in the array A of n elements, and false otherwise. Use the binary search algorithm. The count parameter should return the number of comparisons made to array elements. (d) Write the function: string resize(string *A, int n) which increases the size of an array of n elements to n1 elements. The first n elements should be copied to the new array. The pointer to the array is returned, and the original array passed into resize is deleted. An array of size 0 should be represented by the nullptr pointer. (e) Write the function: void search-and-report (const tring AD], int n, tring colour string label bool (-search) (const string A, int n string colour, int &count)): which calls the supplied search function on the array A to search for the given colour, and reports whether the element is found and the number of comparisons required to reach the conclusion. The label parameter is used to identify the search algorithm used. See the sample session belon for the output format. (f) Write the main program which asks the user to enter a list of colours as strings (may contain spaces), one per line, until line consisting a single # is entered. The list is then sorted (internally). The program then asks the user to enter a colour to search for, and reports whether the colour is in the list and the number of comparisons it takes for the two search algorithms to reach the conclusions The prograrn should repeatedly ask for colours to search for until a single # is entered. Make sure that there is no memory leak Sample session Enter a colour (# when done): red Enter a colour (# when done) : light pink Enter a colour (# when done): dark green Enter a colour (# when done): blue Enter a colour (# when done): # Search (# when done) : light pink Linear search: found, comparisons = 3 Binary search: found, comparisons 3 Search (# when done): black Linear search: not found, comparisons 4 Binary search: not found, comparisons -4 Search (# when done): blue Linear search: found, comparisons 1 Binary search: found, comparisons3 Search (# when done): # Note: depending on how you count the comparisons and your particular impleme tation of linear and binary search functions, the number of comparisons may be dif- ferent 2. Write a second version of the colour program above (colour2.cc) which uses a vector of strings instead of a dynamic array. Notice that there is no need to keep track of the size of the vector vourself<>

<>

<>

<-1>

<>

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

Students also viewed these Databases questions