- #36
Mark44
Mentor
- 37,781
- 10,170
Mark44 said:location is an int value, so you cannot dereference it as if it were a pointer.
Also, every one of the variables you declare at the top of main is uninitialized, so when you pass them to your functions, it's GIGO (garbage in, garbage out).
Whenever you pass a value to a function, you need to make sure that you know its value going in. The only exceptions are when the parameter is of some pointer type, which would include array types.
What function? The getOption function was supposed to return a value to the option variable, but you decided not to write this function. Since you don't have a function that sets this variable you need to set it yourself, like, say, to -1.leroyjenkins said:What do I initialize them as? Like for example, I have int option; I thought the function itself gave them values. Do I need to give them all values?
Mark44 said:In your while loop, the first thing you test is that option != 5. How do you know that option doesn't have the value 5?
An uninitialzed variable could have any value in it, including 5. If that happened, your loop wouldn't run at all.leroyjenkins said:If it's 5, the program is supposed to quit. 5 would make the loop stop, right? Is that not a good way to do it?
Mark44 said:In the parameter list, you're telling the compiler that range is an int. Inside the body of the function, you are redeclaring range. You can't do that.
leroyjenkins said:I took it out of the body. Do I need to put it anywhere else, or is it being declared in the function argument?
Mark44 said:Also, and as mentioned now three times, this function is broken! You are setting range to 999. One of the many things you've missed that I've said before is that what you're calling the range needs to be two separate variables, a min. value and a max. value.
You need to decide what fillArray should do. Part of that planning is figuring out what inputs it needs (the arguments) and what, if any, the outputs should be. If you want to specify a range of values for the elements of the array, you need to pass in two values.leroyjenkins said:Sorry about that, I left that the same because I was trying to find the example in my book that gave me the exact syntax I need and I couldn't find it.
Does it just need to be like int minRange; and int maxRange; or something like that?
The header line of a function is a kind of contract between the function and whoever calls it (main in your case).
Mark44 said:What difference do you notice between these two lines of code? These are the function headers for your two search routines. One of them is giving you and error and the other is not.
How did you change it? It could be that you have now broken both functions.leroyjenkins said:Ok I changed that and it's accepted now.
It means you are trying to do division with two types that aren't compatible.leroyjenkins said:I still have it telling me error: invalid operands to binary / (have 'int *' and 'int')
in the binarySearch function, 2 lines under "while". What does that mean?
Code:
int binarySearch (int list, int end, int target, int* location)
{
int first;
int mid;
[color="red"]int last[50];[/color]
first = 0;
last = end;
while (first <= last)
{
[color="red"]mid = (first + last) / 2;[/color]
...
}
The reason for the error is in the 2nd colored line. first is type int and last is type int * (the name of an array is an address - which makes it a pointer (read-only). The result of adding an int and a pointer to an int is an address - a pointer. The final operation on the right side is to divide the address by 2. Division on pointers is not defined, which is the reason for the compiler error.