c# - How to create a method that takes an integer array of any length as its only parameter, and returns a new integer array that has a length of only 2 -
can here me ? need method takes integer array of length parameter, , returns new integer array has length of 2.
this have far:
public static int[] gethilo(int[] values) { int [] values = int{1,2,3,4}; }
without more information, best can get:
public int[] dosomething(int[] inputarray) { int[] retval = new int[2]; //do input array return retval; }
it takes length input array , returns 2 item array. can index through like:
for (int = 0; < inputarray.length; i++) { int = inputarray[i]; }
or
foreach (int value in inputarray) { int = value; }
edit
to actual task, can like:
public int[] gethighlow(int[] inputarray) { int[] retval = new int[2]; int highest = inputarray[0]; int lowest = inputarray[0]; (int = 1; < inputarray.length; i++) { if (inputarray[i] > highest) highest = inputarray[i]; if (inputarray[i] < lowest) lowest = inputarray[i]; } retval[0] = highest; retval[1] = lowest; return retval; }
please take time understand how function works. compares value against items in array see 1 highest or lowest comparing them saved values.