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 7682245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:31:25+00:00 2026-05-31T18:31:25+00:00

Homework. Dice Game. I’ve got an array that represents five rolls of a die.

  • 0

Homework. Dice Game. I’ve got an array that represents five rolls of a die. Consider:
diceRoll[] = {6,3,3,4,5}. I would LIKE to create a SECOND array that has the counts of values from one to six contained in diceRoll[], (e.g., occurence[] = {0,0,2,1,1,1} for the diceRoll[] above.) but I fear I’m getting lost in nested loops and can’t seem to figure out which value I ~should~ be returning. occurence[] is a global variable, and the intent is that the array will contain six values…the count of ones (at index [0]), twos (at [1]), threes (at [2]), etc.

So Far:

 for(i=1;i<7;i++)   /* die values 1 - 6
    {
       for(j=0;j<diceRoll.length;j++)  /* number of dice
       {
          if (diceRoll[j] == i)  /* increment occurences when die[j] equals 1, then 2, etc.
             occurence = occurence + 1;
       }
    }
    return occurence;
    }

I cannot, however, get the occurence=occurence+1 to work. bad operand types for binary operator is my most common error. I suspect I need to increment occurence OUTSIDE one or both of the for loops, but I’m lost.

Guidance? or perhaps the one-line easy way to do this?
d

  • 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-31T18:31:26+00:00Added an answer on May 31, 2026 at 6:31 pm

    The easiest way I have to do this is to create the second array in order so that
    occurrence[0] = # of 1’s occurrence[1] = # of 2’s and so on. Then this becomes a 1 loop method.

    //method to return number of occurrences of the numbers in diceRolls
    int[] countOccurrences(int[] diceRolls) {
        int occurrence[] = new int[6]; //to hold the counts
    
        for(int i = 0; i < diceRolls.length; i++) { //Loop over the dice rolls array
           int value = diceRolls[i]; //Get the value of the next roll
           occurence[value]++; //Increment the value in the count array this is equivalent to occurrence[value] = occurrence[value] + 1;
           //occurrence[diceRolls[i]]++; I broke this into two lines for explanation purposes
        }
    
        return occurrence; //return the counts 
    } 
    

    EDIT:

    Then to get the count for any particular value use occurrence[value-1]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Homework: Consider the two-dimensional array A: int A[][] = new int[200][200]; where A[0][0] is
For a homework assignment I was given a Card class that has enumerated types
I'm doing a homework project that requires this: Below you will find the code
For homework, I am writing a program that deals with a lot of time_t
For homework, I'm trying to create a CustomButton that has a frame and in
Sounds like a homework? no it's not. I worked up the logic for this
I am doing a homework assignment that deals with classes and objects. In it,
I have a homework problem where I have to make an inheritance program that
I have a homework problem that I really don't know how to start. Here
This IS NOT homework, but it is a practice that the teacher gave us

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.