C# Program Write a program that simulates a lottery. The program should have arr
ID: 3837480 • Letter: C
Question
C# Program
Write a program that simulates a lottery. The program should have array of 6 integers named winning, with a randomly generated number in the range of 1 through 9 for each element in the array. The program should ask user to enter 6 numbers and store them in another integer array named player. The program then compares the numbers in 2 arrays to find out how many numbers match.
If the two numbers are on the same position in both arrays, it is a match. (This is using parallel array concept to compare two arrays.) The program output should display the winning numbers, player’s numbers, and how many numbers matched. For instance, if your winning numbers are 3,5,9,1,4,7 and your player’s numbers are 2,5, 7,1, 9,8, then you have two matches (5 and 1). 5 is on the second position and 1 is on the fourth position in both arrays.
Input Validation: Do not accept player’s number out of range of 1-9.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, n,x,count;
time_t t;
int lottery[6]; /* lottery is an array of 6 integers for storing random numbers generated*/
int player[6]; /* playeris an array of 6 integers for storing numbers entered from user*/
n = 6;
count=0;
/* Intializes random number generator */
srand((unsigned) time(&t));
/* stores 6 random numbers from 0 to 10 in lottery array*/
for( i = 0 ; i < n ; i++ )
{
lottery[i]=rand()%10;
}
/* Entering value in player array*/
for(i=0;i<n;i++)
{
printf("Enter a positive value from 1 to 9 ");
scanf("%d",&x);
if(x<=9&&x>=0)
{
player[i]=x;
}
else
{
i=i-1;
printf("Numbers between 0 to 9 are valid Enter again!!!");
}
}
printf("Value matched:-- ");
for (i=0;i<n;i++)
{
if(player[i]==lottery[i])
{
count=count+1;
printf("%d value matched at position %d",player[i],(i+1));
}
}
printf("Total matched count=%d",count);
return(0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.