0

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
        }      
    }
4
  • 2
    Arrays are immutable, you need to use a collection, like List<int>. Also, a simpler way to do this is with Linq: nadprumer = cisla.Where(i => i > prumer).ToArray(). Commented Feb 23, 2022 at 17:58
  • ^ Exactly what I was going to say. Do that. Commented Feb 23, 2022 at 17:59
  • 1
    @Gusman Arrays are not immutable, you can replace items in the array. I think you meant array size cannot be changed. Commented Feb 23, 2022 at 18:10
  • @juharr Yes, wrong expression, I meant "non resizable". Commented Feb 23, 2022 at 18:14

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you are doing homework or some sort of excercise from the book you read. If they ask you to find all numbers that are greater than their mean then I would do it like that.

First variant (basic level with cycles):

int[] cisla = new int[] { 1, 2, 3, 4, 5, 6 }; //first array            
                       
double prumer = cisla.Average();

List<int> temp = new List<int>();
for (int i = 0; i < cisla.Length; i++)
{
    if (cisla[i] > prumer)
    {
        temp.Add(cisla[i]);
    }
}
int[] nadprumer = temp.ToArray(); //second array 

Second variant (advanced level with LINQ):

int[] cisla = new int[] { 1, 2, 3, 4, 5, 6 }; //first array            
double prumer = cisla.Average();
int[] nadprumer = cisla.Where(x => x > prumer).ToArray();

Comments

0

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();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.