I need help with the following question, I don't want the whole code but I really need help with this section. (DISPLAY THE NUMBER OF KEY COMPARISON FOR EACH SEARCH). Here is the question.
Method 1A
1A-1. Create one array arr1 of size 1000. Populate it with random numbers in the range from 0 to 2000.
1A-2. Randomly generate 5 numbers and try to search each number in the array.
1A-3. Display the number of key comparison for each search.
1A-4. Display a table like the following:
Target Number # of key comparison
356 (Not Found) 1000
cout << "found it at " << dx << "taking " << dx <<"comparisons" ;
if you had a more complex search algorithm, you would maintain a separate count variable and increment it every time you compare. Here, because the index and the count are the same, I didn't need the extra variable.
DISPLAY THE NUMBER OF KEY COMPARISON FOR EACH SEARCH
Yeah, this is slightly weird phrasing for an array, but likely he wants you to code it as jonnin said.
For an unsorted array, you need to check the probe against every element until you find what you're looking for, or not at all. For each of the five generated numbers, you'd loop through the whole array, making up to 1000 comparisons to check if number matches what's in there.
Likely you want a bool variable named "found" or so, so that on match you can set it to true before breaking. At end of that loop, if !found, you can say so and output the array size. For the case of success, you can either remember the last index checked and output that (+1), or keep a count.
Thank you guys.
This is my code I still need to display target numbers and number of key comparison like a table. I figured out to generate random numbers only.
#include <iostream>
#include <ctime>
using namespace std;
int linearSearch(int [], int, int);
void comparison(int [], int);
const int Size = 1000;
int main()
{
int arr1[Size];
srand(time(NULL));
for (int i = 0; i < 1000; i++)
{
if (i == 0)
arr1[i] = 1000;
else
arr1[i] = rand()%2000 + 1;
}
for (int count = 0; count < 5; count++)
{
int index = 0;
int random;
random = rand()%2000 + 1;
cout << "random generated # " << random << endl;
index = linearSearch(arr1, Size, random);
if (index == -1)
{
cout << "The index randam # " << random << " is not in the array. " << endl;
}
else
cout << "The index randam # " << random << " is " << index << endl;
}
cout << "Target Number " << " " << " # of Key Comparison " << endl;
appreciate that.
I implemented this code but still I am getting some errors like ( /// error: expected primary-expression before '}' token at the closing bracket of while loop in function) or (undefined reference to linearSearch(int, int, int, int).'
I need alittle more codes regarding this, I don't know may be I missed in main function couldn't print it the right way.
Thank you for your help.
I fixed the syntax error, but it doesn't give me what I want.
this is the question
Method 1A
1A-1. Create one array arr1 of size 1000. Populate it with random numbers in the range from 0 to 2000.
1A-2. Randomly generate 5 numbers and try to search each number in the array.
1A-3. Display the number of key comparison for each search.
1A-4. Display a table like the following:
and here is my codes.
#include <iostream>
#include <ctime>
using namespace std;
int linearSearch(int [], int, int, int &);
void comparison(int [], int);
void showArray(int [], int);
const int Size = 10;
int main()
{
int arr1[Size];
srand (time(NULL));
for (int i = 0; i < Size; i++)
{
arr1[i] = rand()%2000 + 1;
}
for (int i = 0; i < Size; i++)
{
cout << "Array1 Random generated # " << arr1[i] << endl;
}
cout << endl;
int index = 0;
int random;
int comp;
index = linearSearch(arr1, Size, random, comp);
showArray(arr1, Size);
I made these crude changes to illustrate. Its probably not 100% what you want but its more or less working now. see lines 31, 36, 57. I was wrong about the off by one, its ok.
thanks
this code runs exactly like the one you sent you, I know you added a couple lines but still the output is the same as mine. Any way appreciate your help.
I made some changes to my code but how do you display the table I want.
for example
target numbers # of key comparison
this is my code
#include <iostream>
#include <ctime>
using namespace std;
int linearSearch(const int [], int, int);
const int Size = 1000;
int main()
{
int arr1[Size];
int index = 0;
int target = -1;
srand (time(NULL));
for (int i = 0; i < Size; i++)
{
arr1[i] = rand()%2000 + 1;
}
for (int i = 0; i < 5; i++)
{
cout << "Random generated # " << arr1[i] << endl;
}
cout << endl;
index = 0;
target = 250;
index = linearSearch(arr1, Size, target);
/// show the result of the search
/// if linear search returned -1, then the target is not in the array
if (index == -1)
{
cout << "The Target #: " << target << " is not in the array. " << endl;
}
else
{
cout << "The Target #: " << target << " is " << index << endl;
}
index = 0;
target = 500;
index = linearSearch(arr1, Size, target);
/// show the result of the search
/// if linear search returned -1, then the target is not in the array
if (index == -1)
{
cout << "The Target #: " << target << " is not in the array. " << endl;
}
else
cout << "The Target #: " << target << " is " << index << endl;
index = 0;
target = 980;
index = linearSearch(arr1, Size, target);
/// show the result of the search
/// if linear search returned -1, then the target is not in the array
if (index == -1)
{
cout << "The Target #: " << target << " is not in the array. " << endl;
}
else
cout << "The Target #: " << target << " is " << index << endl;
index = 0;
target = 869;
index = linearSearch(arr1, Size, target);
/// show the result of the search
/// if linear search returned -1, then the target is not in the array
if (index == -1)
{
cout << "The Target #: " << target << " is not in the array. " << endl;
}
else
cout << "The Target #: " << target << " is " << index << endl;
index = 0;
target = 969;
index = linearSearch(arr1, Size, target);
/// show the result of the search
/// if linear search returned -1, then the target is not in the array
if (index == -1)
{
cout << "The Target #: " << target << " is not in the array. " << endl;
}
else
cout << "The Target #: " << target << " is " << index << endl;