The task
这个任务
Design a program that generates a 7-digit lottery number. The program should have an INTEGER array with 7 elements. Write a loop that steps through the array, randomly generating a number in the range of 0 to 9 for each element.Then write another loop that displays the contents of the array.
设计一个程序,生成一个7位数的彩票号码。程序应该有一个包含7个元素的整数数组。编写一个遍历数组的循环,在每个元素的0到9范围内随机生成一个数字。然后编写另一个显示数组内容的循环。
This is my code
这是我的代码
#include <iostream>
#include <cstdlib>
#include <array>
using namespace std;
// Global Constants
const int ARRAY_SIZE = 7;
// Declare Functions
int genNumber();
void displayResults(int lottoArray[], int ARRAY_SIZE);
// Module -- Main
int main(){
// Declare Variables
int LottoArray;
// Set Functions
LottoArray = genNumber();
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
system("pause");
return 0;
}
// Module -- Generate Numbers
int genNumber(){
// Declare Variables for Array
int lottoArray[ARRAY_SIZE];
int i = 0;
// Generate a Number for Each Array Placeholder
for (i = 0; i < ARRAY_SIZE; i++){
// Generate Random Number in Array
lottoArray[i] = rand() % 10;
int checkNumber = lottoArray[i];
// Check for multiples
for (int i : lottoArray){
while (i == checkNumber){
checkNumber = rand() % 10;
}
}
}
return lottoArray[ARRAY_SIZE];
}
// Module -- Display Results
void displayResults(int lottoArray[], int ARRAY_SIZE){
// Declare Variables
int i = 0;
cout << "Lotto Numbers: ";
// Display Each Value in the Array
for (i = 0; i < ARRAY_SIZE; i++){
cout << lottoArray[i] << " ";
}
cout << endl;
}
I get the error on the title on this part of the code
我在代码的这一部分得到了标题上的错误。
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
I understand the problem is that I'm not returning a single int, I'm returning multiple. How would I go about fixing this? My program has to be completely modular, and the main function can only be used to call the other functions.
我知道问题是我没有返回一个整数,我返回多个。我该如何解决这个问题呢?我的程序必须是完全模块化的,而主函数只能用来调用其他函数。
1 个解决方案
#1
1
There are several issues.
有几个问题。
genNumber
needs to return an int *
, not an int
, and LottoArray
in main
needs to be defined as int *
as well.
genNumber需要返回一个int *,而不是int,而LottoArray主要需要被定义为int *。
Once that is fixed, you then have the problem of genNumber
returning an array defined locally. Once the function returns, that data becomes undefined.
一旦这是固定的,您就有了genNumber返回一个本地定义的数组的问题。函数返回后,该数据就没有定义了。
Since you know how big the array is to begin with, you're probably better off passing LottoArray
(and its size) to genNumber
and writing to it directly, and changing the return type to void
.
既然您知道这个数组的开头有多大,那么您最好将LottoArray(及其大小)传递给genNumber,并直接将其写入,并将返回类型更改为void。
So you now have the following:
所以你现在有了以下几点:
#include <iostream>
#include <cstdlib>
#include <array>
using namespace std;
// Global Constants
const int ARRAY_SIZE = 7;
// Declare Functions
void genNumber(int lottoArray[], int size);
void displayResults(int lottoArray[], int size); // don't mask global variables
// Module -- Main
int main(){
// Declare Variables
int LottoArray[ARRAY_SIZE]; // define an array here
// Set Functions
genNumber(LottoArray, ARRAY_SIZE);
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
system("pause");
return 0;
}
// Module -- Generate Numbers
void genNumber(int lottoArray[], int size){
// Declare Variables for Array
int i = 0;
// Generate a Number for Each Array Placeholder
for (i = 0; i < size; i++){
// Generate Random Number in Array
lottoArray[i] = rand() % 10;
int checkNumber = lottoArray[i];
// Check for multiples
for (int i : lottoArray){
while (i == checkNumber){
checkNumber = rand() % 10;
}
}
}
}
// Module -- Display Results
void displayResults(int lottoArray[], int size){
// Declare Variables
int i = 0;
cout << "Lotto Numbers: ";
// Display Each Value in the Array
for (i = 0; i < size; i++){
cout << lottoArray[i] << " ";
}
cout << endl;
}
#1
1
There are several issues.
有几个问题。
genNumber
needs to return an int *
, not an int
, and LottoArray
in main
needs to be defined as int *
as well.
genNumber需要返回一个int *,而不是int,而LottoArray主要需要被定义为int *。
Once that is fixed, you then have the problem of genNumber
returning an array defined locally. Once the function returns, that data becomes undefined.
一旦这是固定的,您就有了genNumber返回一个本地定义的数组的问题。函数返回后,该数据就没有定义了。
Since you know how big the array is to begin with, you're probably better off passing LottoArray
(and its size) to genNumber
and writing to it directly, and changing the return type to void
.
既然您知道这个数组的开头有多大,那么您最好将LottoArray(及其大小)传递给genNumber,并直接将其写入,并将返回类型更改为void。
So you now have the following:
所以你现在有了以下几点:
#include <iostream>
#include <cstdlib>
#include <array>
using namespace std;
// Global Constants
const int ARRAY_SIZE = 7;
// Declare Functions
void genNumber(int lottoArray[], int size);
void displayResults(int lottoArray[], int size); // don't mask global variables
// Module -- Main
int main(){
// Declare Variables
int LottoArray[ARRAY_SIZE]; // define an array here
// Set Functions
genNumber(LottoArray, ARRAY_SIZE);
// Call Display Function
displayResults(LottoArray, ARRAY_SIZE);
system("pause");
return 0;
}
// Module -- Generate Numbers
void genNumber(int lottoArray[], int size){
// Declare Variables for Array
int i = 0;
// Generate a Number for Each Array Placeholder
for (i = 0; i < size; i++){
// Generate Random Number in Array
lottoArray[i] = rand() % 10;
int checkNumber = lottoArray[i];
// Check for multiples
for (int i : lottoArray){
while (i == checkNumber){
checkNumber = rand() % 10;
}
}
}
}
// Module -- Display Results
void displayResults(int lottoArray[], int size){
// Declare Variables
int i = 0;
cout << "Lotto Numbers: ";
// Display Each Value in the Array
for (i = 0; i < size; i++){
cout << lottoArray[i] << " ";
}
cout << endl;
}