Your best bet is a for
loop.
This will allow you to perform an action (I.E subtraction) on each ‘element’ of an array.
By using a return
statement, you can return the new array that has had each element modified
EDIT As per @AlexeiLevenkov’s comment, I have updated my answer to keep a count of the remaining subtraction.
Using this to test :
using System;
public class Program
{
public static void Main()
{
int[] array = new int[]{5,10,15,20,25,30,35};
array=SubtractArray(array,25);
Console.WriteLine("Output is:");
foreach(int v in array){
Console.WriteLine(v+", ");
}
}
public static int[] SubtractArray(int[] array , int subtraction){
for(int i=0; i< array.Length;i++){
if(subtraction>0){
int newValue=array[i]-subtraction;
if(newValue<1){
newValue=0;
subtraction=subtraction-array[i];
}
array[i]=newValue;
}
}
return array;
}
}
5
solved I need to subtract value from the array [closed]