Search code examples
carraysmaxminbubble-sort

Min and Max C Failure


I had written this code.Its very buggy. I wanna have the order how i typed it in. Then sorted with bubblesort.And then the min and max values.The program gives me always the same min and max values. Please help me. Thx ;)

#include<stdio.h>
#include<stdlib.h>
#define N 10
int main()
{
int eingabe[N],mini,maxi,i,temp,j;
float medium,ds;
{
    printf("Bitte 10 Werte eingeben!");
    for(i=0;i<N;i++)
    {
        scanf("%i",&eingabe[i]);
    }
    printf("Die eingegebenen Werte der Eingabereihenfolge nach:");
    for(i=0;i<N;i++)
    {
        printf("\n%i",eingabe[i]);
    }
    for(j=0;j<N-1;j++)
        for(i=0;i<N-1-j;i++)
        {
           if (eingabe[i]>eingabe[i+1])//Bubblesort

            {
                temp=eingabe[i];
                eingabe[i]=eingabe[i+1];//Tausch der Variablen
                eingabe[i+1]=temp;
            }



        }
    printf("Sortierte Werte(min to max):");
    for(i=0;i<N;i++)
    {
        printf("\n%i",eingabe[i]);
    }
    mini=eingabe[0];
    maxi=eingabe[0];
    for(i=0;i<N;i++)
    {
        if(maxi<eingabe[i])
        {
           maxi=eingabe[i];
        }
        if (eingabe[i]<mini)
        {
            mini=eingabe[i];
        }
    printf("\nDer Minimalwert ist: %i",mini);
    printf("\nDer Maximalwert ist: %i",maxi);
    for(i=0;i<N;i++)
    {
        medium=medium+eingabe[i];
    }
    ds=medium/2;
    printf("\nDer Durchschnitt betraegt: %f",ds);

    }



}

return 0; }


Solution

  • if(maxi<eingabe[i])
            {
               maxi=eingabe[i];
            }
            if (eingabe[i]<mini)
            {
                mini=eingabe[i];
            }
    

    here maxi and mini are not initialized so first comparison will not be fine. initialize those variables.
    you can initialize mini and maxi to eingabe[0] You code for bubble sort is not complete.

    for(i=0;i<N;i++)
            {
               if (eingabe[i]>eingabe[i+1])
    
                {
                    temp=eingabe[i];
                    eingabe[i]=eingabe[i+1];
                    eingabe[i+1]=temp;
                }
    
            }
    

    there should be 2 for loops. one for loop means only one run through the array. it will not sort the array. the loops should be,

    for(j=0;j<N-1;j++)
     for(i=0;i<N-1-j;i++)
     {
       //write the if statement
    
     }
    

    in this part of code i think you are getting the min and max values

    for(i=0;i<N;i++)
        {
            if(maxi<eingabe[i])
            {
               maxi=eingabe[i];
            }
            if (eingabe[i]<mini)
            {
                mini=eingabe[i];
            }
        printf("\nDer Minimalwert ist: %i",mini);
        printf("\nDer Maximalwert ist: %i",maxi);
    

    change it to

    mini=eingabe[0];
    maxi=eingabe[0];
    for(i=0;i<N;i++)
        {
            if(maxi<eingabe[i])
            {
               maxi=eingabe[i];
            }
            if (eingabe[i]<mini)
            {
                mini=eingabe[i];
            }
        }
        printf("\nDer Minimalwert ist: %i",mini);
        printf("\nDer Maximalwert ist: %i",maxi);