質問

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; }

役に立ちましたか?

解決 3

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);

他のヒント

eingabe[i]=eingabe[i+1];
                    ↑

eingable is of size N. And since arrays in C are zero-based, the indexes run from 0 to N - 1.
In the last iteration, your program tries to get the value n eingabe[N], which is not available for it, so it crashes.

Also note that mini and maxi are not initialized and contain a garbage value, your program has undefined behavior.

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

In this fragment, you are accessing out of bounds of the array. N = 10, that is the array size, when i becomes 9 i + 1 is 10, but accessing 10th index is undefined behaviour

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top