将数组传递给函数时,我无法使气泡排序在函数中起作用 [英] i cant get bubble sorting to work in function while passing array to the function
本文介绍了将数组传递给函数时,我无法使气泡排序在函数中起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
int size=10, numbers[size], i, order;
for (i=0; i<10; i++)
{
printf("please enter a number:");
scanf("%d", &numbers[i]);
}
ascending(numbers[], size);
}
void ascending(int numbers[], int size)
{
int temp, i, sflag, count=0;
do
{
sflag = 0;
for(i=1; i <10; i++)
{
if (numbers[i-1] > numbers[i])
{
temp = numbers[i-1];
numbers[i-1] = numbers[i];
unmbers[i] = temp;
sflag = 1;
}
}
count++;
}while(sflag);
for (i=0; i<10; i++)
{
printf("%d\t", numbers[i]);
}
}
该代码在函数的第一个if语句中失败,它指示分段错误. 我不确定为什么,我认为将数组传递给函数的方式可能有误.
the code fails at the the first if statement in the function, it says segmentation error. im not sure why, i think there may be an error in how i am passing the array to the function.
推荐答案
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
int size=10, numbers[size], i, order;
for (i=0; i<10; i++)
{
printf("please enter a number:");
scanf("%d", &numbers[i]);
}
ascending(numbers, size);
return 0;
}
void ascending(int numbers[], int size)
{
int temp, i, sflag, count=0;
do
{
sflag = 0;
for(i=1; i <10; i++)
{
if (numbers[i-1] > numbers[i])
{
temp = numbers[i-1];
numbers[i-1] = numbers[i];
numbers[i] = temp;
sflag = 1;
}
}
count++;
}while(sflag);
for (i=0; i<10; i++)
{
printf("%d\t", numbers[i]);
}
}
在 https://www.onlinegdb.com/online_c_compiler#
我无法检测到任何错误
我检查了3,7,8,8,9,10,11,200,317和1,1,1,1,1,1,1,1,1,
I checkt 3,7,8,8,9,10,11,200,317 and 1,1,1,1,1,1,1,1,1
这篇关于将数组传递给函数时,我无法使气泡排序在函数中起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文