Write a program that simulates a lottery. The program should have an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element in the array. The user should enter five digits which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that match. For example, the following shows the lottery array and the user array with sample numbers stored in each. There are two matching digits (elements 2 and 4).

lottery array:
5 2 6 9 1

user array:
1 3 6 8 1

The program should display the random numbers stored in the lottery array and the number of digits matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner.

>> Anyone can do this in C++

Yeah !

>> Anyone can do this in C++

Yeah !

Can u show me the important part of doing this program. Because I cant figure how to use array 4 this program.

maybe you show us what you have done and we can show you the right way... WE DON'T GIVE HOMEWORK TO THOSE WHO DON'T SHOW ANY EFFORT

we surely can do it... can you?

Member Avatar for Dukane

If anyone can do this in C++ then why can't you?

PS: You were wrong, anyone includes me, and I don't know C++ well enough to do this. ;)

i guess the title is missing a question mark in the end...

I don't know C++ well enough to do this. ;)

If you don't know C++ well enough to even give it a try, you should just step back and learn the basics, give this problem a try and come back if you have some issues. Because there is no way someone is gonna teach you C++ language in one post here.

If you don't know C++ well enough to even give it a try, you should just step back and learn the basics, give this problem a try and come back if you have some issues. Because there is no way someone is gonna teach you C++ language in one post here.

this is my program..

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>

#include <ctime>

int main()
{
	int a;
	int b;
	int c;
	int d;
	int e;
	int A;
	int B;
	int C;
	int D;
	int E;

	cout << "Please Enter 1st Number of Your Prediction" << endl;
	cin >> A;

	while (A < 0)
	{
	 cout << "\nInvalid Number Enter" << "Please Renter 1st Number of Your Prediction\n";
	 cin >> A;
	break;
	}

	cout << "\nPlease Enter 2nd Number of Your Prediction" << endl;
	cin >> B;

	while (B < 0)
	{
	 cout << "\nInvalid Number Enter" << "Please Renter 2nd Number of Your Prediction\n";
	 cin >> B;
	break;
	}

	cout << "\nPlease Enter 3rd Number of Your Prediction" << endl;
	cin >> C;

	while (C < 0)
	{
	 cout << "\nInvalid Number Enter" << "Please Renter 3rd Number of Your Prediction\n";
	 cin >> C;
	break;
	}
	
	cout << "\nPlease Enter 4th Number of Your Prediction" << endl;
	cin >> D;

	while (D < 0)
	{
	 cout << "\nInvalid Number Enter" << "Please Renter 4th Number of Your Prediction\n";
	 cin >> D;
	break;
	}
	
	cout << "\nPlease Enter 5th Number of Your Prediction" << endl;
	cin >> E;

	while (E < 0)
	{
	 cout << "\nInvalid Number Enter" << "Please Renter 5nd Number of Your Prediction\n";
	 cin >> E;
	break;
	}

	
	srand ( time(0) );

	a = ( rand()%10 );
	b = ( rand()%10 );
	c = ( rand()%10 );
	d = ( rand()%10 );
	e = ( rand()%10 );


	
	cout << "\nUser Array" << endl;
	cout << "---------------------" << endl;
	cout << "| " << A << " | " << B << " | " << C << " | " << D << " | " << E << " | " << endl;
	cout << "---------------------" << endl;
	cout << "\nLottery Array" << endl;
	cout << "---------------------" << endl;
	cout << "| " << a << " | " << b << " | " << c << " | " << d << " | " << e << " | " << endl;
	cout << "---------------------" << endl;

	{
		if (a == A)
			cout << "\n1st number is correct" << endl;

		else (a =! A);
			cout << "\n1st number is wrong" << endl;
	}

	{
		if (b == B)
			cout << "\n2nd number is correct" << endl;

		else (b =! B);
			cout << "\n2nd number is wrong" << endl;
	}

	{
		if (c == C)
			cout << "\n3rd number is correct" << endl;

		else (c =! C);
			cout << "\n3rd number is wrong" << endl;
	}

	{
		if (d == D)
			cout << "\n4th number is correct" << endl;

		else (d =! D);
			cout << "\n4th number is wrong" << endl;
	}

	{
		if (e == E)
			cout << "\n5th number is correct" << endl;

		else (e =! E);
			cout << "\n5th number is wrong" << endl;
	}
	
	return 0;

}

i dont know how to do in array.. btw this is not my homework.. im learning C++ by myself.. i just got this question from a friend.. without answer.. i just need someone to teach me how to use array

Hey !!! I suggest you to come with an algorithm or psuedocode for your problem and one of us here would let you know where you are wrong or what is missing link in the same.

Do not worry abt the nature of the psuedocode - let it be in simple engish language in stepwise sequence -- but come out with one.

you can then go ahead and put the same in coding.

First thing I'd do is calculate the random value first. This way you have the option of asking "Do you want to try another number?"

Next thing, rather than asking for each individual number (since you are only using 0-9) have the user enter a 5 digit value.

I'll let you work out the syntax for your program, but an array is just a variable that can contain multiple values. For example: int ary[5] defines the variable ary to contain 5 values, ary[0] thru ary[4]. So in your code,
ary[0] would be a
ary[1] would be b
ary[2] would be c
ary[3] would be d
ary[4] would be e

One of your requirement was "....and keep a count of the digits that match .... "
but your code is silent on this.
I suggest you can use a simple int which is initialized at start and would be incremented in the " if part of comparison ( a == A)" of the code.
The value of the variable after all such ifs ( i.e ..e == E )
would be value of digits that are same and the same can be printed.

Further ... knowledge of arrarys that definitely simplify the code.

I suggest you to refer to K & R - the C programming text - concept wise it is the best and price wise affordable aswell.

you can declare a single dimensional array as
int lottery [5] // it can hold only 5 values.
int user[5]
and for receiving the values
int i = 0;

while ( i < 5) {
cin >> lottery
i++;
}
... like wise for user array and now you tell me on the comparison logic using arrays.

Look out for info at the following link http://www.daniweb.com/forums/thread50370.html
at one stage the info contained was of great help to me.

this is my program..

See now we know what you don't know.. :)
See this link to learn teh abt arrays in C++. It tells you:
- What is an array?
- How create an array ?
- How to initialize an array ?
- How to access elements of an array ?
- Finally multidimensional arrays are described if you're interested.

commented: learn to spell 'the' properly -2

Wow! That's some scary looking code you've cooked up!
Why don't you grab a copy of the c++ primer or some book like it. Go through it completely before attempting something like this. I think you are just going to confuse the hell out of yourself if you keep going in this way.
Just a tip from another code neophyte...

Here's a snippet to get the input into an array:


int arr[5];
int i =0;
int x;
while (i<5)
{
cin >> x;
arr = x;
i++
}

Thanks for all the tips.. after i finish i will post it out to let u all to have a look.. thanks again

this is my final program...

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cstdlib>

#include <ctime>

int main()
{
    int wilson;
    const int Size = 5;
    int lottery[Size];
    int wilsonArray[Size];
    int frequency=0;  

    cout << "Please Enter 5 Number of Your Prediction?" << endl;
    cin >> wilson;

    while (wilson < 0 || wilson > 99999)
    {
        cout << "Invalid Number Enter" << endl << "Please Re-enter 5 Number of Your Prediction?" << endl;
        cin >> wilson;
    }

    if( wilson >= 0 && wilson < 100000)
    { 

        wilsonArray[0]=wilson/10000;
        wilsonArray[1]=wilson%10000/1000; 
        wilsonArray[2]=wilson%1000/100;
        wilsonArray[3]=wilson%100/10; 
        wilsonArray[4]=wilson%10;

        cout << "\nWilson Prediction" << endl;
        cout << " *********************" << endl << " | ";
        for(int i = 0; i < 5; i++)
        cout << wilsonArray[i] << " | ";
        cout << "\n *********************" << endl;

        srand(time(0));

        cout << "\nLottery Result"<< endl;
        cout << " *********************" << endl << " | ";
        for (int j = 0; j < 5; j++)
        {
            lottery[j] = rand() % 10;
            cout << lottery[j] << " | ";
            
        }
        cout << "\n *********************" << endl;
        for (int k = 0; k < 5; k++)
            if(wilsonArray[k] == lottery[k])
                ++frequency;

            if(frequency == 0 )
                cout << "\nThere are none of your prediction that matches" << endl;
            if(frequency == 1 )
                cout << "\nThere are one number of your prediction that matches" << endl;
            if(frequency == 2 )
                cout << "\nThere are two number of your prediction that matches" << endl;
            if(frequency == 3 )
                cout << "\nThere are three number of your prediction that matches" << endl;
            if(frequency == 4 )
                cout << "\nThere are four number of your prediction that matches" << endl;
            if(frequency == 5 )
                cout << "\n***************************************************" << endl << "| Congratulation!! All of Your Prediction Matches |" << endl<< "***************************************************" << endl;
            
    }
            
    
    return 0;
}

Its really gr8 ... that you have come up with a this fine code during your initial attempts.
I have a few suggestions:
instead of :

wilsonArray[0]=wilson/10000;  
wilsonArray[1]=wilson%10000/1000;         wilsonArray[2]=wilson%1000/100;        wilsonArray[3]=wilson%100/10;         wilsonArray[4]=wilson%10;

you can also use :

int i=0;
while (n > 0) {
int a = n%10;
wilsonArray[i++] =a;
n = n/10;

also the frequency can be given in a single statement as :

if ( frequency < 5) {
 cout << "\nThere are" << frequency  << "  number of your prediction that matches" << endl;
} 
else if (frequency == 5) {
  Congratulation!! All of Your Prediction Matches |" << 
}

pl. do correct me if I am wrong.

Thanks and Regards
Ram Sharma

Since the frequency is initialized with 0 and can hold a maximum value of 5, we can also write:

if(frequency != 5) 
    cout << "\nThere are" << frequency << " number of your prediction that matches";
else 
    cout << "Congratulation!! All of Your Prediction Matches ";

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.