有谁能告诉我为什么我的排序不';不行?(c代码)
我刚开始编程。 我只是写了这段代码,但它不起作用。 你能告诉我为什么吗 代码=有谁能告诉我为什么我的排序不';不行?(c代码),c,sorting,C,Sorting,我刚开始编程。 我只是写了这段代码,但它不起作用。 你能告诉我为什么吗 代码= int main(int argc, const char *argv[]) { int a, b, c, d, e, f, g, h, i, j; int s; printf("enter 10 numbers: "); scanf("%d%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &am
int main(int argc, const char *argv[])
{
int a, b, c, d, e, f, g, h, i, j;
int s;
printf("enter 10 numbers: ");
scanf("%d%d%d%d%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j);
int numbers[10] = {a, b, c, d, e, f, g, h, i, j};
//%d %d %d %d %d %d %d %d %d %d a, b, c, d, e, f, g, h, i, j
printf("before \n %d %d %d %d %d %d %d %d %d %d\n", a, b, c, d, e, f, g, h, i, j);
for (int k = 0; k == 10; k++) {
if (numbers[k] <= numbers[k + 1]) {
numbers[k] = s;
numbers[k] = numbers[k + 1];
numbers[k + 1] = s;
}
}
printf("after \n %d %d %d %d %d %d %d %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7], numbers[8], numbers[9]);
return 0;
}
int main(int argc,const char*argv[]
{
int a,b,c,d,e,f,g,h,i,j;
int-s;
printf(“输入10个数字:”);
扫描(“%d%d%d%d%d%d%d%d%d%d%d%d”、&a、b、c、d、e、f、g、h、i、j);
整数[10]={a,b,c,d,e,f,g,h,i,j};
//%d%d%d%d%d%d%d%d%d%d%d%d a、b、c、d、e、f、g、h、i、j
printf(“在\n%d%d%d%d%d%d%d%d%d%d%d\n”之前),a、b、c、d、e、f、g、h、i、j);
for(int k=0;k==10;k++){
如果(numbers[k]那么,首先,我很确定scanf函数的输入是错误的。第一个“%d”将咀嚼字符串中的所有数字,因此变量b、c、d、e、f、g、h、I和j将被取消初始化
另外,当“s”没有初始化为任何值时,您正在执行数字[k]=s;
和数字[k+1]=s;
。上述代码中有许多错误。首先,您的“for”条件是错误的(k==10)。其次,仅遍历数组一次是不够的。您需要遍历数组多次,直到不需要进行交换为止。(请参阅Bubblesort)您的for循环条件在第一个实例中变为falls,在第一个步骤中,k为0,并且它的值检查为10(如果(k==10)in(k=0;k==10;k++)
这将不会执行for循环逻辑,在第一种情况下,您将从for循环中出来。帮助您查看排序代码可能出现的问题
英文/伪代码:
Read 10 elements of the array
For each 11 elements ( 0,1,2,3,4,5,6,7,8,9,10 ) of the array
if the current element is less of equals to the next element ( including the 12th element when the element index is 11 )
then
Store the value in 's' in place of the current element ( what is in 's' might be up to the phase of the moon )
Store the value of the next element in the current element ( we will never know what was the the mystery value in 's', unless .... )
Store the value of 's' in the next element ( next comparison will be a big surprise )
Print the 10 elements of the transformed array
公平地说,这可能还不足以表明你的错误
让我们做一个卡片类比
- 洗牌52张牌
- 从牌组中选择10张牌(7 10 5 4…)
- 看你手上的第一张牌(例如一张7号的钻石)
- 看下一个(例如10个俱乐部)
- 如果第一个小于或等于第二个,则:
- 将第一张牌替换为牌组顶部的牌(例如:红心皇后)
- 扔掉第一张牌
- 用第二张卡替换新卡
- 把新卡放在第二位
- 看第二张牌(心后)
- 看第三张牌(黑桃5张)
…让我们跳到第十张牌
- 看第十张卡片(好的……)
- 看第11张牌(你扭曲维度以看到第55维度的另一张牌,它是玫瑰的XZ)
- 比较这些值(通过应用多维数学,你会发现Shrodinger的猫还活着并且正在踢)
- 太阳离地球越来越近,导致了一个奇怪的冰河期,这将持续一秒钟
- 那些名字无人知晓的人会给你带来第12张卡片
- 你被一只蛴螬吃掉了
除了尝试寻找不存在的值之外,有些人建议您查看提供一些易于理解的伪代码的值
祝你在学习C的过程中好运(这直接违反了提问规则))你说它不工作是什么意思?什么不工作?当你试图运行你的代码时,你会遇到什么错误?一旦你在帖子中说出来,你就可以从社区得到一些帮助。1.打开编译器警告。这至少会暴露你的一个问题。2.在调试器中运行你的代码,单步执行并检查变量值es来跟踪代码中发生的情况。这将至少暴露一个问题。首先,阅读