Java Programming Tutorial – 30 – Array Elements as Counters

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Comments

28 responses to “Java Programming Tutorial – 30 – Array Elements as Counters”

  1. Devin Newton Avatar

    Bro, your videos are top notch lol. "Freq…..not that type of freq"

  2. Earl John Calizar Avatar

    is it fine that I use "import java.util.*;"
    to use all inside a library

  3. Vivek Muricken Avatar

    import java.util.*;
    public class ArrCounter {
    public static void main (String args[]){
    Random vk = new Random();
    int freq[] = new int[7];
    int Value[]= new int[7];
    for(int i=1;i<7;i++){
    Value[i]=++freq[1+vk.nextInt(6)];

    System.out.println(Value[i]);
    }
    System.out.println("FacetFrequency");
    for (int face=1;face<freq.length;face++){
    System.out.println(face+"t"+freq[face]);
    }
    }
    }

    Now lets see Value array will store the random value and face frequency will display the frequency of that number to display. Logic is wrong 🙁

  4. Moey Nabeel Avatar

    I love this community, Finally a comment section that is actually useful 🙂

  5. Jorge Bernal Avatar

    What do you think?

    import java.util.Scanner;
    import java.util.Random;

    public class Apples {
    public static void main(String args[]) {

    Random rand = new Random();
    double vect[] = new double[6];

    for(int i = 0; i < 10000; i++) {
    vect[rand.nextInt(6)]++;
    }

    System.out.println("FacetFrecuency");

    for(int i = 0; i < vect.length; i++) {

    System.out.printf("%dt%.1f", (1 + i), (vect[i]/10000) * 100);
    System.out.println("%");
    }
    }
    }

  6. Vinti Avatar

    It works as well as you don't need to put "1"+rand.nextInt(6). You can put " rand.nextInt(7) "

  7. Antwan Schreiner Avatar

    instead of using ++freq[1+rand.nextInt(6)]; why cant we just do ++freq[rand.nextInt(7)]; ? am i missing something or is this just another plausable way to write the code?

  8. RCTestKid Avatar

    Loving how simple and easy Java is.
    However I have experience with C#/C++ so I guess it's to be expected.

  9. Mohanad Alkhashtee Avatar

    Smart presentation. Thank you very much

  10. Mohamed Abdullah Avatar

    What if i make Some Thing Like That

    for(roll=0;roll<100;roll++)
    {
    freq[roll] = 1+rand.nextInt(6);
    }

  11. Pedro Gouveia Avatar

    para os tugas que estão ai ca vai um programinha import java.util.Random;
    import java.util.Scanner;

    public class Teste {

    public static void main (String args[]){

    Scanner sc = new Scanner(System.in);

    Random dice = new Random();

    int frequencia[] = new int [7];

    System.out.println("Quantas vezes quer lançar o dado ?");
    int vezes = sc.nextInt();

    for (int lancar=1;lancar<=vezes;++lancar){

    ++frequencia[1+dice.nextInt(6)];

    }

    System.out.println("FacetFrequencia");

    for(int contador=1;contador<=6;++contador){

    float percentagem = ((float)frequencia[contador] / (float)vezes) * 100;

    System.out.println(contador+"t"+percentagem+"%");

    }

    }

  12. Peng Xu Avatar

    for the 'freq[face]' at line 15, it means get the value of array freq at index face, doesn't it?

  13. Borhan Amini Avatar

    for those who didn't understand the ++freq[index] , I have done bucky's code in a more understandable way that if you compare them you will definitely get the point. Hopefully, it would be helpful. you just need to create a test class test.java in copy paste the code blow.

    import java.util.Random;
    import java.util.Scanner;
    public class test {
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    Random dice = new Random();
    int roll, rndNumber;
    int freq[] = new int[7];
    int freq2[] = new int[7];

    System.out.println("how many random number: ");
    roll = input.nextInt();
    for(int i = 0; i < roll; i++){
    rndNumber = 1+dice.nextInt(6);

    if(rndNumber == 1){
    freq[1] += 1;
    }else if(rndNumber == 2){
    freq[2] += 1;
    }else if(rndNumber == 3){
    freq[3] += 1;
    }else if(rndNumber == 4){
    freq[4] += 1;
    }else if(rndNumber == 5){
    freq[5] += 1;
    }else if(rndNumber == 6){
    freq[6] += 1;
    }

    ++freq2[rndNumber];
    }

    for(int face = 1; face < freq.length; face++){
    System.out.println(face + "t" + freq[face]);
    }

    System.out.println("-————–");

    for(int face = 1; face < freq.length; face++){
    System.out.println(face + "t" + freq2[face]);
    }
    }
    }

  14. 李白 Avatar

    this program is more clever than many beginners think!

  15. Bryan Avatar

    The highest number on a dice is 6, so how can i make it that it will only go between 1 and 6?

  16. Clash with A Square Avatar

    if u dont understand ++freq.. its simple….. by hard it

  17. Jugraj Singh Avatar

    nice but didn't understand it.:-(

  18. RumikXxeno Avatar

    I made it just easier to understand … 🙂 Code below

    import java.util.Scanner;
    import java.util.Random;

    public class blabla {
    public static void main(String args[]){
    Random rand = new Random();
    int face[] = new int[6];

    for(int i= 0; i < 1000; i++){
    face[rand.nextInt(6)]++;

    }
    System.out.println("FacetFrequency");
    for(int j = 0; j < face.length; j++){
    System.out.printf("%dt%dn", j+1, face[j]);
    }
    }
    }

  19. Harshil G. Avatar

    2nd times the charm!

  20. Ivan Limari Avatar

    wtf happened in this tutorial; tutorials like this are a reason why so much people quit…

  21. sean123654 Avatar

    I get the +1 cuz it starts a zero lol

    but im still figuring out how he made the freq tally up with the number shown on the dice…..gonna watch again

  22. Jimmy Harry Avatar

    This is fu*king art!

  23. nat cope Avatar

    how do add more die?

  24. Tom Zhanchi Avatar

    for ease of use, in eclipse, you can just start using random, you will get errors, but typing Ctrl+shift+o will automatically insert the appropriate import statement at the top. also, instead of typing System.out.println, you can type Sysout and then type Ctrl+space and eclipse will type the rest for you

  25. nvc Avatar

    This was unnecessarily confusing. There's no reason why the frequency of the dice face with one dot on it must be placed at index 1 and so on. The design of the dice has nothing to do with anything at all.

    There are six faces, so you need an array with six values. Create that.

    int freq[] = new int[6]

    Roll the dice 1,000 times to determine the frequencies for each face.

    for (int roll = 1; roll <= 1000; roll++){
    freq[rand.nextInt(6)]++;
    }

    If you are using traditional dice, then the faces have dots on them, numbering from 1 to 6. If the elements of the freq array (indexed from 0 to 5) correspond to the faces in ascending order of the number of dots, the code to display them properly will look like this:

    System.out.println("FacetFrequency");

    for (int face = 1; face <= freq.length; face++){
    System.out.println(face + "t" + freq[face-1]);
    }

    You may be playing with, say, children's dice where the faces have different colours instead of different numbers of dots. The part where you determine the frequencies will still be exactly the same. You would create an additional array to store the colours of the faces, e.g.

    String face[] = {"red", "blue", "green", "yellow", "orange", "purple"};

    And the code to display the result would look like this:

    for (int i = 0; i < freq.length; i++){
    System.out.println(face[i] + "t" + freq[i]);
    }

  26. M. Dieng Avatar

    Set roll<=10000000 (ten million) and you'll see that each number shows up about 1.66 million times.

  27. Matty Z Avatar

    This outputs the same result and may be less confusing to understand. To get the values in the index to 1-6 you simply add 1 to the face in the System.out.println code. Also to get 1000 rolls, you can either change the roll<1000 to roll<=1000 or change the initial roll value to 0 and leave the second parameter of the for loop as roll <1000. Hope this helps.

    import java.util.Random;

    public class Mortgage {
    public static void main(String[] args){
    Random ran = new Random();
    int freq[]=new int [6];

    for(int roll=0;roll<1000;roll++){
    ++freq[ran.nextInt(6)];
    }
    System.out.println("FacetFrequency");

    for(int face=0;face<freq.length;face++){
    System.out.println((1+face)+"t"+freq[face]);

    }
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *