Your code has two errors. Note that when you call your function you pass 3 addresses as arguments, so far so good, but the subtle detail is that you are passing the address of the vector:
minimax (&v[10],&a,&b);
Since the address is the top position then when the loop goes through the spaces in memory will not be the values stored in the vector, but rather the memory addresses that are located after the vector. An example
// Ao invés de fazer isso
a = v[2]
b = [6]
// O código faz isso
a = v[12]
b = v[16]
These addresses aren’t part of your array, so you don’t change what’s in that memory space, so there’s trash there. To solve you need to pass the address of the first element of the vector, thus:
minimax (v,&a,&b); // Pode fazer assim
minimax (&v[0],&a,&b); // Ou assim
A vector without the brackets by definition points to its first position, i.e., &v[0] == v
The second error of your code is initializing the variables a and b equaling at the zero position of the vector. Note that the vector at the time of initialization of the variables has junk memory at position [0] and therefore you do not know which value will be assigned to the variables. So when you make that comparison:
if (*a<v[i])
In the above comparison the variable "a" has a memory junk, so the value inside can be anything, examples:
if(989++9 < 100) // Variável a
if(-9868768 < 48) // Variável a
if(-986786 > 0) // Variável b
if(0xF756746 > 9) // Variável b
Because of the situation I exemplified is that shows the memory junk, most of the time the memory junk will be out of values between 0 and 100 and so the if will hardly be activated.
The solution to the problem is quite simple, just do this:
int i, a = 0,b = 100;
As "a" holds the highest number then it starts with zero, and "b" equals 100. I recommend you make the changes and then do the table test to better understand why a be equal to zero and b equal to 100. The table test will be more efficient than I try to answer, I will probably complicate a little kkk.
You have to do the assignments
a=v[0]
andb=v[0]
after read the arrayv
. At the point where you put you are assigning memory junk.– anonimo
What do you mean? I didn’t understand what it would look like
– Agatha Garcia
You did not initialize the variables
a
andb
, so the crazy result. Before thefor
of its function placea=v[0] e b=v[0]
– Natan Fernandes