The task: to write an algorithm that allows generating two new arrays from an array of numbers - the first one contains only negative and the second one only positive.

Question: It seems everything wrote everything works, that's just how to get rid of the empty values ​​of the array. I know that you can add the size of an array when adding a number to this array, but I do not know how to write it.

Aogitrm:

class Program { static void Main(string[] args) { Random ran = new Random(); int[] main_mas = new int[6]; // Создаем главный массив for (int i = 0; i < main_mas.Length; i++) // Вводим переменные main_mas[i] = ran.Next(minValue: -20, maxValue: 20); int[] mas1 = new int[6]; // Создаем отриц. массив int[] mas2 = new int[6]; // Создаем плож. массив for (int i = 0; i < main_mas.Length; i++) { if (main_mas[i] < 0) mas1[i] = main_mas[i]; else mas2[i] = main_mas[i]; } Console.WriteLine("Исход: \n"); for (int i = 0; i < main_mas.Length; i++) Console.WriteLine(main_mas[i]); Console.WriteLine("Отриц: \n"); for (int i = 0; i < mas1.Length; i++) Console.WriteLine(mas1[i]); Console.WriteLine("полож: \n"); for (int i = 0; i < mas2.Length; i++) Console.WriteLine(mas2[i]); Console.ReadKey(); } } 

Run algorithm: enter image description here

    2 answers 2

    Instead of your code

     for (int i = 0; i < main_mas.Length; i++) { if (main_mas[i] < 0) mas1[i] = main_mas[i]; else mas2[i] = main_mas[i]; } 

    do this:

     int k=0, l=0; for (int i = 0; i < main_mas.Length; i++) { if (main_mas[i] < 0) mas1[k++] = main_mas[i]; else mas2[l++] = main_mas[i]; } 

    After exiting the cycle, k and l contain the number of negative and positive elements.

    I hope you understand your mistake. If not, ask.

    • It seems to me that your answer is no good. It is necessary to create 2 new arrays without zero values ​​in them. And you count the number put. and negative and ... what? What is the use of it? Or did you decide to leave the possibility to the author of the question to decide further what to do with this? - Bulson 4:03 pm
    • @Bulson Code not only counts, but also fills in the beginning of new arrays without spaces. Here the ends will remain empty, if the length with the stock was (it was not counted in advance) - MBo
    • @MBo is understandable, I mean, the result is still not the one that is needed. - Bulson
    • @Bulson naturally I don’t want to “produce a freebie” and I need the author of the question to learn how to program, especially judging by the text of the question, this is a normal student who just needs a little hint, and then he will understand what to do. - Alexander Muksimov
    • @Alexander Muksimov , I realized, then + - Bulson 4:38 pm

    you can use the List for your task
    Here is the altered code:

     var ran = new Random(); var mainArr = new int[6]; for (var i = 0; i < mainArr.Length; i++) mainArr[i] = ran.Next(minValue: -20, maxValue: 20); var mas1 = new List<int>(6); var mas2 = new List<int>(6); foreach (var t in mainArr) { if (t < 0) mas1.Add(t); else mas2.Add(t); } Console.WriteLine("Исход: \n"); foreach (var t in mainArr) Console.WriteLine(t); Console.WriteLine("Отриц: \n"); foreach (var t in mas1) Console.WriteLine(t); Console.WriteLine("полож: \n"); foreach (var t in mas2) Console.WriteLine(t); Console.ReadKey(); 

    the List implementation has an array in it (I have specified the initial length)
    it will be copied to a larger array if there is not enough space for the elements.
    if you deal with large arrays, it is desirable to evaluate what they will be in order not to occupy too much space

    • With each sheet, sorry, a fool can. And you using only arrays write. :) - Bulson 4:16 pm
    • @Bulson Alexander has already written such an option - dgzargo