对于此作业,我必须在另一个文件中编写一个选择排序函数,该函数按升序对数组进行排序,以便在我的驱动程序中使用。我已经在这个问题中搜索了我的错误,但我仍然没有找到任何我见过的可以帮助我的东西。有人可以帮帮我吗?
这是我目前所拥有的。
#include <iostream>
using namespace std;
void *selectionsort(int values[], int size){
for(int i=0; i<size-1; i++){
for(int j=0; j<size; j++){
if(values[i] < values[j]){
int temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
}
如果需要的话,这是我的司机。
#include <stdlib.h>
#include <iostream>
#include "selection.cpp"
using namespace std;
#define ArraySize 10 //size of the array
#define Seed 1 //seed used to generate random number
int values[ArraySize];
int main(){
int i;
//seed random number generator
srand(Seed);
//Fill array with random intergers
for(i=0;i<ArraySize;i++)
values[i] = rand();
cout << "\n Array before sort" << endl;
for(i=0;i<ArraySize; i++)
cout << &values[]<< "\n";
//int* array_p = values;
cout << "\n Array after selection sort." << endl;
//Function call for selection sort in ascending order.
void *selectionsort(int values[], int size);
for (i=0;i<ArraySize; i++)
cout << &values[] << "\n";
//system("pause");
}
最佳答案
你的函数不是无效的,把它改成
void selectionsort(int values[], int size){
额外:如果您这样定义函数,它会返回一个空指针。 void 指针是可以指向……任何东西的指针。
当然,请参阅@brokenfoot 的回答以了解如何调用函数。
关于c++ - 错误 : Control reaches end of non-void function in C++ for selection sort function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369266/