递归排序函数
我编写了一个程序来对数组进行递归排序。 但是,我在第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来您正在尝试打印整个数组,而 C 不会在一次调用
printf
中执行此操作。相反,您需要一个循环来迭代数组并单独打印出每个数字:由于 rec_sort 不返回数组,因此您还需要与 printf 调用分开调用它,因此您' d 得到类似的东西:
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: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:只需删除第 11 行的
[]
即可。但这对您的问题来说是一个幼稚的答案,不会让您走得太远。还有其他问题 - 最明显的是printf(..., rec_sort(...)...);
考虑
rec_sort
有void
返回类型,您如何期望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 ofprintf(..., rec_sort(...)...);
Considering
rec_sort
hasvoid
return type, how do you expectprintf()
to understand what to do? I am not sure what you want either, but this should be at least a start for you.但
rec_sort()
是void
。不返回
任何东西另外,将你的main声明为
int main()
but
rec_sort()
isvoid
. Doesn'treturn
anythingAlso, declare your main as
int main()
问题在于:
你到底想在那里做什么?
[]
是一个索引操作,因此您要么需要在其中放入一个数字,要么完全将它们省略(如果您想谈论整个数组)。The problem is in this:
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).您应该做的第一件事是删除方括号:
返回值
第二,请注意,rec_sort返回void,因此您不能使用您需要的
第三:您需要再次调用rec_sort
第四:您想对
做什么系统声明?
The first thing you should do is remove the square brackets:
Second, note that rec_sort returns void, so you cant use the return value
you need
Third: you need to call rec_sort again
Fourth: what are you tryin to do with the
system
statement?