Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:42:35+00:00 2026-05-27T13:42:35+00:00

Write a program that randomly selects from a bag of eight objects. Each object

  • 0

Write a program that randomly selects from a bag of eight objects.
Each object can be red, blue, orange, or green, and it can be a ball or a cube.
Assume that the bag contains one object for each combination (one red ball, one
red cube, one orange ball, one orange cube, and so on). Write code similar to
Example 5.3, using two string arrays—one to identify colors and the other to
identify shapes.

I am trying to write a program to carry out the above exercise – the problem I am having is the same object can be selected more than once each time.

This is the code so far

#include "stdafx.h"
#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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:42:36+00:00Added an answer on May 27, 2026 at 1:42 pm

    Since this is homework, I’m not going to give an exact answer, but describe what you could do:

    1. Keep a list of objects you’ve already chosen.
    2. After you choose an object, compare that object to the list of objects you’ve already chosen. If it’s in the list, choose another object. If it’s not in the list, add it to the list.
    3. Make sure that you don’t try to choose more than 8 objects, or else you’ll end up in an infinite loop in part 2.

    These would go in your choose_object() subroutine. You could do it in a while() loop, something like:

    int seen_before = 0;
    while(!seen_before) {
      pick your random numbers
      if(numbers not in list) {
        add to list
        break
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a program that read from a file that contains the
I write a program that send TCP packets from localhost to localhost. And I
I want to write a program that can turn on Caps lock key without
How can I write a program that will automatically generate a sample examination? For
I need to write a program that retrieves information from a database and creates
I want to write a program that can not close it In other words,
I have to write program that gets a number n from the user, and
I need to write a program that reads in either from ifstream or cin,
I'm willing to write program that after 5 sec shows text that was hidden
I want to write a program that would print every combination of a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.