Returning an array from a function is not a problem, but you have made some other mistakes. First, you are declaring p in a place where it would only be in scope for the duration of the for loop. No problem, the easiest fix is to move the declaration of p. But the next issue is that you are doing weird things with count. As you have it written it will always cause an ArrayOverflow exception. I don't really understand the goal you have in mind, allocating and using the array this way. From the loop we can infer that p will not need more elements than y. Generally I prefer not to allocate the way you tried, anyway. Adding one element at a time is slow. The way you did it only compounds the problems. It loses the original array and none of the old elements are kept.
So this is what I came up with:
Code:
using System.IO;
using System;
class Program
{
static int Sum(int[] a)
{
int sum = 0;
for (int i = 0; i < a.Length; i++)
{
sum += a[i];
}
return sum;
}
public static int[] Prop(int x, params int[] y)
{
int[] p = new int[y.Length];
if (y.Length > 1)
for (int i = 0; i < y.Length; i++)
{
int f = x * y[i] / Sum(y);
p[i] = f;
}
return p;
}
static void Main()
{
int[] res = Prop(2016, 5, 7, 9);
for (int i = 0; i < res.Length; i++) {
Console.WriteLine("res[{0,2:D}] = {1:D}", i, res[i]);
}
}
}
I didn't really check the math... hope this helps.