java - Using int[] arrays to manipulate each other through nested loops -


what trying use 1 method generate 30 random numbers 0-15 , method count how many times each number prints , put in second array. each position in second array corresponds number in array is. if = [0] , 0 shows 3 times, should 3 , on.

so far, have gotten this. seems happening counts number of times 15 shows (the last number of array). though wrong. don't see i'm doing wrong. there must wrong logic.

import java.util.arrays;  public class frequencyofnumbers {      public static void main(string[]args){         system.out.println(arrays.tostring(randomnums()));         system.out.println(arrays.tostring(sortednums(randomnums())));     }      public static int[] randomnums (){         int[] random = new int[30];         for(int i=0;i<random.length;i++){            double randnum = math.random() * 16;             random[i] = (int)randnum;         }         return random;     }      public static int[] sortednums(int[] sort){         int[] numvals = new int[15];         for(int i=0;i<numvals.length;i++)         {             for(int j=0;j<sort.length;j++)             {                 if(sort[j] == numvals[i])                 {                     numvals[i]++;                 }             }         }         return numvals;     } } 

example output i'm receiving:

[5, 15, 0, 5, 4, 10, 4, 11, 5, 13, 13, 8, 9, 9, 10, 6, 0, 9, 10, 12, 3, 7, 4, 9, 4, 11, 9, 15, 10, 7]  [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2] 

you have problems in code:

  1. don't call randomnums() twice. otherwise don't count right frequency. generate different arrays.

  2. if want keep numbers 0 15, should allocate 16 elements numvals.

  3. you don't need inner for loop when count appearances. consider numbers, take values, , increment number of appearances.

try this:

public static void main(string[] args) {     int[] randomnumbers = randomnums();     system.out.println(arrays.tostring(randomnumbers));     system.out.println(arrays.tostring(sortednums(randomnumbers))); }  public static int[] randomnums() {     int[] random = new int[30];     (int = 0; < random.length; i++) {         random[i] = (int) (math.random() * 16);     }     return random; }  public static int[] sortednums(int[] sort) {     int[] numvals = new int[16];     (int j = 0; j < sort.length; j++) {         numvals[sort[j]]++;     }     return numvals; } 

a possible output:

[8, 6, 5, 12, 12, 9, 15, 6, 7, 9, 15, 3, 6, 7, 3, 8, 6, 3, 15, 8, 12, 4, 7, 12, 2, 15, 6, 5, 4, 5] [0, 0, 1, 3, 2, 3, 5, 3, 3, 2, 0, 0, 4, 0, 0, 4] 

performance

if want count number of comparisons (practically) can:

  1. create new member in class:

    static int steps = 0; 
  2. define new method increments counter:

    private static boolean f() {     steps++;     return true; } 
  3. add f() && before current condition in if.

  4. add following last line in main method:

    system.out.println(steps); 

using 1 for generate 30 steps.

using 2 for loops (as wrote in comment) generate 480 steps (480 = 30 * 16).

in case irrelevant because both operations fast. larger input if first approach takes 1 second, should care if second 1 takes more 10 seconds.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -