English 中文(简体)
2. 计算中的记忆性错误? [闭门]
原标题:Memory related errors in counting sort? [closed]
Closed. This question needs to be more focused. It is not currently accepting answers.

我一直在努力执行计票。

It requests the user for the number of times the program should run n. The number of elements which will be sorted sz and the element with the maximum value k. Though the program runs correctly with n = 1, i.e. running it once, in other cases I get an error.

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

void cntsrt(int a[], int b[], int k);

int sz;

int main(int argc, char **argv) {
    int n, k,*b, *a, i, j;

    scanf("%d", &n);

    while (n--) {
        scanf("%d", &sz);
        scanf("%d", &k);

        a = (int *) calloc(sz + 1, sizeof(int));

        for (i = 1; i <= sz; i++)
            scanf("%d", &a[i]);

        b = (int *) calloc(sz + 1, sizeof(int));

        cntsrt(a, b, k);
        printf("
");

        for (j = 1; j <= sz; j++)
            printf("	%d", b[j]);

        free((void*)a);
        free((void*)b);
    }

    printf("
");

    return 0;
}

void cntsrt(int a[], int b[], int k) {
    int  i, j,*c;

    c = (int*) malloc(k*(sizeof(int)));

    for (i = 0; i <= k; i++)
        c[i] = 0;

    for (j = 1; j <= sz; j++)
        c[a[j]] = c[a[j]] + 1;

    for (i = 1; i <= k; i++)
        c[i] = c[i] + c[i - 1];

    for (j = 8; j > 0; j--) {
        b[c[a[j]]] = a[j];
        c[a[j]] = c[a[j]] - 1;
    }
}
最佳回答
 a = (int *) calloc(sz + 1, sizeof(int));
for (i = 1; i <= sz; i++)
    scanf("%d", &a[i]);

射线指数应从0s

Int the function cntsrt -

 c = (int*) malloc(k*(sizeof(int)));
 for (i = 0; i <= k; i++)  // should be < k. There is no kth index
     c[i] = 0;

如果阵列长度为n,则其可检索索引为0n-1

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签