HydratedDeer
HydratedDeer

Reputation: 43

Summing Numbers in an Array Greater than an Inputted Value

Need to add all numbers in an array that is greater than an inputted number. The seed is just so the output can be replicated.

Example:

[12,16,45,3,32]

Inputted Value: 30

Output:

77

import java.util.*;

    public class SumAbove {
        public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
            int seed = scnr.nextInt();
            Random rand = new Random(seed);


            System.out.println("Enter a positive integer between 1-100 to search above:");
            int minVal = scnr.nextInt();

            int[] arr = new int[rand.nextInt(100)+1];
            for (int i=0; i<arr.length; i++) {
                arr[i] = rand.nextInt(100)+1;
            }
            System.out.println(Arrays.toString(arr));


             int sum = 0;
             for (int i=0; i<arr.length; i++) {
             if (arr[i]>minVal) {
                    sum += i;
                }
             }
            System.out.println(sum);
        }
    }

Upvotes: 0

Views: 411

Answers (4)

WJS
WJS

Reputation: 40062

Here is how you can do it with streams in Java 8+.

      int nValues = 5;
      int minValue = 1;
      int maxValue = 30;
      Random r = new Random();
      for (int i = 0; i < 10; i++) {
         int[] values = r.ints(nValues, minValue, maxValue + 1).toArray();

         // min to sum is the threshold
         int minToSum = r.nextInt(7) + 10; // between 10 an 16 inclusive


         int sum = Arrays.stream(values).filter(m -> m > minToSum).sum();

         System.out.println("sum = " + sum + " for greater than " + minToSum
               + " : " + Arrays.toString(values));
      }

The following output.

sum = 65 for values greater than 11 : [2, 10, 14, 23, 28]
sum = 92 for values greater than 10 : [13, 18, 15, 19, 27]
sum = 94 for values greater than 12 : [25, 6, 14, 25, 30]
sum = 54 for values greater than 10 : [14, 8, 14, 26, 5]
sum = 22 for values greater than 15 : [15, 8, 13, 22, 14]
sum = 28 for values greater than 13 : [3, 28, 9, 6, 5]
sum = 87 for values greater than 13 : [5, 18, 25, 21, 23]
sum = 31 for values greater than 13 : [16, 7, 12, 2, 15]
sum = 42 for values greater than 15 : [7, 22, 20, 10, 5]
sum = 40 for values greater than 12 : [2, 2, 13, 27, 9]


Upvotes: 0

Pavan Gudur
Pavan Gudur

Reputation: 37

Replace sum += i with sum += arr[i].

The variable i is just the position. arr[i] is the value at that position.

Upvotes: 1

oguzhancaner
oguzhancaner

Reputation: 1

public static void main(String[] args) {

    int nums[] = { 12, 16, 45, 3, 32 };
    int value;
    int sum = 0;

    System.out.println("Enter a positive integer between 1-100 to search above: ");
    Scanner sc = new Scanner(System.in);
    value = sc.nextInt();

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] > value)
            sum = nums[i] + sum;
    }
    System.out.println(sum);
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201487

Instead of sum += i; you want sum += arr[i]; (as already noted), you also only need one loop (since you know the minimum before the first loop). Like,

int minVal = scnr.nextInt(), sum = 0;
int[] arr = new int[rand.nextInt(100) + 1];
for (int i = 0; i < arr.length; i++) {
    arr[i] = rand.nextInt(100) + 1;
    if (arr[i] > minVal) {
        sum += arr[i];
    }
}
System.out.println(Arrays.toString(arr));
System.out.println(sum);

Upvotes: 2

Related Questions