Count Sort in C - Error: Use of undeclared identifier

Count Sort in C - Error: Use of undeclared identifier

我正在尝试编写一个使用计数排序算法的函数,但是当我尝试编译时它说 output_arraycount_array 未声明,尽管我已经将它们声明为 int 类型数组。

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

int MAX_LAENGE = 1000;
int MAX_VALUE = 100;
int i, k, j;
void count_sort_calculate_counts(int input_array[], int len, int count_array[])
{
    for (i=0; i<=len;i++)
    {
        count_array[i] = 0;
    }
    for (j=1; j<=len;j++)
    {
        count_array[input_array[j]] = count_array[input_array[j]] + 1;
    }
}

void count_sort_write_output_array(int output_array[], int len, int count_array[])
{
    k=0;
    for (j=1;j<=len;j++)
    {
        for (i=1; i<=count_array[j]; i++)
        {
            output_array[k] = j;
            k = k + 1;
        }
    }
}



int main(int argc, char *argv[]) {

    if (argc < 2){
        printf("Aufruf: %s <Dateiname>\n", argv[0]);
        printf("Beispiel: %s zahlen.txt\n", argv[0]);
        exit(1);
    }

    char *filename = argv[1];

    int input_array[MAX_LAENGE];  
    int len = read_array_from_file(input_array, MAX_LAENGE, filename);

    printf("Unsortiertes Array:");
    print_array(input_array, len);
    int i, k, j;
    count_sort_calculate_counts(input_array, len, count_array);
    count_sort_write_output_array(output_array, len, count_array);

    printf("Sortiertes Array:");
    print_array(output_array, len);

    return 0;
}

编译器输出:

introprog_blatt01_aufgabe02_vorgabe.c:51:51: error: use of undeclared identifier 'count_array'; did you mean 'print_array'? count_sort_calculate_counts(input_array, len, count_array);

./input_blatt01.h:2:6: note: 'print_array' declared here void print_array(int array[], int len);

introprog_blatt01_aufgabe02_vorgabe.c:52:35: error: use of undeclared identifier 'output_array'; did you mean 'input_array'? count_sort_write_output_array(output_array, len, count_array); introprog_blatt01_aufgabe02_vorgabe.c:45:9: note: 'input_array' declared here int input_array[MAX_LAENGE];

introprog_blatt01_aufgabe02_vorgabe.c:52:54: error: use of undeclared identifier 'count_array'; did you mean 'print_array'? count_sort_write_output_array(output_array, len, count_array);

./input_blatt01.h:2:6: note: 'print_array' declared here void print_array(int array[], int len);

introprog_blatt01_aufgabe02_vorgabe.c:55:17: error: use of undeclared identifier 'output_array'; did you mean 'input_array'? print_array(output_array, len);

introprog_blatt01_aufgabe02_vorgabe.c:45:9: note: 'input_array' declared here int input_array[MAX_LAENGE];

我在 main 函数中没有看到 output_array 和 count_array 的声明,因此 "know" 没有关于它们的声明。阅读变量范围。编辑:我想它可以重新制作,因此您可以将指针传递给数组。

您将 output_arraycount_array 声明为以下函数的参数:

void count_sort_calculate_counts(int input_array[], int len, int count_array[])
void count_sort_write_output_array(int output_array[], int len, int count_array[])

但是你没有在 main 函数中声明 output_arraycount_array

您可能需要这个:

...
int input_array[MAX_LAENGE];  
int output_array[MAX_LAENGE];   // <<< add this
int count_array[MAX_LAENGE];    // <<< add this 
int len = read_array_from_file(input_array, MAX_LAENGE, filename);
...