1

i have a program in c that finds the first and second max in an array, but i want to get the indexes of these elements. Here is my code:

#include <stdio.h>
int main(){
    int max1,max2,n;
    scanf("%d",&n);
    int a[n],i;
    int i_m1,i_m2;
    i_m1 = i_m2=0;
    for(i = 0;i < n;i++){
        scanf("%d",&a[i]);
    }
    max1 = max2 = 0;
    for(i = 0;i < n;i++){
        if(a[i]>max1){
            max1=a[i];
            //i_m1++;
        } else if(a[i]>max2 && a[i]<max1){
            max2=a[i];
            //i_m2++;
        }
    }
    printf("%d %d\n",max1,max2);
    //printf("%d %d\n",i_m1,i_m2);
    for(i = 0;i < n;i++){
        printf("%d ",a[i]);
    }
    return 0;
}

I first enter the number of elements in the array then the array. For example if i have an array

number of elements: 5
elements in array: 
3 4 2 5 1
indexes:
0 1 2 3 4
max1=5, max2=4
i_m1=3, i_m2=1

How can i get the indexes i_m1 and i_m2 ?

1
  • 1
    You don't want to do i_m1++;, you want to do i_m1 = i;
    – codyne
    Commented Dec 7, 2021 at 21:40

3 Answers 3

2

For starters even this if-else statement

for(i = 0;i < n;i++){
    if(a[i]>max1){
        max1=a[i];
        //i_m1++;
    } else if(a[i]>max2 && a[i]<max1){
        max2=a[i];
        //i_m2++;
    }
}

is wrong.

If the condition of the if statement

    if(a[i]>max1){

is true then you need also assign max1 to max2 if max1 is not equal to max2 before changing max1.

The approach can look the following way

if ( n < 2 )
{
    i_m1 = 0;
    i_m2 = 0;
}
else
{
    if ( a[0] < a[1] )
    {
        i_m1 = 1;
        i_m2 = 0;
    }
    else
    {
        i_m1 = 0;
        i_m2 = 1;
    }

    for ( i = 2; i < n; i++ )
    {
        if ( a[i_m1] < a[i] )
        {
            if ( a[i_m2] < a[i_m1] ) i_m2 = i_m1;
            i_m1= i;
        }
        else if ( a[i_m2] < a[i] )
        {
            i_m2 = i;
        }
    }
}

printf( "max1 = %d max2 = %d\n", a[i_m1], a[i_m2] );
printf( "i_m1 = %d i_m2 = %d\n", i_m1, i_m2 );        

Here is a demonstration program.

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

int main( void )
{
    enum { N = 10 };
    int a[N];
    int n = N;
    
    srand( ( unsigned int )time( NULL ) );
    
    for ( int i = 0; i < n; i++ )
    {
        a[i] = rand() % N;
    }

    for ( int i = 0; i < n; i++ )
    {
        printf( "%d ", a[i] );
    }
    
    putchar( '\n' );
    
    //  Below the algorithm for a variable length array
    
    int i_m1 = 0, i_m2 = 0;

    if ( !( n < 2 ) )
    {
        if ( a[0] < a[1] )
        {
            i_m1 = 1;
            i_m2 = 0;
        }
        else
        {
            i_m1 = 0;
            i_m2 = 1;
        }

        for ( int i = 2; i < n; i++ )
        {
            if ( a[i_m1] < a[i] )
            {
                if ( a[i_m2] < a[i_m1] ) i_m2 = i_m1;
                i_m1= i;
            }
            else if ( a[i_m2] < a[i] )
            {
                i_m2 = i;
            }
        }
    }

    printf( "max1 = %d max2 = %d\n", a[i_m1], a[i_m2] );
    printf( "i_m1 = %d i_m2 = %d\n", i_m1, i_m2 );      
}

Its output might .look like

0 3 5 0 3 8 8 2 5 6 
max1 = 8 max2 = 8
i_m1 = 5 i_m2 = 6
2

You're thinking too hard about what to do with i as evidenced by the commented out i_m1++ and i_m2++.

If max1=a[i]; then it follows that i_m1 = i; and same for max2/i_m2.

0
1

Also logic error.

Once a new max1 found, the old value should become the new max2.

    if(a[i]>max1){
        max2 = max1; // add
        max1 = a[i];
    } else if(a[i]>max2 && a[i]<max1){
        max2=a[i];
    }
1
  • 1
    yes i know but for my program it only need to find the smaller max2 than max1
    – itmemilan
    Commented Dec 7, 2021 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.