Reputation: 33
Hi I'm wondering if anyone could tell me how to set the initial values for my minimums so that they aren't biased by 0 initial value. The program is to look at comparisons in time it takes to run a binary versus linear search. It is homework and I was about to turn it in when I realized what I had done. I have now thoroughly messed my code up trying to fix but I'll try to piece it back the way it was. Thanks for any help you can give me.
Will include more code if needed but didn't want to get a negative comment for including too much but decided this was best and then I could add more if needed. I'm sure its so obvious like something like you should use a while loop or something but my brain is tired and I can't see it tonight. So please don't laugh!!! Thanks again.
using namespace std;
int main()
{
const int arraySize = 1000;
int numberArray[arraySize];
int searchKey = 0, linearCount = 0, binaryCount = 0,
linearMin = 0, linearMax = 0, linearTotal = 0,
binaryMin = 0, binaryMax = 0, binaryTotal = 0;
double linearAverage = 0 , binaryAverage = 0;
srand (time(NULL));
for (int loopCount = 0; loopCount < arraySize; loopCount++)
{
// gets the random number array populated with 1000 elements
getRandomNumber(numberArray, arraySize);
// gets the search key from one of the random numbers in the array
searchKey = getSearchKey(numberArray, arraySize);
// begins the comparisons for the linear Count
linearCount = doLinearSearch(numberArray, arraySize, searchKey);
// Sort method used was bubbleSort for the requirement of binary search
bubbleSort(numberArray, arraySize);
binaryCount = doBinarySearch(numberArray, arraySize, searchKey);
//linearMin = linearCount; HERE'S WHAT I ORIGINALLY HAD BUT NOW KNOW I GOOFED
/* sets the linear Minimums and Maximums and keeps a running total for average
puts the smallest number of times in linearMin and loops through until
it finds the next smallest if any and assigns it this count number*/
if (linearCount < linearMin) linearMin = linearCount;
if (linearCount > linearMax) linearMax = linearCount;
linearTotal = linearTotal + linearCount;
linearAverage = 1 * linearTotal / arraySize;
// sets the binary Minimums and Maximums and keeps a running total for average
if (binaryCount < binaryMin) binaryMin = binaryCount;
if (binaryCount > binaryMax) binaryMax = binaryCount;
binaryTotal = binaryTotal + binaryCount;
binaryAverage = 1 * binaryTotal / arraySize;
}
// Display results
cout << "After 1000 tests on " << arraySize << " element arrays, \n";
"The linear search results were:\n";
cout << "The minimum number of comparisons to find the key was: " << linearMin << endl;
cout << "The maximum number of comparisons to find the key was: " << linearMax << endl;
cout << "The average number of comparisons to find the key was: " << linearAverage << endl;
cout << "After 1000 tests on " << arraySize << " element arrays, \n";
"The binary search results were:\n";
cout << "The minimum number of comparisons to find the key was: " << binaryMin << endl;
cout << "The maximum number of comparisons to find the key was: " << binaryMax << endl;
cout << "The average number of comparisons to find the key was: " << binaryAverage << endl;
} // End Main
I AM SO SORRY IF I OFFEND ANYONE HERE BUT... Realize that I am in tears, yes tears because originally I had a program written that was working and yes this was homework. I saw was because on Sunday right before it was do I was putting in some last minute comments when it hit me that the values coming back could not be right because it was I thought finding minimumValue everytime based on the last time of my loop. Mind you there are two parts which I was having trouble with part 2 as well but thought at least I could some credit turning in part 1 till my discovery which had I not noticed at least I would have been better than a 0. I AM NOT WANTING ANYONE TO DO MY HOMEWORK FOR ME! But for all the forums out there I am beginning to think who can be the rudest to the newbie or who can talk enough techno babble or who can get the most points and try impressing people has become more important than the concept of paying it forward means. I'm sure nobody cares out there but as of today I've had like 5 hours of sleep since Sunday, I happen to find some text messages confirming my husband (not for long) is cheating on me yet again, I have asked for help from a slew of people who have since caused my somewhat working and what at least resembled a program and now looks like Freddy Kruger has been learning how to write C++. At this point I'm pretty sure my 3.87 GPA I had is being drop kicked down the hall but the bottom line is whether or not I get a miracle to turn it in really doesn't matter now because if I can't figure it out than I should give up now. Most assignments build on the things learned so if I can't do this what will I do next assignment. I'm sorry but I am a little slow on this but as I do grasp the concepts eventually I just need some whys and hows sometimes and right now with this post I just need someone who could possibly be less interested in their reputation or in making me feel worse than I already do and someone who could tell me why this didn't work right. Forums you say are for help well can someone please help me?
What I was supposed to accomplish is this populate 1000 element array with random numbers create a search key from that array and assign it said value do Linear Search and count # of times it takes to do it Sort array in order Do same on binary search In main call all 5 functions in a loop to get data for a 1000 iterations display results of loop with min comparisons, max and average for each
The random generator and search key part did work individually and I will post if needed but here is code for the searches and sort
int doLinearSearch(const int randomNumberArray[], const int arraySize, const int key)
{
int index = 0,
position = -1,
ctComparisons = 0;
bool keyfound = false;
while (index < arraySize && !keyfound)
{
if (randomNumberArray[index] == key)
{
keyfound = true;
position = index;
}
index++;
}
ctComparisons = index + 1;
return ctComparisons;
}
void bubbleSort(int randomNumberArray[], const int arraySize)
{
bool swapFlag;
int tempHolder;
do
{
swapFlag = false;
for (int count = 0; count < (arraySize - 1); count++)
{
if (randomNumberArray[count] > randomNumberArray[count + 1])
{
tempHolder = randomNumberArray[count];
randomNumberArray[count] = randomNumberArray[count + 1];
randomNumberArray[count + 1] = tempHolder;
swapFlag = true;
}
}
} while (swapFlag);
}
int doBinarySearch(const int randomNumberArray[], const int arraySize, const int key)
{
int firstElement = 0,
lastElement = arraySize - 1,
middleSearch,
position = -1,
ctComparisons = 0;
bool keyFound = false;
while (!keyFound && firstElement <= lastElement)
{
middleSearch = (firstElement + lastElement) / 2;
if (randomNumberArray[middleSearch] == key)
{
keyFound = true;
position = middleSearch;
}
else if (randomNumberArray[middleSearch] > key)
lastElement = middleSearch -1;
else
{
firstElement = middleSearch + 1;
}
ctComparisons ++;
}
return ctComparisons;
}
I have tried the suggestions that everyone has given and then some but thats not my problem please I don't mean to be all needy or a pest I just want to learn and would be truly appreciative and will promise one day to pay it forward as well.
I run it and it does not seem to actually be putting my comparison count in to anything but the last iteration of my loop. The numbers I get for the maximum are all over the board. So can you tell me what's causing this to be nightmare. I know it is so simple that it is probably laughable but please.... Will post all of it if you want I just figured I was going to be yelled at already for too much info...
Upvotes: 1
Views: 1425
Reputation: 24249
Your question appears to be: How do I make my "min" value detect values from inside my loop.
int array[5] = { 5, 4, 3, 2, 1 };
int minVal = /* what */;
for (size_t i = 0; i < 5; ++i) {
if (array[i] < minVal)
minVal = array[i];
}
// minVal should be 1, not 0.
The answer is: set minVal to something >= your maximum value. If you know what the constraints are, that makes it easy:
int minVal = LARGEST_POSSIBLE_VALUE;
You can achieve this with the syntax:
unsigned int minVal = ~0;
the 1s complement of 0 which will be the maximum value allowed with all bits set to 1. But if you're using a signed it, it will set it to -1. Of course, you can be more specific there and do
int minVal = -1;
and then make your check
if (minVal == -1 || array[i] < minVal)
minVal = array[i];
Unless you absolutely need to optimize out the extra test.
Or you can use climits
#include <climits>
int minVal = INT_MAX;
Now your minVal will be as-big-as-it-can-get until a smaller value is found. Since it is possible that at the end of your loop it could be INT_MAX either by assignment or by no match being found, make sure you have some way of determining which case it is.
Upvotes: 3
Reputation: 4753
Declare a variaable of scope Static
..so that it will not get initialized again and again..
Upvotes: -2