When the while loop iterates, it skips both “if” loops and when the “q=q->next” statement runs, both the max and min values are changed as well. Am I not initializing the max/min integers correctly?
void FindMaxMin(int& max, int& min)
{
NODE* q;
q=List; //to start over
while(q != NULL)
{
max=min=q->info; //Sets max and min to first value
if(q->info>max)
max=q->info;
if(q->info<min)
min=q->info;
q=q->next;
}
}
Max/min are initialized every loop cycle to the current element value. That’s why ifs are skipped (since the data is neither more nor less than the actual value – it’s the same).
You should write something like this to correctly initialize max/min data: