loops - Computing the frequency of 5 in an array -
very basic query here beginner... i'm looking find frequency of number within array... in (mangled) code below have tried calculate occurances of number 5 in array i'm running problem in formulating loop
heres code attempt:
//compute frequency of 5 in array named numbers
public class find //begin class {
public static void main (string []args) //begin main { double numbers[] = {6,7,12,5,4,2,4,6,9,5,7,11,1,23,32,45,5}; //initialise , populate array int total = 0; int counter = 0; (int x : numbers) { if (numbers[] == 5; counter ++) {system.out.println( numbers[i] + " "); }
}
// end code // *****************
int numbers[] = {6,7,12,5,4,2,4,6,9,5,7,11,1,23,32,45,5}; for(int x : numbers) { if(x == 5) counter++; } system.out.println(counter);
i can see trying use for each loop in implementation.
@code whisperer provides alternative that, if want use for each loop have make sure loop type , array type match. in case, array type double loop type int. within each iteration, you're selecting individual value in array, don't need include brackets.
Comments
Post a Comment