0

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

2
  • the variables mini and maxi are initialized so you are testing value with anything. So it's dangerous! Mar 20, 2014 at 14:20
  • Did you mean are not initialized?
    – Oerd
    Mar 20, 2014 at 14:24

3 Answers 3

1
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.

1
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

2
  • [i-1] instead of [i+1]? Mar 20, 2014 at 14:21
  • @user3336852 No, change the for loop to for(i=0;i<N-1;i++) after verifying that N>0 holds. Mar 20, 2014 at 14:24
0
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);
9
  • they are initialized on the top int line. Where i should initalize them? Mar 20, 2014 at 14:25
  • @user3336852 They are defined on the first line but not initialized to any value. int i; will not initialize i to any particular value. Mar 20, 2014 at 14:26
  • @user3336852 you can initialize min and max to eingabe[0]
    – LearningC
    Mar 20, 2014 at 14:28
  • like this:mini=eingabe[0]; maxi=eingabe[0]; for(i=0;i<N;i++) ? Mar 20, 2014 at 14:39
  • @user3336852 ya. with that initialization try executing program. it should work.
    – LearningC
    Mar 20, 2014 at 14:41

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.