简体   繁体   中英

C Pointers Array 2nd Max

I'm trying to find the 2nd max number from an array. For some reason, I keep getting output zero.

int main()
{
    int i,j;
    int arr[5],max=0,max2=0;
    int *(ptr)=arr;
    printf("Enter Array Elements: ");
    for(i=0;i<5;i++)
    {
        scanf("%d",ptr+i);
    }
        for(j=0;j<5;j++){
            if(max<*(ptr+j)&&max>max2)
            {
                max=*(ptr)+j;
            }
            else if(max2<*(ptr+j)&&max2<max)
            {
                max2=*(ptr)+j;
            }
        }
    printf("\n2nd Maximum: %d",max2);
}
    

both your conditions compare max to max2 . Since they are both initialized to 0 , neither of these conditions is ever met. Instead you need to compare the current value ( *(ptr+j) ) to the maximum number first, and if that condition isn't met, to the second maximum:

for (j = 0; j < 5; j++) {
    int curr = *(ptr+j);
    if (curr >= max)
    {
        max2 = max;
        max = curr;
    }
    else if(curr > max2)
    {
        max2 = curr;
    }
}

Initially max and max2 are equal each other due to setting them to 0 .

int arr[5],max=0,max2=0;

So neither of these two if statements evaluate their conditions to true

        if(max<*(ptr+j)&&max>max2)
                         ^^^^^^^^ 
        //...

        else if(max2<*(ptr+j)&&max2<max)
                               ^^^^^^^^

because max is not greater than max2 .

Moreover setting initially max and max2 is a wrong approach because the user can enter only negative values to elements of the array. In this case you will get wrong maximums.

If you want to find two maximum values that are not equal to each other except the case when the array contains all elements equal each other then the approach is not trivial as you think.

At first you need to find two unequal elements in the array. For example

int max  = arr[0];
int max2 = arr[0];

i = 1;

while ( i < 5 && arr[i-1] == arr[i] ) ++i;

if ( i != 5 )
{
    if ( max < arr[i] )
    {
        max = arr[i];
    }
    else
    {
        max2 = arr[i];
    }

    for ( ; i < 5; i++ )
    {
        if ( max < arr[i] )
        {
            max2 = max;
            max = arr[i];
        }
        else if ( max2 < arr[i] )
        {
            max2 = arr[i];
        }
    }
}   

if ( max != max2 )
{
    printf("\n2nd Maximum: %d\n", max2 );  
}
else
{
    printf("\nAll elements are equal to %d\n", max2 );
}

You are over complicating it. It probably would help to write it first without using pointers that way you can visualize what is going on then rewrite it.

int main()
{
    int arr[5],max=0,max2=0;
    int *(ptr) = arr;
    printf("Enter Array Elements: ");
    for (int i=0;i<5;i++){
        scanf("%d",ptr+i);
    }
    /*
    max = arr[0];
    for (int j=1;j<5;j++){
        if(arr[j]>=max){
            max2 = max;
            max = arr[j];
        }
    }*/
    max = *(ptr);
    for(int j = 1;j<5;j++){
        if(*(ptr+j)>=max){
            max2 = max;
            max = *(ptr+j);
        }
    }
    printf("\n2nd Maximum: %d",max2);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM