
Задан двумерный массив. Заменить первый элемент каждой строки на количество положительных элементов
этой строки.(язык программирования си++)

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

Ответ:
#include <iostream>
#include <cstdlib> // обьявление rand()
#include <ctime> // обьявление srand()
#define SIZE 5 // размер массива
int main()
{
using namespace std;
setlocale(LC_ALL, "ru");
srand(time(NULL));
int arr[SIZE][SIZE];
cout << "Начальный массив: \n";
for (unsigned i = 0u; i < SIZE; i++)
{
int positive = 0;
for (unsigned j = 0u; j < SIZE; j++)
{
arr[i][j] = rand() % 20 - 10;
cout << arr[i][j] << '\t';
if (arr[i][j] > 0)
positive++;
}
arr[i][0] = positive;
cout<<'\n';
}
cout << "\nМассив-результат: \n";
for (unsigned i = 0u; i < SIZE; i++)
{
for (unsigned j = 0u; j < SIZE; j++)
cout << arr[i][j] << '\t';
cout<<'\n';
}
return 0;
}



Task Overview
You need to modify a two-dimensional array by replacing the first element of each row with the count of positive elements in that row. This task is to be performed in the C++ programming language.Approach
To achieve this, you can iterate through each row of the array, count the positive elements, and then replace the first element with the count.Here's a sample code snippet to accomplish this:
```cpp
#include
void replaceFirstElement(int arr[][3], int rows) { for (int i = 0; i < rows; i++) { int count = 0; for (int j = 0; j < 3; j++) { if (arr[i][j] > 0) { count++; } } arr[i][0] = count; } }
int main() { int arr[2][3] = {{1, -2, 3}, {-4, 5, -6}}; replaceFirstElement(arr, 2);
// Print the modified array for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << arr[i][j] << " "; } cout << endl; }
return 0; } ```
In this example, the `replaceFirstElement` function takes the 2D array and the number of rows as input. It then iterates through each row, counts the positive elements, and replaces the first element with the count. The `main` function demonstrates how to use this function with a sample 2x3 array and prints the modified array.
This approach ensures that the first element of each row is replaced with the count of positive elements in that row.
Feel free to modify the array dimensions and values as per your specific requirements.
Let me know if you need further assistance!


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







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