For the lab assignment we have to use arrays to to some kind of lottery. Our profesor hasn't been helping much with it and it's due tonight. I have very limited knowledge of this and am trying to learn. Here's the instruction:
Lottery. In this assignment you are to code a program that selects 10 random non-repeating positive numbers (the lottery numbers), takes a single input from the user and checks the input with the ten lottery numbers. The user wins if her input matches one of the lottery numbers.
and the code: (any help would matter since I desperately need help.
//Lottery
//10/12/15
//liza henriquez
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::endl; using std::cin;
constint lotNumber = 10;
int wins[lotNumber];
void fillUp(int [], int size){
size = lotNumber;
for (int i = 0; i < size; ++i)
size = wins[i] - 1;
}
void draw(int i, int j){
int lotteryNum = 0, size, number, wins, random;
while (lotteryNum < 10){
for (i = 0; i < 10; i++)
int random;
srand(time(nullptr));
random = rand() % 100; // gives random number
check(wins[lotteryNum], size, number);
if (false){
lotteryNum++;
}
//check();
}
}
void entry(int ans){
cout << "Enter a single number from 0 to 99:" << endl;
cin >> ans;
cout << ans << endl;
}
bool check(int wins[lotNumber], int size, int number){
int ans, lotteryNum;
while (i < 10){
for (int i = 0; i < size; i++)
if (wins[i] == ans)
returntrue;
returnfalse;
}
void printOut(){
cout << "echoing array:ln";
for (int i = 0 ; i < 10; i++)
cout << wins[i];
cout << "You won!";
cout << "You lost";
}
Where is your main function?
Have you learned about linear search in the array? If so, below is a code example using the linear search to find an element in the array.
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int searchList(constint [], int, int);
int main()
{
constint MAXVALUE{ 100 }; // Maximum Value for lottery
constint MINVALUE{ 0 }; // Minimum Value for lottery
constint lotNumber{ 10 }; // Size of the array (10).
int wins[lotNumber]; // declare an array of 10 elements.
int number{ 0 };
unsigned seed = time(nullptr); // Get system time.
srand(seed); // Seed the random number
// Run a for loop to insert 10 random numbers into that array.
for (int count = 0; count < lotNumber; count++)
{
wins[count] = (rand() % (MAXVALUE - MINVALUE - 1)) + MINVALUE;
cout << wins[count] << endl; // Display the list of 10 elements to test the random numbers.
}
cout << "Type a number to see if it matches with the array>>>";
cin >> number;
searchList(wins, lotNumber, number);
if (searchList(wins, lotNumber, number) == -1) //-1 is not a valid subscript in the array
cout << "You lose" << endl;
else
cout << "You win" << endl;
return 0;
}
// Linear search.
int searchList(constint list[], int numElems, int value)
{
int index = 0; // Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true; // Set the flag
position = index; // Record the value's subscript
}
index++; // Go to the next element
}
return position;
}
//Lottery
//10/12/15
//liza henriquez
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::endl; using std::cin;
constint lottMax = 10;
int wins[lottMax];
void fillUp(int wins[], int lottMax){
for (int i = 0; i < lottMax; ++i)
wins[i] = - 1;
}
void draw(int i, int random){
int lotteryNum = 0, number, wins, random;
constint lottMax = 10;
while (lotteryNum < lottMax){
for (i = 0; i < lottMax; i++)
srand(time(nullptr));
random = rand() % 100; // gives random number
check(wins,number);
if (false){
lotteryNum++;
}
}
}
void entry(int random){
cout << "Enter a single number from 0 to 99:" << endl;
cin >> random;
cout << random << endl;
}
bool check(int wins[], int number){
int lotNumber, i;
for (int i = 0; i < lottMax; ++i)
if (wins[i] == lotNumber)
returntrue;
else{
returnfalse;
}
}
void printOut(){
cout << "echoing array:ln";
for (int i = 0 ; i < 10; i++)
cout << wins[i];
if (true){
cout << "You won!";
}
else (false);
cout << "You lost";
}
I keep getting an error on the check function inside draw. It says:
argument of type "int" is incompatible with parameter of type "int *".
Line 20: int lotteryNum = 0, number, /*wins,*/ random;
Line 20 you are declaring a single int wins, which is used as the argument to the check function on line 26. I guess you actually meant to be using the (global) int wins[lottMax]; that you declared on line 12.
@lhenriqu, I am sure you are getting or will get more errors than the one you mentioned. Take a look at the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool check(int wins[], int number) // The argument number is in the function, but it is not used.
{
int lotNumber, i;
for (int i = 0; i < lottMax; ++i) // lottMax is undefined.
// Missing the enclosing brackets {} to loop thru the if/else statement.
if (wins[i] == lotNumber) // This will get a runtime error as lotNumber is undefined
returntrue;
else
{
returnfalse;
}
}
and...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void draw(int i, int random) // Why are you using random as a argument?
{
int lotteryNum = 0, number, wins, random;
constint lottMax = 10;
while (lotteryNum < lottMax)
{
for (i = 0; i < lottMax; i++) // This for loop is only for the seeding the random number.
srand(time(nullptr)); // You should not need to seed the random number 10 times.
random = rand() % 100; // gives random number (Only one number not 10)
check(wins, number); // You will get a runtime error as variable number is undefined.
if (false)
{
lotteryNum++;
}
}
}
Since I do not know the order of the functions, which you are calling in the main function, I cannot provide additional details about it.
You could use the linear search for the check function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool check(constint list[], int numElems, int value)
{
int index = 0; // Used as a subscript to search array
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
found = true; // Set the flag
index++; // Go to the next element
}
return found;
}
Then in you could notify the user if they lose or won, based on the number the chose like this:
1 2 3 4 5 6 7 8 9
if (!check(wins, lotNumber, number)) //if the number is not found.
{
cout << "You lose" << endl;
cout << "The lottery numbers were: " << endl;
for (int count = 0; count < lotNumber; count++)
cout << wins[count] << " ";
}
else
cout << "You win";