I am quite new C# and what I am trying to do is fill empty array with values from other array by for cycle. If value from first array meets requirements for second array, then it is filled in.
int[] cisla = new int[] {a, b, c, d, e, f}; //first array
int[] nadprumer = new int[] {}; //second array
decimal prumer = (a b c d e f) / 6;
for (int i = 0; i< cisla.Length; i )
{
if (cisla[i] > prumer)
{
//join value into "nadprumer" array
}
}
CodePudding user response:
A List type would be allot better like mentioned above - but here it goes. Maybe this will help?
static void Main(string[] args)
{
int[] cisla = new int[] { 1, 2, 3, 4, 5, 6 }; //first array type int cannot be strings or characters
int[] nadprumer = new int[6] ; //second array needs initialized
decimal prumer = (cisla[0] cisla[1] cisla[2] cisla[3] cisla[4] cisla[5]) / 6;//why do you need this?
for (int i = 0; i < cisla.Length; i )
{
//if (cisla[i] > prumer)//why are you doing this?
//{
//join value into "nadprumer" array
nadprumer[i] = cisla[i];
Console.WriteLine(nadprumer[i]);
//}
}
Console.ReadLine();
}
CodePudding user response:
Your code:
int[] nadprumer = new int[] {};
will create an array of length 0.
What you need to do is:
int[] nadprumer = new int[cisla.Length];
and then inside your loop:
nadprumer[i] = cisla[i];
But this will leave the rest as 0. If you don't want that, create a List<int>
as the target, and then have
theList.Add(cisla[i]);
in the loop, and if you need the result to be an array, change it to an array in the end like this:
theList.ToArray();