Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i am tryin to make a function that takes an array and multiples each of its values by 2, then returns each multiplied value in a new array, so I can write out the individual values of the original array and the new multiplied one.
IE, an array with 2, 4, and 6, I want to make a function that takes it, then multiplies each value by 2, then return a new array that has 4, 6, and 8.

What I have tried:

i tried creating a foreach loop in a function that multiplies each array item by 2, but now I can't put the answers in a new array and return that.
Posted
Updated 6-Dec-17 20:32pm
Comments
Patrice T 7-Dec-17 1:05am    
And you have some code ? a question ?
George Swan 7-Dec-17 2:10am    
4*2=6??
6*2=8??

1 solution

class Program
   {
       static void Main(string[] args)
       {
           int[] inputArray = { 1, 2, 3, 4 };
           int[] outputArray = GetArrayMultiply(inputArray, 2);  // 2,4,6,8
       }

      static int[] GetArrayMultiply(int[] inputArray, int multiplier)
       {
           int[] outputArray = new int[inputArray.Length];
           for (int i = 0; i < inputArray.Length; i++)
           {
               outputArray[i] = inputArray[i] * multiplier;
           }
           return outputArray;
       }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900