Create a simple lottery program using C. A user will input four integer values b
ID: 3817658 • Letter: C
Question
Create a simple lottery program using C. A user will input four integer values between 0 and 9. Your program will then randomly generate four random integer numbers and compare them with the user's inputs. Use a two-dimensional array to store all numbers, i.e. user inputs in column 1 and computer generated numbers in column 2 of the array. Your program should also prompt user to re-enter the value if it exceeds 9. Likewise, it the input is less than 0, your program should also prompt for re-entry. Declare "Win" if at least two consecutive computer generated numbers are the same as user's inputs. If all the four numbers are the same, then declare "Strike" Otherwise, "Loose". See Figure 4 for sample execution and output. Enter an integer value between 0 and 9: 2 Enter an integer value between 0 and 9: 9 Enter an integer value between 0 and 9: 19 Invalid input. Integer should be between 0 and 9 only. Enter an integer value between 0 and 9: 3 Enter an integer value between 0 and 9: 6 Your input numbers are 2 9 3 6 Computer generated numbers are 1 8 1 7 *** RESULTS *** LooseExplanation / Answer
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<time.h>
int main()
{
srand(time(NULL));
int lottory[4][4];
int j,i;
for(j=0;j<4;j++){
printf("Enter a integer value between 0 and 9: ");
scanf("%d",&lottory[j][0]);
while(lottory[j][0]<0 || lottory[j][0]>9){
printf("Ivalid Input.Integer should be between 0 and 9 only Try again. ");
printf("Enter a integer value between 0 and 9: ");
scanf("%d",&lottory[j][0]);
}
}
for(j=0;j<4;j++){
lottory[j][1] =(int) rand()%9+1;
}
//print the 2 dimensional array col1 is useri/p and col2 is system generated
printf(" Your input numbers are ");
for(i=0;i<4;i++)
printf("%d ",lottory[i][0]);
printf(" Computer generated numbers are ");
for(j=0;j<4;j++)
printf("%d ",lottory[j][1]);
printf(" **********RESULTS*********** ");
int p,q,flag=0;
for(p=0;p<4;p++)
{
if(lottory[p][0]==lottory[p][1] )
flag++;
}
if(flag==2){
printf(" Win ");
}else {
if(flag==4)
printf(" Strike ");
else
printf(" Loose ");
}
}
If u want to test if program rus as expected,get the generated numbers printed on the screen and based on that input ur values ad check :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.