berikut ini source code untuk mencari nilai modus (nilai data yang paling sering muncul) dari n buah data, silahkan dicermati : 
/**-----------------------------.
Program Modus dengan bahasa C   |
Diprogram oleh : pemrogram      |
------------------------------**/

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

#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n1. Line : %d\n2. File : %s\n",__LINE__,__FILE__);
#else
    #define TRACE_LINE
#endif
#define MAX 20
#define DELAY 10000000
#define INPUT 'i'
#define OUTPUT 'o'
#define TRUE 1
#define FALSE 0

typedef struct {
    int number, sigma;
} Data;

typedef struct {
    int count;
    Data* data;
} List;

int InList(List, int),
    Modus(int*, int),
        ChoosePivot(int, int);
            void UpdateList(List*, int),
                AppendtoList(List*, int),
                    InputOutput(int*, int, const char),
                QuickSort(int*, int, int),
            // SelectionSort(int*, int),
        Swap(int*, int*),
    Delay(void),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer, n;
    printf("Masukkan banyak data : ");
    scanf("%d",&n);
    fflush(stdin);
    if((n > FALSE) && (n <= MAX)) {
        buffer = (int*)calloc(n,sizeof(int));
        InputOutput(buffer,n,INPUT);
        printf("\n1. Data yang anda masukkan : ");
        InputOutput(buffer,n,OUTPUT);
        QuickSort(buffer,FALSE,(n-TRUE)); // FALSE = 0;
        // SelectionSort(buffer,n);
        printf("\n2. Data setelah disorting : ");
        InputOutput(buffer,n,OUTPUT);
        printf("\n3. Modus : %d\n",Modus(buffer,n));
    }
    FreeBuffer(buffer);
    getch();
    return(EXIT_SUCCESS);
}



void InputOutput(int* buffer, int n, const char STAT) {
    int i;
    if('i' == STAT) {
        for(i = 0; i < n; ++i) {
            printf("%d. Masukkan data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if('o' == STAT) {
        for(i = 0; i < n; ++i) {
            printf("%d ",buffer[i]);
            Delay();
        }
    }
}

int InList(List list, int number) {
    int i;
    for(i = 0; i < list.count; ++i) {
        if(number == list.data[i].number) {
            return(i);
        }
    }
    return(FALSE);
}

void UpdateList(List* list, int number) {
    int i = InList(*list,number);
    list->data[i].sigma++;
}

void AppendtoList(List* list, int number) {
    list->data[list->count].number = number;
    list->data[list->count].sigma = TRUE;
    list->count++;
}

int Modus(int* buffer, int n) {
    int i, j, max, result;
    List list;
    list.data = (Data*)malloc(n*sizeof(Data));
    list.count = FALSE;
    for(i = 0; i < n; ++i) {
        if(!InList(list,buffer[i])) {
            AppendtoList(&list,buffer[i]);
            for(j = i+TRUE; j < n; ++j) {
                if(buffer[i] == buffer[j]) {
                    UpdateList(&list,buffer[i]);
                }
            }
        }
    }
    max = list.data[0].sigma;
    result = list.data[0].number;
    for(i = 1; i < list.count; ++i) {
        if(list.data[i].sigma > max) {
            max = list.data[i].sigma;
            result = list.data[i].number;
        }
    }
    if(result == list.data[0].number) {
        result = FALSE;
    }
    return(result);
}

int ChoosePivot(int top, int bottom) {
    return((top+bottom)/2);
}

void QuickSort(int* buffer, int bottom, int top) {
    int key, i, j, k;
    if(bottom < top) {
        k = ChoosePivot(bottom,top);
        Swap(&buffer[bottom],&buffer[k]);
        key = buffer[bottom];
        i = bottom+TRUE;
        j = top;
        while(i <= j) {
            while((i <= top) && (buffer[i] <= key)) {
                ++i;
            }
            while((j >= bottom) && (buffer[j] > key)) {
                --j;
            }
            if(i < j) {
                Swap(&buffer[i],&buffer[j]);
            }
        }
        Swap(&buffer[bottom],&buffer[j]);
        QuickSort(buffer,bottom,(j-TRUE));
        QuickSort(buffer,(j+TRUE),top);
    }
}

/* void SelectionSort(int* buffer, int n) {
    int i, j, min;
    for(i = 0; i < n-TRUE; ++i) {
        min = i;
        for(j = i+TRUE; j < n; ++j) {
            if(buffer[min] > buffer[j]) {
                min = j;
            }
        }
        Swap(&buffer[min],&buffer[i]);
    }
} */

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void Delay(void) {
    int i = FALSE;
    while(i < DELAY) {
        ++i;
    }
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}


source code from : EcHodotordotid
edit post

Comments

3 Response to 'Array Himpunan'

  1. mikrotik89
    http://hendricool.blogspot.com/2010/12/array-himpunan.html?showComment=1293681914189#c2852923024482035340'> 30 Desember 2010 pukul 11.05

    wah manteb iki...

    bahasa C++ yo...dhewo arek iki...

     

  2. mikrotik89
    http://hendricool.blogspot.com/2010/12/array-himpunan.html?showComment=1296981999058#c2029417751530507174'> 6 Februari 2011 pukul 15.46

    Ini jeh refrensi untuk membuat aplikasi dengan konsep Three teir: http://www.leptopku.com/aplikasi-three-tier-vb-net-with-sql-server.html

    Semoga bermanfaat

     

  3. mikrotik89
    http://hendricool.blogspot.com/2010/12/array-himpunan.html?showComment=1301624123704#c4451769063624748317'> 1 April 2011 pukul 09.15

    byuh...byuh...

    opo iku...??

     

My Pictures