Вопрос задан 15.06.2023 в 05:11. Предмет Информатика. Спрашивает Кирьянова Ангелина.

Дан массив размера N. Вывести второй положительный элемент массива и его номер. С#​

0 0
Перейти к ответам

Ответы на вопрос

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Kuzmenko Kirill.

using System;

namespace turgunovmanas413

{

   class Program

   {

       static int[] array = new int[100];

       static int n;

       static bool deleted;

       static void Input()

       {

           int i;

           Console.WriteLine("Количество элементов массива?");

           n = Convert.ToInt32(Console.ReadLine());

           Random r = new Random();

           for (i = 0; i < n; i++)

               array[i] = r.Next(-100, 101);

       }

       static void Output()

       {

           int i;

           for (i = 0; i < n; i++) Console.Write("{0,4}", array[i]);

               Console.WriteLine(); Console.WriteLine();

       }

       static void Del(int k)

       {

           for (int i = k; i < n - 1; i++)

           {

               array[i] = array[i + 1];

           }

           array[n] = 0;

           n = n - 1;

           deleted = true;

       }

       static void Solution()

       {

           int j = 0;

           for (int i = 0; i < array.Length; i++)

           {

               if (array[i] % 2 == 0)

                   j++;

               if (j == 2)

               {

                   Del(i);

                   break;

               }

           }

           if (!deleted)

               Console.WriteLine(" Элемент не найден");

       }

       static void Main()

       {

           deleted = false;

           Input();

           Output();

           Solution();

           Output();

           Console.ReadKey();

       }

   }

}

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Task: Find the Second Positive Element in an Array

To find the second positive element in an array and its index, you can use the following C# code:

```csharp using System;

class Program { static void Main() { int[] array = { 1, 7, 3, 4 }; int count = 0; int index = -1;

for (int i = 0; i < array.Length; i++) { if (array[i] > 0) { count++; if (count == 2) { index = i; break; } } }

if (index != -1) { Console.WriteLine("The second positive element in the array is " + array[index] + " at index " + index); } else { Console.WriteLine("There is no second positive element in the array"); } } } ```

This code initializes an array, iterates through it, and tracks the count of positive elements encountered. When the second positive element is found, it captures its index and breaks out of the loop. Finally, it prints the second positive element and its index.

Let me know if you need further assistance!

0 0

Похожие вопросы

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос