我目前正在开发一个项目,我们必须创建一个1000个元素的数组,然后将其传递给另一个函数进行排序。我在网上看到的所有东西都向你展示了如何将它从主要功能传递到另一个功能,但不是其他功能。努力将一个函数中创建的数组传递给排序函数
请看看我的代码,并帮助我从Array()
通过Ar[1000]
到ISort
最终主
#include <iostream>
#include <time.h>
using namespace std;
void Array()//function to make array
{
int Ar[1000];//creating array
int i = 0;//random variable to be element #
int counter = 0;// counter variable
int randnum;//variable to old random number
srand(time(NULL));//seeding rand with time
while (counter != 1000)
{
randnum = rand();
Ar[i] = randnum;
cout << Ar[i]<<endl;
counter++;
}
}
void ISort(int Ar[1000])//Iterative sort
{
int count = 0;//another counter variable
int count2 = 0;//counter variable # 3 because nested loops
int j=0;//Temp index # similar to i
int temp; //Temp variable to help switch elements
while (count != 1000)
{
if (Ar[count] < Ar[j])
{
temp = Ar[count];
Ar[count] = Ar[j];
Ar[j] = temp;
}
}
}
/*void RSort(int Ar)//Recursive sort
{
}
*/
int main()
{
Array();
ISort();
system("Pause");
return 0;
}
函数'Array'中声明的数组是局部的,当执行离开函数时将消失。将数组传递给函数或使其成为全局函数。 –
在'main'函数中,你没有将数组传递给'ISort'。你的编译器应该警告你这个。 –
...或可能Array()函数返回一个数组而不是'void'(不返回) – Ripi2