gpt4 book ai didi

C程序帮助: Unexpected Output

转载 作者:行者123 更新时间:2023-11-30 17:43:27 27 4
gpt4 key购买 nike

计算机科学学校项目。我需要编写一个程序,其中用户声明数组的大小,然后以数字、非递减顺序填充数组,然后声明一个值 x。然后将 X 分配到适当的位置,以便整个数组按数字、非递减顺序排列。然后输出该数组。

代码构建正确,没有错误,但输出困惑。

#include <stdio.h>

int main (void) {

//Local Declarations
int size;
int ary[100];
int x;
int i;
int j;

//Statements
printf("Enter the size of the array: ");
scanf("%d", &size);

printf("\nEnter digits to fill the array, in numerical order: ");
for (i = 0; i < size; i++) {
scanf("%d", &ary[i]);
}
size++;

printf("\nInput x, the value to add to the array: ");
scanf("%d", &x);

while(i=0 <= x && x > ary[i]){
i++;
j = size - 1;
while(j >= i) {
ary[j++] = ary[j];
j--;
}
}
for(i = 0; i < size; i++) {
printf("%d,", ary[i]);
}

return 0;
} //main

示例输出:

Enter the size of the array: 5

Enter digits to fill the array, in numerical order: 1
2
3
4
5

Input x, the value to add to the array: 6
1,2,3,4,5,1630076519,
Process returned 0 (0x0) execution time : 8.585 s
Press any key to continue.

总是最后一个值弄乱了这个巨大的数字。我一生都无法弄清楚为什么。该项目将于美国东部时间午夜截止。

最佳答案

对于while循环,你可以试试这个吗,

i = 0;
while (i < x && x > ary[i]) {
i++;
j = size - 1;
while (j >= i) {
j++;
ary[j] = ary[j]; // Equivalent to ary[j++] = ary[j];, yet easier to read
j--;
}
}

关于C程序帮助: Unexpected Output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20232722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com