Question

In: Computer Science

In C++, Write the following program using a vector: A common game is the lottery card....

In C++, Write the following program using a vector:

A common game is the lottery card. The card has numbered spots of which a certain number are selected at random. Write a Lotto() function that takes two arguments. The first should be the number of spots on a lottery card and the second should be the number of spots selected at random. The function should return a vector object that contains, in sorted order, the numbers selected at random. Use your function as follows:

Vector winners;

winners = Lotto(51,6);

This would assign to winners a vector that contains six numbers selected randomly from the range 1 through 51.

Also write a short program that lets you test the function,

Solutions

Expert Solution

Thanks for the question.


Here is the completed code for this problem.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer.

Thanks
===========================================================================

//Header files for I/O,vector use and for shuffling and sort
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
//Function prototype
vector<int> Lotto(int, int);
//Test
int main()
{
   //Declare a vector
   vector<int> winners;
   //Call function to get winners lotto
   winners = Lotto(51, 6);
   //Display lotto values
   for (int i = 0; i < winners.size(); i++) {
       cout << winners.at(i) << " ";
   }
   cout << endl;
}
/*
    Function to generate random lotto values
   Take maximum of random number and count of lotto number as parameter
   Return winners lotto vector
*/
vector<int> Lotto(int maxRand, int cnt) {
   //Return vector
   vector<int> winners;
   //Tempory vector
   vector<int> temp;
   //Put 1 to maxRand numbers
   for (int i = 1; i <= maxRand; i++) {
       temp.push_back(i);
   }
   //Shuffle the vector
   random_shuffle(temp.begin(), temp.end());
   ////Put first cnt of values into winners lotto
   for (int i = 0; i < cnt; i++) {
       winners.push_back(temp.at(i));
   }
   //Sort the vector ascending order
   sort(winners.begin(), winners.end());
   //Return resulting vector
   return winners;
}

-----------------------------------------

Please appreciate this help with an up vote !!

thank you so much : )


Related Solutions

Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
IN C++ Write a program to play the Card Guessing Game. Your program must give the...
IN C++ Write a program to play the Card Guessing Game. Your program must give the user the following choices: - Guess only the face value of the card. -Guess only the suit of the card. -Guess both the face value and suit of the card. Before the start of the game, create a deck of cards. Before each guess, use the function random_shuffle to randomly shuffle the deck.
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
in C language only. Write a program called gamecards.c that has a card game logic by...
in C language only. Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card. 1. Every card must be represented by exactly two chars representing a rank and a suit. ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10) Suits: 'H', 'D', 'S', 'C'. 7D represents the “7 of Diamond” etc.. 2. Write a function called...
Write a C program called cards.c that simulates some card game logic by comparing the cards...
Write a C program called cards.c that simulates some card game logic by comparing the cards from 4 people and determining which person has the best card. The program MUST work as follows: Eachcardmustberepresentedbyexactlytwocharsrepresenting a rank and a suit. The possible rank options are: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. The possiblesuit options are: 'H', 'D', 'S', 'C'. So 6H represents the “6 of hearts”, JC represents the “Jack of Clubs” etc... YouMUSTwriteafunctioncalledisValidRank(charc)which...
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT