Вопрос задан 06.12.2023 в 07:58. Предмет Информатика. Спрашивает Манойло Вадим.

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

этой строки.(язык программирования си++)
0 0
Перейти к ответам

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

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

Ответ:

#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;

}

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

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 using namespace std;

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!

0 0

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

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

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

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