This loop is infinite but it should not be - C -


look @ this:

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

this pretty simple function: uses function (that works sure, tested it) called "find_max_position" iterates through vector of integers find position of max value. if vector {1, 3, 19}, find_max_position returns integer, in case 1.

i need find_grad find "grade" of value entered. example, if call find_grad(table, 3) on vector {0, 1, 5, 4, 3}, function should return me 2, because 3 third bigger value (it starts 0). when call function loop becomes infinite. thought because when "table[(find_max_position(table))] = 0;" i'm acting on copy of vector , not vector itself, , when loop restarts bigger value it's there. maybe explained situation bit bad think it's easy understand reading code. can help?

edit: forgot > find_max_value returns integer, bigger value found on vector of integers.

problem comparing integer pointer. missed * before value.

correct code:

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

you can without using pointer. need remove * before value.

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

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -