Strange errors

Pages: 123
Ok I've figured out that my error above is actually dealing with this:
1
2
3
4
Search::~Search()
{
  delete [] array;
}


Am I just deleting an array that isn't even there? That's what I'm thinking at least.
Can you show me your constructor again?
1
2
3
4
Search::Search(int size, int seed) : size(size), seed(seed)
{
	array = new double[size];
}
Ah, I missed a mistake from earlier:
1
2
3
4
5
6
7
8
9
10
void Search::initialize_array()
{
	int i=0;
	srand(seed);
	for(i; i<=getSize();i++) //<-- problem here
	{
		double number = rand() % 1000;
		array[i]=number;
	}
}
You used <= instead of <

For reference, 99% of the time I never use <=, so if you find yourself using <=, ask yourself if you're really supposed to be using it there ;)
Last edited on
With the size(size) and seed(seed), do I need to define &/ declare them inside my class .cpp and .h? Or would my set_seed(int seed) take care of seed(seed) and vice versa with size?
I'm confused as to what you're talking about, can you clarify?
For this part of the constructor size(size), seed(seed), do I need to define them as their own separate functions? For example:
1
2
3
4
5
6
7
8
9
datatype Search::size(size)
{
 // Some data;
}

datatype Search::seed(seed)
{
  // Some data;
}

Or do I not do anything for it? I've just never seen a constructor like this one, so I'm just wondering if I might be forgetting something with it.
No, this part of the constructor is called an initializer list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Example_Class
{
    int member1;
    std::string member2;

    Example_Class()
    : member1(7)
    , member2("Default value")
    {
    }
    Example_Class(std::string param1, int param2)
    : member1(param2)
    , member2(param1)
    {
    }
};
In the case of your code, it just happens that the member and parameter have the same name.
In the case of your code, it just happens that the member and parameter have the same name.

Which, incidentally, is something that many programmers avoid, in order to avoid confusion. We usually adopt some kind of naming convention that clearly differentiates between data members and other variables.

Common conventions include:

1
2
3
4
5
int m_size;

int _size;

int size_;
In reality there is no need for such a thing as that since you would never have a direct 1-to-1 relationship between your class members and your constructor parameters, so that notation is generally only useful for examples - however this is entirely opinion and based on my experience, it may not reflect your experience.
Last edited on
Well it has other benefits too - you can see at a glance when a variable is part of the state of the object (i.e. a data member), rather than having to check the declaration. It's especially useful when - as is often the case in professional software development - other people will need to be able to read and quickly understand your code at some point in the future.
Ok that makes sense. None of my professors have taught/shown me that kind of constructor before. My current professor tends to just read certain parts from our textbook during class and is always a week ahead. In other words, he's "teaching" us next week's subject while we're still trying to figure out last week's project.
Where would I find a good reference for using "time.h" to time my three types of searches? I need to use each of my search functions and time how long it takes each search to find the number.
This very site has an extensive reference section, including a C library reference.
In C++, you would use #include <ctime> and not #include <time.h>
1
2
3
4
5
6
7
8
9
10
	clock_t start = clock();
			{
				for(int i = 0; i < N; i++)
				{
					search.sequential_search(467);
				}
			}
			clock_t end = clock();
			clock_t diff = end - start;
			cout<<"It takes Sequential Search "<<((diff/N * CLOCKS_PER_SEC)*1,000,000)<<"micro-seconds"<<endl;

This is what I've gotten so far but I'm still coming up with 0 for my answer.
N is const int N = 100;
Comma operator is different than comma in math you are basically just multiplying by 1 and not 1 million. Also you are doing integer division which results in an integer. I would suggest diff / static_cast<double>( N ) diff / (double) N
or define N as a const double instead of integer.
Change 1,000,000 to 1000000
1
2
3
4
5
6
7
void Search::initialize_array()
{
	for(int i=0; i < size; i++)
	{
		Array[i]=rand() % size;
	}
}

From the various examples on this site, this should initialize the array with random numbers until the index reaches the size of the array but I'm only getting one element to show with a random number when I debug it. It goes through the for loop like it should and i increments and gets a new random number appointed to that particular index but when I check the array in the debugger, it only shows the first element with it's random number. Am I forgetting something here?
When ever you want to use rand() you should call srand() once to give it a seed. The way to make it look the most random is to use the system clock since it it based on ms from midnight I believe so it would be hard to get the same results twice.

Basically just throw in a srand( time( NULL ) ); In your main function or in the initialize array.

http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand
Pages: 123