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


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

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



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!


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







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili