Home > Back-end >  I have a problem in the position of the maximum and minimum, check the program below please
I have a problem in the position of the maximum and minimum, check the program below please

Time:01-12

#include<stdio.h>
#include<math.h>

            int main () {
            int MIN,MAX,j,k;
            int e,i,T[100],f=0;

\ Here to enter the dimension of my array

            printf("Entrez la dimension de votre tableau ne depasser pas 100 : \n");
             scanf("%d",&e);

\ here to let the user complete, the array

            if (e>100 || e<=0)
             { printf("Veuillez entrez une dimension valide");
                return 0; }

                 for(i=0;i<e;i  ) {
                   printf("T[%d]=",i);
                     scanf("%d",&T[i]);
                     }

\ here to show the values stored in my array

                  printf("  Les elements du tableau sont:");
                   for(i=0;i<e;i  ) {
                     printf(" %d  ",T[i]);
                       }

                    MAX=T[0];
                    MIN=T[0];

The problem is concerning the position, of my maximum, it shows the dimension of the array chosen by the user, since it's the same variable used I thought of storing the variable in another one but doesn't work You can observe above, there are some variables not used, I thought of using them to solve the problem, but doesn't seem like it worked

                     for(i=0;i<e;i  ) {
                      if (T[i]>MAX) {
                         MAX=T[i];
                                   }
                                         }
                       printf("\n  Le maximum est: %d et occupe la 
                              position: %d \n",MAX,i);

\ the second loop to find the minimum in my array \ my problem is in the position of my maximum or minimum since it's the same

           for(i=0;i<e;i  ) {
                 if (T[i]<MIN) {
                          MIN=T[i];
                                    } }
              printf("\n  Le minimum est: %d et occupe la position: %d \n",MIN,i);

                      return 0;
                                 }

CodePudding user response:

Your problem is that you don't save the index that resulted in the max value(same for min). Therefore i will always be equal to e at the end of the loop.

int MaxPos = 0
for(i=0;i<e;i  ) {
    if (T[i]>MAX) {
        MAX=T[i];
        MaxPos = i
    }
}
//Note that if we got here, then i = e
printf("\n  Le maximum est: %d et occupe la position: %d \n",MAX,MaxPos);

CodePudding user response:

After the both for loops the variable i is equal to the value of the variable e due to the form of the loops

for(i=0;i<e;i  )

Pay attention to that it is enough to have one for loop to find the minimum and the maximum. And it is enough to track only positions of the maximum and the minimum because knowing a position you can always get the value stored in an array at this position.

For example

MAX = 0;
MIN = 0;

for ( i = 1; i < e; i   )
{
    if ( T[MAX] < T[i] )
    {
        MAX = i;
    }
    else if ( T[i] < T[MIN] )
    {
        MIN = i;
    }
}

printf("\n  Le maximum est: %d et occupe la position: %d \n", T[MAX], MAX);
printf("\n  Le minimum est: %d et occupe la position: %d \n", T[MIN], MIN);

CodePudding user response:

You have bad choice also of initial values, apart from not keeping the position:

MAX=T[0];
MIN=T[0];

When you search for maximum you initial value should be the minimum value possible MAX=INT_MIN;. The real value is -2147483647 - 1. In same way your initial value for minimum should be MIN = INT_MAX, the real value is 2147483647. When your values are properly initialized, then you will have correct results.

  •  Tags:  
  • Related