我目前正在開發一個項目,我們必須創建一個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