I have to write a program that allows a user to pick five numbers from one to 55. The computer will then generate 5 random numbers and determine how many of the users choices match the generated numbers. I have to create an algorithm for this program. The program will have a void function to generate 5 random numbers from one to 55, no duplicate numbers allowed. The random numbers and the choices will be stored in two one dimensional arrays. The program will have a void function to display the random
numbers and the users choices and the number of matches. The program will contain a loop that will generate a new set of random numbers and user choices unless the user tells the program to quit. I don't even know where to start with this. The void function is what I'm not getting mostly.
I put together this to show you the void function part and I included how to make the 5 random numbers.
Things you need to do:
-User input for 5 numbers (check each to see if they are within 1-55)
-Make a checker for what numbers the user got right (in the void function)
-Output the numbers that the user got correct (in the void function)
#include <iostream>
#include <time.h>
using namespace std;
void check(int input[5])
{
bool used;
int randNum[5];
memset(randNum, 0, sizeof(randNum));
srand(time(NULL));
//creates the 5 unique random numbers
for( int i = 0; i < 5; i++ )
{
int number = rand()%54+1;
do
{
used = false;
for( int c = 0; c < 5; c++ )
{
if( number == randNum[c] )
used = true;
}
}while(used);
randNum[i] = number;
}
//checks to see what numbers the user got right
//outputs numbers the user got right
}
int main()
{
int numbers[] = {1, 2, 3, 4, 5};
check(numbers);
system("PAUSE");
return 0;
}
Here is a possible pseudo-code for what ye' wish to accomplish:
1. Prompt the user for 5 numbers:
do{
cout << "Enter a number: ";
cin >> user_picks[i];
i++;
}while(i<5);
2. Ensure that user did not make a duplicate pick:
do{
cout << "Enter a number: ";
cin >> user_picks[i];
//You will design the is_duplicate() function
if(is_duplicate(user_picks[i]))
{
cout << "\aError! Number already used!";
i--;
}
i++;
}while(i<5);
3. Generate 5 random non-duplicated numbers:
//Seed random number generator
srand(time(NULL));
//Populate lotto[5] array with 5 unique integers
for(int i=0; i<5; i++)
{
lotto[i] = rand()%55+1;
//You will design the is_duplicate() test
if(is_duplicate(lotto[i]))
{
i--;
}
}
4. Compare the user_picks[5] array with the lotto[5] array:
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
if(user_picks[i] == lotto[j])
{
match++;
}
}
}
5. Display useful information to the user:
cout << "Your numbers: ";
for(int i=0; i<5; i++)
{
cout << user_picks[i] << ' ';
}
cout << "Official lottery drawing: ";
for(int i=0; i<5; i++)
{
cout << lotto[i] << ' ';
}
cout << "You have successfully matched " << matches << " numbers!";
I have just spoon fed you a lot of useful information that no one else would give you without any effort or attempt at programming on your part so I suggest you take this pseudo-code and run with it.
Here is what I ended up with, but now I'm at a loss as to how to get it to repeat until the user quits.
#include <iostream>
#include <ctime>
using namespace std;
//function prototype
void randomNumbers (int []);
void displayNumbers (int []);
void displayMatches (int[],int []);
void getChoices(int []);
int main()
{
//declare variables
int choice [5]={0};
int matchingNum[5] = {0};
int num[5]={0};
//get choices
getChoices(choice);
randomNumbers(num);
//call function to display random numbers
cout << "The random numbers are: " << endl;
displayNumbers(num);
//call function to display choices
cout << "Your choices were: " << endl;
displayNumbers(choice);
//call function to display number of correct choices
displayMatches (choice,num);
//quit program prompt
// cout << "End of program" << endl;
return 0;
} //end of main function
//******function definitions******
void randomNumbers (int lotto[])
{
bool check[56]={false};
bool again= true;
int randomNumber=0;
srand (static_cast<int>(time(0)));
for(int x=0;x<5;++x)
{
do
{
randomNumber = 1 + rand() % (55 - 1 +1);
if(check[randomNumber]==false)
{ lotto[x]=randomNumber;
check[randomNumber]=true;
again = false;
}
}while(again==true);
again= true;
}
} //end of randomNumbers function
void displayNumbers (int num[])
{ for(int x =0;x<5;++x)
{
cout << num[x]<<endl;
} //end of displayRandom function
}
void getChoices( int choice[])
{
bool check[56]={false};
int pick=0;
for(int x=0;x<5;++x)
{
//get input items
cout << "Pick five random numbers from 1 to 55"<< endl;
cout << x+1<<" Number: ";
cin>>pick;
if(check[pick]==false)
{
choice[x]=pick;
check[pick]=true;
}
else
{
cout<< "Please enter another number you may not have two picks of the same number or"<<endl;
cout<<"greater than 55"<<endl;
--x;
}
}
}
void displayMatches (int choice[],int lotto[])
{
int match=0;
cout<<"Your matches are: "<<endl;
for(int x=0;x<5;++x)
{
for(int y=0 ;y<5;++y)
{
if(choice[x]==lotto[y])
{
cout<<choice[x]<<endl;
++match;
}
}
}
if (match==5)
cout<<"You were the big winner you matched all 5 numbers"<<endl;
else
{
if(match!=0)
cout<< "you matched "<< match<<" numbers"<<endl;
else
cout<<"You loose! You matched 0 numbers"<<endl;
}
}
create do-while loop in main function
do {
//lines #19-#32
//ask user if he wants to quit ("press q to quit")
//get his answer
while (/*!(check if q was pressed)*/);
What's more I suppose You'll have to reset some arrays/variables.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.