递归排序函数

发布于 2024-10-17 03:05:10 字数 1045 浏览 1 评论 0原文

我编写了一个程序来对数组进行递归排序。 但是,我在第 11 行收到以下错误:“]”标记之前出现语法错误。

这是代码:

//This program recursively sorts an array

#include<stdio.h>

void rec_sort(int values[], int n); 

main()
{
    int vals[4];
    vals[0] = 37; vals[1] = 48; vals[2] = 56; vals[3] = 63;
    printf("this array sorted: %x\n", rec_sort(vals[], 4));

    system("Pause"); 
    return 0;
}

void rec_sort(int values[], int n) {
//Base case
if (n<2) return;

int maxIndex=0;
int i;

//Find max item in array in indexes 0 through n-1
for(i=1; i<n;i++) {
      if(values[i] > values[maxIndex])
         maxIndex=i;

    }

    //Swap this element with one stored in n-1
    //Set temp to n-1, set n-1 in array to max, set max to temp
    int temp = values[n-1]; //Store temp as last element in array
    values[n-1] = values[maxIndex]; //Store last element as max value in array
    values[maxIndex] = temp; //temp will keep on changing, as array is sorted

    //Recursively sort the array values of length n-1
    sort(values, n-1); 
}

I've written a program to recursively sort an array.
However, I get the following error on line 11: syntax error before ']' token.

Here is the code:

//This program recursively sorts an array

#include<stdio.h>

void rec_sort(int values[], int n); 

main()
{
    int vals[4];
    vals[0] = 37; vals[1] = 48; vals[2] = 56; vals[3] = 63;
    printf("this array sorted: %x\n", rec_sort(vals[], 4));

    system("Pause"); 
    return 0;
}

void rec_sort(int values[], int n) {
//Base case
if (n<2) return;

int maxIndex=0;
int i;

//Find max item in array in indexes 0 through n-1
for(i=1; i<n;i++) {
      if(values[i] > values[maxIndex])
         maxIndex=i;

    }

    //Swap this element with one stored in n-1
    //Set temp to n-1, set n-1 in array to max, set max to temp
    int temp = values[n-1]; //Store temp as last element in array
    values[n-1] = values[maxIndex]; //Store last element as max value in array
    values[maxIndex] = temp; //temp will keep on changing, as array is sorted

    //Recursively sort the array values of length n-1
    sort(values, n-1); 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暖伴 2024-10-24 03:05:10

看起来您正在尝试打印整个数组,而 C 不会在一次调用 printf 中执行此操作。相反,您需要一个循环来迭代数组并单独打印出每个数字:

for (i=0; i<4; i++)
    printf("%x\n", vals[i]);

由于 rec_sort 不返回数组,因此您还需要与 printf 调用分开调用它,因此您' d 得到类似的东西:

// sort the data:
rec_sort(vals, 4);

// print the sorted values:
for (i=0; i<4; i++)
    printf("%x\n", vals[i]);

It looks like you're trying print out the whole array, which C won't do in one call to printf. Instead, you need a loop to iterate through the array and print out each number individually:

for (i=0; i<4; i++)
    printf("%x\n", vals[i]);

Since rec_sort isn't returning the array, you also need to invoke it separately from the call the printf, so you'd get something like:

// sort the data:
rec_sort(vals, 4);

// print the sorted values:
for (i=0; i<4; i++)
    printf("%x\n", vals[i]);
寄与心 2024-10-24 03:05:10

只需删除第 11 行的 [] 即可。但这对您的问题来说是一个幼稚的答案,不会让您走得太远。还有其他问题 - 最明显的是 printf(..., rec_sort(...)...);

考虑 rec_sortvoid 返回类型,您如何期望 printf() 理解要做什么?我也不确定你想要什么,但这至少应该是你的一个开始。

Just remove the [] on line 11. But this is a naive answer to your question, and won't get you far. There are other problems - the most obvious is the idea of printf(..., rec_sort(...)...);

Considering rec_sort has void return type, how do you expect printf() to understand what to do? I am not sure what you want either, but this should be at least a start for you.

━╋う一瞬間旳綻放 2024-10-24 03:05:10
printf("this array sorted: %x\n", rec_sort(vals[], 4));

rec_sort()void。不返回任何东西

void rec_sort(int values[], int n)

另外,将你的main声明为int main()

printf("this array sorted: %x\n", rec_sort(vals[], 4));

but rec_sort() is void. Doesn't return anything

void rec_sort(int values[], int n)

Also, declare your main as int main()

失退 2024-10-24 03:05:10

问题在于:

rec_sort(vals[], 4)

你到底想在那里做什么? [] 是一个索引操作,因此您要么需要在其中放入一个数字,要么完全将它们省略(如果您想谈论整个数组)。

The problem is in this:

rec_sort(vals[], 4)

What exactly do you want to do there? The [] is an index operation, so either you need to put a number in there, or you leave them out completely (if you want to talk about the whole array).

海风掠过北极光 2024-10-24 03:05:10

您应该做的第一件事是删除方括号:

printf("this array sorted: %x\n", rec_sort(vals, 4));

返回值

第二,请注意,rec_sort返回void,因此您不能使用您需要的

int i; // at the top of the main
// ...
rec_sort(vals, 4);
printf("this array sorted: ");
for(i = 0; i < 4; ++i) printf("%x ", vals[i]);
printf("\n");

第三:您需要再次调用rec_sort

第四:您想对做什么系统声明?

The first thing you should do is remove the square brackets:

printf("this array sorted: %x\n", rec_sort(vals, 4));

Second, note that rec_sort returns void, so you cant use the return value

you need

int i; // at the top of the main
// ...
rec_sort(vals, 4);
printf("this array sorted: ");
for(i = 0; i < 4; ++i) printf("%x ", vals[i]);
printf("\n");

Third: you need to call rec_sort again

Fourth: what are you tryin to do with the system statement?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文