Finding specific numbers in an array?

kevin

I'm fairly new to Java, and have been working on arrays. The task was :

  • Print an array of random numbers
  • Print the average of said numbers
  • Count the amount of numbers within 50 units less/more than the average in the array
  • Allow user to enter a number and search the array to see if it's in there

My code so far: (keep in mind, it's pretty rough, I got kinda lost towards the end.) All I'm asking is for someone to explain/help with how to find the 50+- average, and searching for user input in the array

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int[] nums = new int[100];

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

            nums[i] =(int) (Math.random()*1000);    
        }
        for (int i = 0; i < nums.length; i++){

            if (i % 20 == 0){
                System.out.println("\n");
            }
                System.out.print(nums[i] + " ");        
        }

        int sum = 0;
        for(int i = 0; i< nums.length; i++){
            sum += nums[i];
        }

        int average;
        average = sum/(nums.length);
        System.out.println("\n\nThe average equals: "+ average);

        int fifty = 0;
        for(int i = 0; i < nums.length; i++){   
            if(nums[i]  <= average - 50 && nums[i] >= average + 50){
                fifty++;
            }
        }
        System.out.print("Number of values within 50 of the average: " + fifty);

        int usrVal;

        System.out.println("\nEnter a value to test: ");
        usrVal = in.nextInt(); 

    }       
}       
Scary Wombat

so as you know what your average is

then loop through your array and compare each number to the average

  if (Math.abs (nums[i] - average) < 50) {
      System.out.println (nums[i]);
      // or count it or whatever
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

finding the sum of numbers in an array

From Dev

Finding specific values in an array

From Dev

Finding the primary numbers in an array of pointer

From Dev

finding sum of numbers in a char array

From Dev

sorting the array and finding repeated numbers

From Dev

finding specific indices with pointer array

From Dev

Finding a specific string in array of strings

From Dev

Finding all combinations of numbers that equal a specific number

From Dev

Fill array with specific numbers

From Dev

finding a specific array element and remove the container array

From Dev

Finding a duplicate numbers in an array and then counting the number of duplicates

From Dev

Recursion for finding the smallest path of numbers in an array

From Dev

Finding the Largest increase in an array filled with random numbers

From Dev

Finding difference between consecutive numbers in an array in javascript

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding the minimum sum within an array of numbers

From Dev

Finding missing numbers in a distributed array system

From Dev

Finding the minimum value of int numbers in an array (Java)

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding the minimum sum within an array of numbers

From Dev

finding how many consecutive numbers in an array [java]

From Dev

Finding the 3 largest numbers in an array/list of n numbers without the sorting

From Dev

finding a specific index on a two dimensional array

From Dev

Finding range index of a sorted array on a specific element

From Dev

Finding the sum of specific multidimensional array php

From Dev

Finding indexes of a specific repeated number in array

From Dev

select specific numbers out of array

From Dev

Remove specific numbers from array

From Dev

Python finding numbers from a list that satisfy a specific condition

Related Related

  1. 1

    finding the sum of numbers in an array

  2. 2

    Finding specific values in an array

  3. 3

    Finding the primary numbers in an array of pointer

  4. 4

    finding sum of numbers in a char array

  5. 5

    sorting the array and finding repeated numbers

  6. 6

    finding specific indices with pointer array

  7. 7

    Finding a specific string in array of strings

  8. 8

    Finding all combinations of numbers that equal a specific number

  9. 9

    Fill array with specific numbers

  10. 10

    finding a specific array element and remove the container array

  11. 11

    Finding a duplicate numbers in an array and then counting the number of duplicates

  12. 12

    Recursion for finding the smallest path of numbers in an array

  13. 13

    Finding the Largest increase in an array filled with random numbers

  14. 14

    Finding difference between consecutive numbers in an array in javascript

  15. 15

    Java - Finding Largest and Smallest Numbers using an Array

  16. 16

    Finding the minimum sum within an array of numbers

  17. 17

    Finding missing numbers in a distributed array system

  18. 18

    Finding the minimum value of int numbers in an array (Java)

  19. 19

    Java - Finding Largest and Smallest Numbers using an Array

  20. 20

    Finding the minimum sum within an array of numbers

  21. 21

    finding how many consecutive numbers in an array [java]

  22. 22

    Finding the 3 largest numbers in an array/list of n numbers without the sorting

  23. 23

    finding a specific index on a two dimensional array

  24. 24

    Finding range index of a sorted array on a specific element

  25. 25

    Finding the sum of specific multidimensional array php

  26. 26

    Finding indexes of a specific repeated number in array

  27. 27

    select specific numbers out of array

  28. 28

    Remove specific numbers from array

  29. 29

    Python finding numbers from a list that satisfy a specific condition

HotTag

Archive