Subtracting each element from an array
Hi,
Im looking to find a piece of code which will take an array of ints and then subtract 1 away from each element that array to keep doing this for a certain amount of times. I've managed to make a method which takes away 1 from the array as shown below
Code :
public static void subtraction(int[] num){
System.out.println("contents of array after subtracting 1 from each element is ");
for(int i = 0; i < num.length; i++){
System.out.println((num[i] - 1));
However I can only get it to subract once, so its obvious to me that I need some form of loop to cycle through and keep subtracting. However every effort Ive made the program will only ouput that first subtraction.
Re: Subtracting each element from an array
Quote:
I can only get it to subract once
Your code shows the contents of the array element minus 1. It does not change the contents of the array.
You need to use an assignment statement if you want to change the element's value.