我的程序应该遍历一个数组多次,因为只有几个小时只使用一个指针。
for (int i = 0; i < totalHours; i++)
{
cout << " " << i + 1;
while (ptrSvr <= last)
{
rand_set(ptrSvr);
print(ptrSvr);
ptrSvr++;
}
cout << endl;
}
return 0;
这是rand_set的功能
void rand_set(int* &ps)
{
*ps = rand() % 10;
}
这是印刷的功能。
void print(int* ps)
{
cout << " " << *ps << " ";
}
一旦这个迭代,我就不会理解如何将指针设置回数组中的第一个地址,这样当我增加时,它将从头开始打印一个新行。目前,它只会打印一行。还有一个问题 - 我只能使用给定的指针来访问数组。我不能使用数组索引。
此外,随机生成的最高数字必须存储在highestAttempts指针中,并且在打印时,必须在其旁边有*。此功能必须包含在rand_set函数中。我只是在它们旁边打印了一个*,或者它打印在第一个随机数上,如果它们增加则打印在每个随机数上。
提前感谢您的任何建议。我是一个200级的c ++类,所以这应该尽可能简单。
变量被声明和初始化,如此。
void initialize(int ts, int* &ps, int* &pt,
int* &l, int* &ha)
{
for (int i = 0; i < ts; i++)
{
ps[i] = 0;
pt[i] = 0;
l = &ps[i];
}
ha = NULL;
srand(time(NULL));
}
...
// Pointer declarations
int *ptrSvr; // Po
int *ptrTotal; // Po
int *last; // Po
int *highestAttempts; // Po
// Variable declarations
int totalServers; // Va
int totalHours; // Va
int total; // Va
// Prompt user for number of serv
// totalServers and totalHours
cout << "Enter the number of web
cin >> totalServers;
cout << "Enter the number of hour
cin >> totalHours;
// Declares arrays for ptrSvr and
// of total servers
ptrSvr = new int[totalServers];
ptrTotal = new int[totalServers];
答案 0 :(得分:-1)
如果您有类似
的内容int arr [] = {};
然后
ptrSvr = arr;
while
循环后重置。否则你必须存储ptrSvr
的第一个值,并在while
循环退出后重置它