myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   C programm Array (https://mygully.com/showthread.php?t=1952083)

moKy 07.12.09 17:38

C programm Array
 
hallo, ich soll ein c-programm schreiben, das eine unbekannte anzahl von integer zahlen ca 10 einliest. zuerst unsortiert folge anschliessend die folge nach jedem vergleichsdurchgang bis zur sortierten folge.

ich weiss garnicht wie anfangen soll.... kann mir da einer weiter helfen.

danke

urga 07.12.09 18:48

hier mal ne kaum dokumentierte lösung. die hilft nicht wirklich, aber gibt dir vielleicht eine idee...
Code:

# include <stdio.h>
# include <stdlib.h>

# define MAX_INPUT_SIZE 100
void array_ausgabe (int* a, int size) {
        int i;
        for (i = 0; i < size; i++) {
                printf ("array[%d] == %d\n", i, a[i]);
        }
} // array_ausgabe()

// oh scheisse _const void *_ was mag das wohl sein ;)
// aber du sollst ja wahrscheinlich eh deine eigene sortierfunktion schreiben...
int mycomp (const void *a, const void *b) {
        return (*(int*) a) - (*(int*) b);
} // comp

int main(int ac, char **av)
{
        int array[MAX_INPUT_SIZE];
        int array_index = 0;
        printf ("gib zahlen ein, ende bei -1\n");
        int input = 0;
        while (array_index < MAX_INPUT_SIZE && input != -1) {
                printf ("> ");
                scanf ("%d", &input);
                if (input != -1) {
                  array[array_index++] = input;
                }
        }
        if (array_index == MAX_INPUT_SIZE) {
                printf ("mehr als %d zahlen darfst du nicht eingeben!\n", MAX_INPUT_SIZE);
        }
        printf ("array unsortiert:\n");
        array_ausgabe(array, array_index);

        printf ("array sortiert:\n");
        // hier statt qsort _deine_ sortierfunktion.
        // google mal nach bubblesort ...
        qsort (array, array_index, sizeof (int), mycomp);
        array_ausgabe(array, array_index);
        return 0;
}



Alle Zeitangaben in WEZ +1. Es ist jetzt 05:18 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.