【发布时间】:2017-10-19 09:45:53
【问题描述】:
我通过以下方式使用随机数创建5 x 5 的array。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main () {
int a[5][5];
int randomNumber;
srand (time(NULL));
// output each array element's value
for ( int i = 0; i < 5; i++ ) {
cout << endl;
for ( int j = 0; j < 5; j++ ) {
a[i][j] = randomNumber;
randomNumber = rand() %100 + 1;
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
}
但是当我将循环包装到一个函数中然后调用它时。然后它没有显示任何结果。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main () {
// output each array element's value
cout <<"Array Results are.....";
int PopulateArray();
return 0;
}
int PopulateArray(){
int a[5][5];
int randomNumber;
srand (time(NULL));
for ( int i = 0; i < 5; i++ ) {
cout << endl;
for ( int j = 0; j < 5; j++ ) {
a[i][j] = randomNumber;
randomNumber = rand() %100 + 1;
cout << a[i][j] << " ";
}
cout << endl;
}
}
现在结果是这样的
问题
当我将loop 包裹在function 中时,为什么没有显示结果?
【问题讨论】:
标签: c++ function loops functional-programming