【发布时间】:2014-04-17 14:31:44
【问题描述】:
如何在一个文件中正确定义 C++ 函数并在不使用头文件的情况下从另一个文件中调用它?我会使用头文件,但我的教授告诉我们不要。我的文件一直有很多编译问题,处理我的功能不存在。任何帮助表示赞赏。我的程序应该使用选择排序和降序使用冒泡排序对数组进行升序排序。
这是我目前所拥有的。这是我的司机。
驱动程序.cpp
#include "selection.cpp"
#include "bubble.cpp"
#define ArraySize 10 //size of the array
#define Seed 1 //seed used to generate random number
int values[ArraySize];
int main(int argc, char** argv) {
int i;
//seed random number generator
srand(Seed);
//Fill array with random integers
for(i=0;i<ArraySize;i++)
values[i] = rand();
cout << "\n Numbers in array." << endl;
for(i=0;i<ArraySize; i++)
cout << &values[i]<< "\n";
int* array_p[] = values[];
//Function call for BubbleSort
bubblesort(array_p[], ArraySize);
for (i=0;i<ArraySize; i++)
cout << &values[i] << "\n";
//SelectionSort
selectionsort(array_p, ArraySize);
cout << "Numbers in ascending order." << endl;
for (i=0;i<ArraySize; i++)
cout << &values[i] << "\n";
return 0;
}
气泡.cpp
#include <iostream>
int* bubblesort(int values[], int size) {
int i, j;
for(i=0;i<size-1;i++){
for(j=0; j<size-1; j++){
if(values[j+1] > values[j]){
int temp = values[j];
values[j] = values[j+1];
values[j+1] = temp;
return values;
}
}
}
};
selection.cpp
#include <iostream>
int *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;
return values;
}
}
}
};
【问题讨论】:
标签: c++ function sorting bubble-sort selection-sort