尝试在 C 编程中对数组进行排序时获取未知值

发布于 2025-01-14 00:02:22 字数 1006 浏览 4 评论 0原文

我正在使用 VS 代码。我正在使用冒泡排序方法对 C 中的数组进行排序。它似乎排序正确,但我在最后得到的数字 4 不在数组中。这是代码和输出。

#include <stdio.h>

int bubble_sort(int nums[], int size);

int main() {
    int nums[] = { 101, 201, 111, 23, 41, 33, 1 };
    
    int size = sizeof(nums) / sizeof(int);
    
    int buble = bubble_sort(nums, size);
    
    printf("%d\n", buble);
}

//  defining bubble sort  
int bubble_sort(int nums[], int size) {

    for (int a = 0; a < size - 1; a++) {
        for (int b = 0; b < size - a - 1; b++) {
            //swapping
            if (nums[b] > nums[b + 1]) {
                nums[b] = nums[b] ^ nums[b + 1];
                nums[b + 1] = nums[b] ^ nums[b + 1];
                nums[b] = nums[b] ^ nums[b + 1];
            }
        }
    }
    for (int i = 0; i < 7; i++) {
        printf("%d\n", nums[i]);
    }
}

我得到的输出:

1
23
33
41
101
111
201
4

这是我得到的输出。在这里,请参阅最后一个数字。它不在数组中。我不知道这个号码是从哪里来的。我尝试过调试,但它来自某个地方。谁能告诉我从哪里以及为什么得到这个号码。

I am using VS code. I was sorting an array in c using bubble sort method. It seems to sort right, But I am getting number 4 at the end which is not in the array. here is the code and output.

#include <stdio.h>

int bubble_sort(int nums[], int size);

int main() {
    int nums[] = { 101, 201, 111, 23, 41, 33, 1 };
    
    int size = sizeof(nums) / sizeof(int);
    
    int buble = bubble_sort(nums, size);
    
    printf("%d\n", buble);
}

//  defining bubble sort  
int bubble_sort(int nums[], int size) {

    for (int a = 0; a < size - 1; a++) {
        for (int b = 0; b < size - a - 1; b++) {
            //swapping
            if (nums[b] > nums[b + 1]) {
                nums[b] = nums[b] ^ nums[b + 1];
                nums[b + 1] = nums[b] ^ nums[b + 1];
                nums[b] = nums[b] ^ nums[b + 1];
            }
        }
    }
    for (int i = 0; i < 7; i++) {
        printf("%d\n", nums[i]);
    }
}

output I am getting:

1
23
33
41
101
111
201
4

This is the output I am getting. Here, see the last number. It is not there in the array. I have no idea where this number is from. I tried debugging but it is coming from somewhere. Could anyone tell me from where and Why I am getting this number.

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

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

发布评论

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

评论(2

这样的小城市 2025-01-21 00:02:22

这条线没有意义。

printf("%d\n",buble);

您正在打印 bubble_sort 函数的返回值,但它没有 return 语句,因此您不知道它将返回什么。

您应该使用 -Wall 选项进行编译。如果这样做,您会收到有关您做错的事情的警告,包括以下警告:

bubble.c: In function ‘bubble_sort’:
bubble.c:33:3: warning: control reaches end of non-void function [-Wreturn-type]

它说“您有一个想要返回值的函数,但没有 return 语句。”

This line doesn't make sense.

printf("%d\n",buble);

You're printing the return value of the bubble_sort function, but it doesn't have a return statement, so you don't know what that is going to return.

You should be compiling with the -Wall option. If you do, you'll get warnings about things you're doing wrong, including this one:

bubble.c: In function ‘bubble_sort’:
bubble.c:33:3: warning: control reaches end of non-void function [-Wreturn-type]

It's saying "You have a function that wants to return a value, and you have no return statement."

本宫微胖 2025-01-21 00:02:22

您正在使用 buble_sort 函数内的循环在屏幕上打印输出。您不必将返回类型设置为 int 或将其返回值存储在新变量中,这将占用额外的内存空间。

由于您的函数没有返回值,并且您已将返回类型原型化为 int,因此它可能会打印 int 的大小(即 4 个字节)。

我建议您将以下行更改为:

int bubble_sort(int nums[], int size);

int buble = bubble_sort(nums,size);   

int bubble_sort(int nums[],int size){

void bubble_sort(int nums[], int size);    

bubble_sort(nums,size);

void bubble_sort(int nums[],int size){

并删除以下

printf("%d\n",buble);

You're using loop inside buble_sort function to print the output on the screen. You don't have to set return type to int or store its return value in a new variable which will take extra space in memory.

Since your function does not have a return value and you've prototyped return type as int, it is possibly printing the size of int (which is 4 byte).

I suggest you to change below lines :

int bubble_sort(int nums[], int size);

int buble = bubble_sort(nums,size);   

int bubble_sort(int nums[],int size){

to this

void bubble_sort(int nums[], int size);    

bubble_sort(nums,size);

void bubble_sort(int nums[],int size){

and delete below

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