Homework: Write a similar program to DrawCards.cpp for a bag that contains the e
ID: 3860205 • Letter: H
Question
Homework: Write a similar program to DrawCards.cpp for a bag that contains the eight objects: Each item has a unique combination of color (red, blue, orange, green) and shape (ball, cube). Every time an object is picked from the bag, it can't be picked again, so the number of possible choices decreases by one. The logic should be identical to that in DrawCards.cpp, but the array settings will differ. You may also want to give your variables different names, such as items_remaining and (for the integer array) items picked. The program should also deal with the case when we run out of items. Simulate replacing all items in the bag and continue picking items. (Hint: Read the book) DrawCards.cpp OutputExplanation / Answer
Here by i have the similar code for yourrequirements Kindly checkit.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int rand_0toN1(int n);
void choose_object();
char *colour[4] =
{"Red", "Blue", "Orange", "Green"};
char *object[2] =
{"Ball", "Cube"};
int main()
{
int n, i;
srand(time(NULL)); // Set seed for randomizing.
while (1) {
cout << "Enter no. of objects to draw ";
cout << "(0 to exit): ";
cin >> n;
if (n == 0)
break;
for (i = 1; i <= n; i++)
choose_object();
}
return 0;
}
void choose_object() {
int c; // Random index (0 thru 4) into
// colours array
int o; // Random index (0 thru 2) into
// object array
c = rand_0toN1(4);
o = rand_0toN1(2);
cout << colour[c] << "," << object[o] << endl;
}
int rand_0toN1(int n) {
return rand() % n;
}
Output
Enter no. of objects to draw (0 to exit): 2
Blue,Cube
Orange,Cube
Enter no. of objects to draw (0 to exit): 3
Blue,Ball
Orange,Cube
Green,Cube
Enter no. of objects to draw (0 to exit): 4
Red,Ball
Orange,Cube
Orange,Ball
Red,Ball
Enter no. of objects to draw (0 to exit):
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.