我有我的泡沫排序程序的麻烦..我已经完成除了我有问题,我的函数调用我的swap()
函数..然后使用作为参数输入...麻烦指数在参数..冒泡排序在c
我已经尝试了很多不同的方式,下面是我最近的尝试。
我需要将交换作为单独的功能。编辑----------------------------- 随着你的建议,尤其是哈里斯和BLUEPIXY我的代码似乎工作..
虽然它有一些错误,并说它不能开始(即时通讯假设这与更多的代码块,因为它一直给我的问题)
澄清其系统错误不是代码块错误 没有更多的错误代码
这里是我的代码:
#include <stdio.h>
#define MAX 9
//moved values up here so it would be declared before its used below
int values[] = {7, 3, 9, 4, 6, 1, 2, 8, 5};
//functions
void printValues(){
int i;
printf ("{");
for(i = 0;i < 9;i ++){//small for loop to iterate through each element in array
printf("%d ", values[i]);
}//end for loop
printf("}");
} //end printValues
void swap(int *x, int *y){
int* temp;
*temp = *x;
*x = *y;
*y = *temp;
} //end swap function
void sort(){
int i;
int j;
for (i=0;i<9;i++){ // starts our loop; loops 9 times, one for each int, increments for each loop.
for (j=0;j<9-i-1;j++){//starts at max length minus 1 minus i's current pass
if (values[j] > values[j + 1]){
swap(&values[j] , &values[j+1]);
}//end if statement
}//end secondary for loop
}//end main for loop
} //end sort function
//# list; 9 integers
int main(){
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return(0);
} // end main
的错误消息是很清楚的。你不明白什么? – juanchopanza
许多问题。 1.“swap”中的参数类型丢失。 2.没有声明'temp'类型。 3.你将整数传递给'swap'。这些都是通过引用传递的,所以在函数退出时,'swap'中所做的任何更改都会丢失。我的猜测是,你实际上想要传递一个指向值的指针,而在'swap'中你需要取消引用这些指针。 – kaylum
'void swap(int * x,int * y){int} temp = * x; * x = * y; * y = temp; }',然后在调用者端'swap(&values [j],&values [j + 1]);' – BLUEPIXY