Babz
Babz

Reputation: 75

java how do i read a negative int as a positive one

The question at hand is:

The method should compute for each row the sum of the absolute values of the elements of that row. The method should return the maximum of these sums. For example, if applied to the test array, the method should return the value max (3+1+4+0,5+9+2+6, 5+3+7+8) = max (8,22,23) = 23.

The test array:

3 -1  4  0
5  9 -2  6 
5  3  7 -8

so far i have made a method that gets the value of all the rows and returns to me the sum in a list; however, i want it to turn negative integers into positive ones. here's my code so far:

public static int[] rowSums(int[][]array) {
int[] a = new int[array.length];
    for (int i = 0;i<array.length;i++){
        int sum = IntStream.of(array[i]).sum();
        a[i]=sum;


} return a;}

Output:

[6,18,7]

Upvotes: 4

Views: 3252

Answers (6)

Blank Byte
Blank Byte

Reputation: 1

Change all numbers to positive numbers. This can be done using Math.abs(int).Check the link.

Upvotes: 0

djm.im
djm.im

Reputation: 3323

Update this line
int sum = IntStream.of(array[i]).sum();
to
int sum = IntStream.of(array[i]).map(Math::abs).sum();

And solution for whole matrix (this snippet will return 23):

Optional<Integer> maxVal = Arrays.stream(matrix)
    .map(array -> Arrays.stream(array).map(Math::abs).sum())
    .reduce(Integer::max);

And without Optional

int maxVal = Arrays.stream(matrix)
    .map(array -> Arrays.stream(array).map(Math::abs).sum())
    .reduce(Integer::max).get();

Upvotes: 1

Jacob G.
Jacob G.

Reputation: 29700

You can use Math#abs(standing for absolute value) which converts any negative number into a positive number:

int sum = IntStream.of(array[i]).map(Math::abs).sum();

If you'd like to lose those for-loops, you can even just stick with streams:

public static int[] rowSums(int[][] array) {
    return Arrays.stream(array)
                 .mapToInt(i -> Arrays.stream(i).map(Math::abs).sum())
                 .toArray();
}

Upvotes: 3

Naresh Bharadwaj
Naresh Bharadwaj

Reputation: 302

Here you can use the Map. So The code will be like this.

public static int[] rowSums(int[][]array) {
int[] a = new int[array.length];
    for (int i = 0;i<array.length;i++){
        int sum = IntStream.of(array[i]).map(n->{
            if(n<0) {
                return n*-1;
            }
            else {
                return n;
            }
        }).sum();
        a[i]=sum;


} return a;}

Here I just make all the negative Integers into Positive.

Upvotes: 2

Just use:

Math.abs(var)

This will turn var to a positive if it is negative.

Therefore the full answer is as follows:

public static int[] rowSums(int[][]array) {
int[] a = new int[array.length];
    for (int i = 0;i<array.length;i++){
        int sum = IntStream.of(array[i]).map(Math::abs).sum();
        a[i]=sum;


} return a;}

Upvotes: 0

MrB
MrB

Reputation: 818

Use Math.abs() It'll take any number and return it as a positive number. You need it on this line:

int sum = IntStream.of(array[i]).sum();

to take array[i] and make it absolute. So

IntStream.of(Math.abs(array[i]).sum();

Upvotes: 0

Related Questions