חזור לתיקייה: מועד א

examA_q3

חיפוש בתיקיה
Ctrl K
לחץ כאן או הקש Ctrl K כדי לחפש בכל התיקיה

    examA_q3

    02340114

    מבוא למדמח מ

    רשימות הרצאה לקריאה בשבת

    examA_q3

    הופק מתוך תקיית התואר של שקד

    uni-folder.vercel.app

    exama-q3.c

    הערה (קובץ קוד)

    קובץ זה הוא קובץ קוד מסוג C.

    #include <stdio.h>
    #include <stdlib.h>
    
    #define DONT_KNOW "I_dont_know"
    
    void printIDontKnow()
    {
        printf("%s", DONT_KNOW);
        exit(0);
    }
    
    int examA_q3(int a[], int n, int m, int b[]);
    
    
    int main(void)
    {
        // uncomment next line if you don't know the answer
        // printIDontKnow();
    
        int n;
        if (scanf("%d", &n) != 1)
            return 1;
        int *a = malloc(sizeof(int) * n);
        if (!a)
            return 1;
        for (int i = 0; i < n; i++)
        {
            if (scanf("%d", a + i) != 1)
            {
                free(a);
                return 1;
            }
        }
        int m;
        if (scanf("%d", &m) != 1)
        {
            free(a);
            return 1;
        }
        int *b = malloc(sizeof(int) * n);
        if (!b)
        {
            free(a);
            return 1;
        }
        int res = examA_q3(a, n, m, b);
        for (int i = 0; i < res; i++)
        {
            printf("%d ", b[i]);
        }
        free(a);
        free(b);
        return 0;
    }
    
    void fillHistogramByNumbers(int a[], int aLength, int hist[], int histLength)
    {
        // zeroizing the histogram
        for (int i = 0; i < histLength; i++)
        {
            hist[i] = 0;
        }
    
        // filling in the values
        for (int i = 0; i < aLength; i++)
        {
            hist[a[i] - 1]++;
        }
    }
    
    void fillHistogramByCount(int histByNumbers[], int oldLength,
        int hist[], int newLength)
    {
        // zeroizing the histogram
        for (int i = 0; i < newLength; i++)
        {
            hist[i] = 0;
        }
    
        // putting in the values
        for (int i = 0; i < oldLength; i++)
        {
            if (histByNumbers[i] > 0) // (i + 1) appears at least one time in a[]
            {
                hist[(histByNumbers[i] - 1)] = (i + 1);
            }
        }
    }
    
    int fillB(int hist[], int length, int b[])
    {
        int count = 0;
        for (int i = 0; i < length; i++)
        {
            if (hist[i] > 0)
            {
                b[count] = hist[i];
                count++;
            }
        }
    
        return count;
    }
    
    int examA_q3(int a[], int n, int m, int b[])
    {
        int *histogramByNumbers = (int *) malloc(m * sizeof(int));
        if (histogramByNumbers == NULL)
        {
            return -1;
        }
        int *histogramByCount = (int *) malloc(n * sizeof(int));
        if (histogramByCount == NULL)
        {
            free(histogramByNumbers);
            return -1;
        }
    
        // filling the histograms
        fillHistogramByNumbers(a, n, histogramByNumbers, m);
        fillHistogramByCount(histogramByNumbers, m,
            histogramByCount, n);
    
        int num = fillB(histogramByCount, n, b);
    
        free(histogramByNumbers);
        free(histogramByCount);
        return num;
    }