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

  • Home
  • SEARCH
  • 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 8747165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:18:45+00:00 2026-06-13T12:18:45+00:00

This is the code for an upcoming university practical I have: import java.util.Random; public

  • 0

This is the code for an upcoming university practical I have:

import java.util.Random;

public class Practical4_Assessed 
{

public static void main(String[] args) 
{

    Random numberGenerator = new Random ();
    int[] arrayOfGenerator = new int[100];
    int[] countOfArray     = new int[10];
    int count;

    for (int countOfGenerator = 0; countOfGenerator < 100; countOfGenerator++)
    {
        count = numberGenerator.nextInt(10);
        countOfArray[count]++;
        arrayOfGenerator[countOfGenerator] = count + 1;
    }

    int countOfNumbersOnLine = 0;
    for (int countOfOutput = 0; countOfOutput < 100; countOfOutput++)
    {
        if (countOfNumbersOnLine == 10)
        {
            System.out.println("");
            countOfNumbersOnLine = 0;
            countOfOutput--;
        }
        else
        {
            if (arrayOfGenerator[countOfOutput] == 10)
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "  ");
                countOfNumbersOnLine++;
            }
            else
            {
                System.out.print(arrayOfGenerator[countOfOutput] + "   ");
                countOfNumbersOnLine++;
            }
        }
    }

    System.out.println("");
    System.out.println("");

    String occurrencesReport = "";
    String graph = "";

    for (int countOfNumbers = 0; countOfNumbers < countOfArray.length; countOfNumbers++)
    {
        occurrencesReport += "The number " + (countOfNumbers + 1) + 
            " occurs " + countOfArray[countOfNumbers] + " times.";

        if (countOfNumbers != 9)
            graph += (countOfNumbers + 1) + "   ";
        else
            graph += (countOfNumbers + 1) + "  ";

        for (int a = 0; a < countOfArray[countOfNumbers]; a++)
        {
            graph += "*";
        }
        occurrencesReport += "\n";
        graph += "\n";
    }

    System.out.println(occurrencesReport);
    System.out.println(graph);

    int max = 0;
    int test = 0;
    for (int counter = 0; counter < countOfArray.length; counter++)
    {
        if (countOfArray[counter] >= max)
        {
            max = countOfArray[counter];
            test = counter + 1;
        }
    }

    System.out.println("The number that appears the most is " + test + ".");

}
}

The program creates an array that will store 100 integers (all of which are between 1 and 10), which are generated by a random number generator, and then print out ten numbers of this array per line. It then scans these integers, counts up how often each number appears and store the results in a second array.
Following this, it outputs a horizontal bar chart of asterisks showing how often each number appears before finally outputting the number that appears the most often.

I thought I had the code totally and completely done, but I’ve just realised that if multiple numbers occur the same amount of times, the last part of my code can’t handle this, e.g. if the numbers 3 and 5 both appears 12 times, the code can only produce one of them.

Does anyone have a way around this?

Thanks,
Andrew

  • 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-06-13T12:18:45+00:00Added an answer on June 13, 2026 at 12:18 pm

    There are a couple of ways to address this, which range from quick to complex. The easiest way is to brute force it like such:

    int max = 0;
    //int test = 0;
    for (int counter = 0; counter < countOfArray.length; counter++)
    {
        if (countOfArray[counter] >= max)
        {
            max = countOfArray[counter];
            //test = counter + 1;
        }
    }
    
    System.out.print("The number that appears the most is");
    boolean first = true;
    for(int i = 0; i < countOfArray.length; i++)
    {
        if(countOfArray[i] == max)
        {
            if(first)
                first = false;
            else
                System.out.print(",");
            System.out.print(" " + (i+1) );
        }
    }
    System.out.println(".");
    

    Here’s the output:

    6   2   6   5   6   8   9   3   5   8   
    9   8   10  10  4   5   8   9   8   5   
    1   7   8   5   6   7   10  4   5   4   
    2   7   9   2   3   3   1   2   10  3   
    5   2   10  1   1   6   3   3   8   10  
    2   6   10  2   5   1   4   10  8   7   
    7   8   7   3   7   8   3   4   5   5   
    7   8   9   8   6   6   8   1   10  3   
    2   5   4   6   9   9   10  10  1   10  
    9   4   10  9   7   3   4   3   2   4   
    
    The number 1 occurs 7 times.
    The number 2 occurs 9 times.
    The number 3 occurs 11 times.
    The number 4 occurs 9 times.
    The number 5 occurs 11 times.
    The number 6 occurs 9 times.
    The number 7 occurs 9 times.
    The number 8 occurs 13 times.
    The number 9 occurs 9 times.
    The number 10 occurs 13 times.
    
    1   *******
    2   *********
    3   ***********
    4   *********
    5   ***********
    6   *********
    7   *********
    8   *************
    9   *********
    10  *************
    
    The number that appears the most is 8, 10.
    

    There are much cleaner ways to go about it, but hopefully that gives you a decent start!

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

Sidebar

Related Questions

I currently have this code: - if @ue.id = 1 = form_tag(/about-us/upcoming-events/process, :id =>
For my upcoming university C project, I'm requested to have modular code as C
This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
This code produces a different output in Python 2 and Python 3 . class
This is for an upcoming project. I have two tables - first one keeps
so in some sample code from this upcoming Core Audio Book i've encountered an
I have this code that creates a little speech bubble. I am going to
I have this code here and it works but there has to be a
This code below allows me to find the word error in all my files
This code is the core of a much larger script that works great in

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.