C-Programm bricht bei einem Variablentausch ab



  • Hi,

    habe folgendes Problem:
    Ich habe ein Programm geschrieben, dass mehrere Zahlen mit dem Bubblesort und dem Minsort sortiert. Der Bubblesort funktioniert dabei einbahnfrei, aber sobald man den Minsort startet, bricht das Programm, ohne Fehlermeldung, ab.(Press any key to continiue)
    Das hier ist der Quelltext:

    #include <stdio.h>
    #include "conio.h"
    #include "conioex.h"
    #include "conioex.c"
    
    int eingeben();
    int sortierenbu();
    void sortieren();
    int ausgeben();
    
    void main(void)
    {
    	int zahlen[50], zahlen1[50], anzahl, a;
    	char eingabe;
    	textcolor(GREEN);
    	do
    	{
    	clrscr();
    	printf("\n\n\n\n\n\t\t\tSoritermaschine\n\t\t\t______________________________\n\n\t\t\tMenu\n\n\t\t\tZahlen eingeben[1]\n\t\t\tZahlen sortieren(Bubblesort)[2]\n\t\t\tZahlen sortieren(Minsort)[3]\n\t\t\tZahlen ausgeben[4]\n\t\t\tEXIT[e]");
    	eingabe=getch();
    	switch(eingabe)
    	{
    	case '1':
    		anzahl=eingeben(zahlen);
    		for(a=0;a<50;a++)
    		zahlen1[a]=zahlen[a];
    		break;
    	case '2':
    		sortierenbu(anzahl,zahlen);
    		break;
    	case '3':
    		sortieren(anzahl,zahlen);
    		break;
    	case '4':
    		ausgeben(anzahl,zahlen,zahlen1);
    		break;
    	}
    	}while(eingabe!='e');
    }
    
    int eingeben(int zahlen[])
    {
    	int anzahl, schleife=1;
    	clrscr();
    	printf("Wie viele Zahlen wollen Sie eingeben?\n");
    	scanf("%i", &anzahl);
    	printf("\n\nGeben Sie die Zahlen ein\n\n");
    	do
    	{
    		printf("%i:", schleife);
    		scanf("%i", &zahlen[schleife-1]);
    		schleife++;
    	}while(schleife!=anzahl+1);
    	return anzahl;
    }
    
    int sortierenbu(int anzahl, int zahlen[])
    {
    	int a,b,c;
    	for(a=0;a<500;a++)
    	{
    		for(b=0;b<anzahl;b++)
    		{
    			if(zahlen[b]>zahlen[b+1])
    			{
    				c=zahlen[b];
    				zahlen[b]=zahlen[b+1];
    				zahlen[b+1]=c;
    			}
    		}
    	}
    	clrscr();
    	gotoxy(25,8);
    	printf("Zahlen werden sortiert!!!");
    	gotoxy(25,13);
    	for(a=0;a<25;a++)
    	{
    		textcolor(YELLOW);
    		printf("=");
    		Sleep(100);
    		textcolor(GREEN);
    	}
    	gotoxy(25,18);
    	printf("Zahlen sortiert");
    	Sleep(1000);
    	return 0;
    }
    
    void sortieren(int anzahl, int zahlen[])
    {
    	int l=0,b=1,c,d,f;
    	for(d=1;d<500;d++)
    	{
    		for(c=0;c<anzahl;c++)
    		{
    			if(zahlen[l]<zahlen[b])
    			{
    				b++;
    			}
    			else
    			{
    				l=b;
    				b++;
    			}
    
    		}
    		f=zahlen[d];
    		zahlen[d]=zahlen[l];
    		zahlen[l]=f;
    		l=d;
    		b=d+1;
    	}
    	clrscr();
    	gotoxy(25,8);
    	printf("Zahlen werden sortiert!!!");
    	gotoxy(25,13);
    	for(l=0;l<25;l++)
    	{
    		textcolor(YELLOW);
    		printf("=");
    		Sleep(100);
    		textcolor(GREEN);
    	}
    	gotoxy(25,18);
    	printf("Zahlen sortiert");
    	Sleep(1000);
    
    }
    
    int ausgeben(int anzahl, int zahlen[], int zahlen1[])
    {
    	int a;
    	clrscr();
    	gotoxy(25,1);
    	printf("Ihre Zahlen sortiert:\n\n");
    	printf("\t\tEingegebene Zahlen\t\tsortierte Zahlen\n");
    	for(a=0;a<anzahl;a++)
    	{
    		printf("%2i.Zahl:\t\t%i\t\t\t\t%i\n", a+1, zahlen1[a], zahlen[a+1]);
    	}
    	a=getch();
    	return 0;
    }
    

    Das Problem liegt in der Funktion sortieren bei den folgenden Zeilen(108-110):

    f=zahlen[d];
    zahlen[d]=zahlen[l];
    zahlen[l]=f;
    

    Kommentier ich diese aus, bricht das Programm nicht ab.
    Aber wieso bricht das Programm ab? Und was kann ich machen, damit es nicht mehr abbricht?
    Danke schonmal.



  • Und was hat das mit C# zu tun? => Falsches Forum!


Anmelden zum Antworten