108k views
5 votes
A function defined beginning with void SetNegativesToZeros(int userValues[], ... should modify userValues such that any negative integers are replaced by zeros. The function _____.

User Jenifer
by
4.4k points

1 Answer

3 votes

Answer:

public static void setNegativesToZero(int userValues[]){

System.out.println("Array before setting negatives to zero "+Arrays.toString(userValues));

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

if(userValues[i]< 1){

userValues[i] = 0;

}

}

System.out.println();

System.out.println("Array After setting negatives to zero "+Arrays.toString(userValues));

}

Step-by-step explanation:

Using Java programming Language, the method is created to receive an array of ints as parameter (as specified in the question)

Using a for loop, we iterate over the entire array checking for values that are negative (<0) and setting them to zero.

See a complete program below

import java.util.Arrays;

import java.util.Scanner;

public class num1 {

public static void main(String[] args) {

// Create an array and assign values

int [] arr = {1,2,3,-1,1,2,-4};

//Calling the method setNegativesToZero

setNegativesToZero(arr);

}

//Creating the Method

public static void setNegativesToZero(int userValues[]){

System.out.println("Array before setting negatives to zero "+Arrays.toString(userValues));

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

if(userValues[i]< 1){

userValues[i] = 0;

}

}

System.out.println();

System.out.println("Array After setting negatives to zero "+Arrays.toString(userValues));

}

}

User Gregory Boutte
by
3.7k points