Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ (cpp) please read carefully Your club is in a process of creating a weekly l

ID: 3773774 • Letter: C

Question

C++ (cpp) please read carefully

Your club is in a process of creating a weekly lottery. Once a week, five distinct random integers between 1 to 40 (inclusive) are drawn. If a player guesses all of the numbers correctly, the player wins a certain amount.

Write a program that does the following:
Generates five distinct random numbers between 1 and 40 (inclusive) and stores them in an array. You need to use function for populating array of random numbers.
Sorts the array containing the lottery numbers using a function.
Prompts the player to select five distinct integers between 1 and 40 (inclusive) and stores the numbers in an array. The player can select the numbers in any order, and the array containing the numbers need not be sorted.
Determines whether the player guessed all the lottery numbers correctly. If the player guessed the lottery numbers correctly, it outputs the message ‘‘You win!’’; otherwise it outputs the message ‘‘You lose!’’ and outputs the lottery numbers.
Your program should allow a player to play the game as many times as the player wants to play. Before each play, generate a new set of lottery numbers.

#include <iostream>

using namespace std;

next?????

Explanation / Answer

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARR_SIZE 5
#define MAX 40

using namespace std;

int gen_rand(int array[]){
srand(time(NULL));
int hasDuplicate;
for(int i=0;i<ARR_SIZE;i++)
{
array[i] = (rand() % MAX)+1 ;
//printf("array[%d] = %d ", i, array[i]);
hasDuplicate = 0;

for(int j=0; j<=(i-1); j++)
{
if (array[i] == array[j])
{
hasDuplicate = 1;
i--;
}

}
}
return 0;
}

int sort_array(int array[]){
for(int i=0;i<ARR_SIZE;i++)
for(int j=0;j<ARR_SIZE-i-1;j++)
if(array[j]>array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
// for(int i=0;i<ARR_SIZE;i++)
// cout<<array[i]<<" ";
return 0;
  
}
int main()
{
int k,opt;
int array[ARR_SIZE];
int array1[ARR_SIZE];
while(1){
srand(time(NULL));
gen_rand(array);
sort_array(array);
cout<<"Select 5 distinct numbers between 1 and 40 (inclusive)";
for(int i=0;i<ARR_SIZE;i++)
cin>>array1[i];
sort_array(array1);
for(k=0;k<ARR_SIZE;k++)
if(array[k] != array1[k]){
cout<<" You lose!";
cout<<" The lottery numbers are : ";
for(int j=0;j<ARR_SIZE;j++)
cout<<array[j]<<" ";
break;
}
if(k==ARR_SIZE)
cout<<" You win!";
cout<<" Do you want to continue ? 1: to continue ";
cin>>opt;
  
if(opt!=1)
break;
cout<<" ";
}
return 0;
}