-1

I'd like to ask why my code doesn't work I've tired many more things to no avail for some peculiar reason the program can find the minimum value of the array but cannot find the minimum value of it and returns the value 0.000000. Hope the code below helps.

This is what I have on my main function.

int main(void){

    float array[5] = {12,32,56,67,54};

    float min;
    float max;

    float *ptrMin;
    float *ptrMax;

    ptrMin = &min;
    ptrMax = &max;

    findMinMax(array, ptrMin, ptrMax);

    printf("%f %f", *ptrMin, *ptrMax);

    system("pause");
    return 0;
}

I use the function findMinMax to return TWO values back to my main function.

void findMinMax(float *array, float *ptrMin, float *ptrMax){
    float ptrMinTemp;
    float ptrMaxTemp;

    *ptrMin = ptrMinTemp;
    *ptrMax = ptrMaxTemp;

    while(*array){
        if(*array < ptrMinTemp){
            ptrMinTemp = *array;
            *ptrMin = ptrMinTemp;
        }

        if(*array > ptrMaxTemp){
            ptrMaxTemp = *array;
            *ptrMax = ptrMaxTemp;
        }
        array++;
    }
}

Before adding this comment , yes I created the prototype function and also included the correct header files.

#include <stdio.h>
#include <stdlib.h>

void findMinMax(float *array, float *ptrMin, float *ptrMax);

In the console I get the following:

0.000000 67.000000Press any key to continue . . .
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • What **exactly** is the question here? I see that your output contains 67, which is the maximum value. Is your question **actually** why min is 0? – Lasse V. Karlsen Dec 30 '17 at 21:46
  • `float ptrMinTemp;` and `float ptrMaxTemp;` are *unintialised variables*. Please enable and fix compiler warnings. – Weather Vane Dec 30 '17 at 21:48
  • Do you get any warnings from the compiler when compiling this code? Now would be a good time to include these in your question (or actually, fix the warnings, see if you still *have* a question). – Lasse V. Karlsen Dec 30 '17 at 21:49
  • Yes , that is exactly my question , It should obviously print the value 12. –  Dec 30 '17 at 21:50
  • You check whether a value is lower than an unitialized value (0). Try your code when you add a negative value to your array and you will see it will work. Just check whether min is initialized. – Mitja Dec 30 '17 at 21:50
  • 1
    Both answers from Stargateur and Ronan will solve your issues (you got more than one issue in your code obviously). – Ely Dec 30 '17 at 21:54
  • @Elyasin Yes I managed to fix it , I was dumb enough to actually instead of declaring *ptrMax = *array; and *ptrMax = *array; I made them point to the array. –  Dec 30 '17 at 22:06

3 Answers3

6
while(*array)

is wrong by nature, this mean, "stop when *array is zero", you can't do that unless you put 0 at the end of your array to mark it as the end of your array.

So you must send the size of your array:

// ...
findMinMax(array, ptrMin, ptrMax, sizeof array / sizeof *array);

void findMinMax(float *array, float *ptrMin, float *ptrMax, size_t size)
// ...
for (size_t i = 0; i < size; i++) // use array[i] not *array in that loop

Plus you didn't initialize your min and max:

float min = array[0];
float max = array[0];
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • I always thought while(*p) meant to run as long as the value is not null , thanks for that :) –  Dec 30 '17 at 21:52
  • @ThomasSourtzis C is not a context free language, `while (*p)` could work as expected in a lot of case but not this one. – Stargateur Dec 30 '17 at 21:53
  • Should I also increment the array pointer at the end of the for(size_t..); loop ? –  Dec 31 '17 at 01:13
  • @ThomasSourtzis With my loop you must not change the value of array. `i` will do you job for you as `array[i]`. – Stargateur Dec 31 '17 at 01:28
4

The array does not have a sentinel value equal to 0.0f. Thus this loop

while(*array){

does not make sense.

Moreover the variables

float ptrMinTemp;
float ptrMaxTemp;

are not initialized explicitly and have indeterminate values. As result these statements

*ptrMin = ptrMinTemp;
*ptrMax = ptrMaxTemp;

lead to undefined behavior.

Take into account that the array in the function findMinMax is not changed. So the first parameter should be declared with the qualifier const.

In general as the pointer can point to an empty array so the function should have some way to signal this particular case.

The function findMinMax can be defined the following way

int findMinMax( const float *array, size_t n, float *ptrMin, float *ptrMax )
{
    int success = n != 0;

    if ( success )
    {
        *ptrMin = *array;
        *ptrMax = *array;

        for ( ; n--; ++array )
        {
            if ( *array < *ptrMin )
            {
                *ptrMin = *array;
            }
            else if ( *ptrMax < *array )
            {
                *ptrMax = *array;
            }                
        }

    }

    return success;
}

and called like

findMinMax( array, sizeof( array ) / sizeof( *array ), ptrMin, ptrMax );

Here is a demonstrative program

#include <stdio.h>

int findMinMax( const float *array, size_t n, float *ptrMin, float *ptrMax )
{
    int success = n != 0;

    if ( success )
    {
        *ptrMin = *array;
        *ptrMax = *array;

        for ( ; n--; ++array )
        {
            if ( *array < *ptrMin )
            {
                *ptrMin = *array;
            }
            else if ( *ptrMax < *array )
            {
                *ptrMax = *array;
            }                
        }

    }

    return success;
}

int main(void) 
{
    float array[5] = { 12, 32, 56, 67, 54 };

    float min;
    float max;

    float *ptrMin;
    float *ptrMax;

    ptrMin = &min;
    ptrMax = &max;

    findMinMax(array, sizeof( array ) / sizeof( *array ), ptrMin, ptrMax);

    printf("%f %f", *ptrMin, *ptrMax);

    return 0;
}

Its output is

12.000000 67.000000
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you that is very interesting as well ! I will give it a run to see how it works :) Mind you is the *ptrMax *array actually corrrect or you are missing a conditional operator ? –  Dec 30 '17 at 22:08
  • @ThomasSourtzis See my updated post. The original code had typos. – Vlad from Moscow Dec 30 '17 at 22:25
1

Your problem

Your are setting your variables ptrMin and ptrMax to the content of uninitialized variables ptrMinTemp and ptrMaxTemp:

float ptrMinTemp;
float ptrMaxTemp;

*ptrMin = ptrMinTemp;
*ptrMax = ptrMaxTemp;

Solution

Just set your pointers' content to the first cell of your array, like this:

*ptrMin = *array;
*ptrMax = *array;
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56