Matteo Del Seppia - 3 months ago 18

C Question

look at this:

`int find_grad(int * table, int * value)`

{

int i = 0;

while (find_max_value(table) >= value)

{

table[(find_max_position(table))] = 0;

i++;

}

return i;

}

This is a pretty simple function: it uses another function (that works for sure, i tested it) called "find_max_position" that iterates through a vector of integers to find the position of the max value. So if the vector is {1, 3, 19}, find_max_position returns an integer, in this case 1.

I need find_grad to find the "grade" of the value I entered. For example, if I call find_grad(table, 3) on a vector like this {0, 1, 5, 4, 3}, the function should return me 2, because 3 is the third bigger value (it starts from 0).

But when I call the function the loop becomes infinite. I thought that it could be because when I do "table[(find_max_position(table))] = 0;" I'm acting over a copy of the vector and not the vector itself, and when the loop restarts the bigger value it's always there. Maybe I explained the situation a bit bad but I think it's easy to understand reading the code. Can anybody help?

EDIT: I forgot this > find_max_value just returns an integer, the bigger value found on a vector of integers.

Answer

Problem is you are comparing an integer with a pointer. You missed a `*`

before `value`

.

Correct code:

```
int find_grad(int * table, int * value)
{
int i = 0;
while (find_max_value(table) >= *value)
{
table[(find_max_position(table))] = 0;
i++;
}
return i;
}
```

Source (Stackoverflow)

Comments