Вопрос задан 18.06.2023 в 11:47. Предмет Информатика. Спрашивает Жарнова Диана.

Нужна помощь с задачей! Буду рад за помощь! Задачу надо решить с С++. 12.141. Дан двумерный

массив. Составить программу: а) которая определяет, является ли последовательность элементов некоторой строки массива упорядоченной по неубыванию; б) которая определяет, является ли последовательность элементов некоторого столбца массива упорядоченной по невозрастанию. В случае отрицательного ответа в обеих задачах должны быть напечатаны координаты первого элемента, нарушающего указанную упорядоченность.
0 0
Перейти к ответам

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

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

Код:

#include <iostream>

#include <string>

using namespace std;

void printArray(int** arr, size_t X, size_t Y) {

   for (size_t i = 0; i < X; ++i) {

       for (size_t j = 0; j < Y; ++j) {

           cout << arr[i][j] << " ";

       }

       cout << endl;

   }

}

int main() {

   size_t X, Y;

   cout << "Number of rows in the array: ";

   cin >> X;

   cout << "Elements in each row of the array: ";

   cin >> Y;

   int** arr = new int* [X];

   for (size_t i = 0; i < X; ++i) {

       arr[i] = new int[Y];

       cout << "#" << i + 1 << ": ";

       for (size_t j = 0; j < Y; ++j)

           cin >> arr[i][j];

   }

   size_t index;

   cout << "index to check the row for non-decreasing ordering: ";

   cin >> index;

   --index; // numbering from 1

   bool flag = 1;

   for (int i = 0; i < Y - 1; ++i) {

       if (!(arr[index][i] <= arr[index][i + 1])) {

           cout << "No, " << i + 1 << (i + 1 == 1 ? "st" : (i + 1 == 2 ? "nd" : (i + 1 == 3) ? "rd" : "th")) << " element (" << arr[index][i] << ") violates the non-decreasing ordering (" << arr[index][i] << " > " << arr[index][i + 1] << ").\n";

           flag = 0;

           break;

       }

   }

   if (flag)

       cout << "Yes, the specified row is ordered in non-decreasing order.\n";

   flag = 1;

   index = 1;

   cout << "index to check the column for non-increasing ordering: ";

   cin >> index;

   --index;

   flag = 1;

   for (int i = 0; i < X - 1; ++i) {

       if (!(arr[i][index] >= arr[i + 1][index])) {

           cout << "No, " << i + 1 << (i + 1 == 1 ? "st" : (i + 1 == 2 ? "nd" : (i + 1 == 3) ? "rd" : "th")) << " element (" << arr[i][index] << ") violates the non-increasing ordering (" << arr[i][index] << " < " << arr[i][index + 1] << ").\n";

           flag = 0;

           break;

       }

   }

   if (flag)

       cout << "Yes, the specified row is ordered in non-increasing order.\n";

}




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

To solve the given problem, you need to write a program in C++ that determines whether a sequence of elements in a two-dimensional array is ordered in non-decreasing order for rows and non-increasing order for columns. If the sequence is not ordered, the program should print the coordinates of the first element that violates the specified order.

Here's an example of how you can approach this problem:

Step 1: Define the problem

The problem requires us to determine whether a sequence of elements in a two-dimensional array is ordered in non-decreasing order for rows and non-increasing order for columns. If the order is violated, we need to print the coordinates of the first element that violates the order.

Step 2: Input the array

First, you need to input the two-dimensional array. You can do this by using nested loops to iterate over the rows and columns of the array and inputting the elements.

Step 3: Check row-wise order

To check if the elements in each row are ordered in non-decreasing order, you can iterate over each row and compare each element with the next element in the row. If you find an element that is greater than the next element, you can print its coordinates and break out of the loop.

Step 4: Check column-wise order

To check if the elements in each column are ordered in non-increasing order, you can iterate over each column and compare each element with the next element in the column. If you find an element that is less than the next element, you can print its coordinates and break out of the loop.

Step 5: Print the result

If the program does not find any violations of the specified order in both the rows and columns, you can print a message indicating that the sequence is ordered. Otherwise, you can print the coordinates of the first element that violates the order.

Here's a sample code snippet that demonstrates the approach described above:

```cpp #include

int main() { const int rows = 3; const int cols = 4; int arr[rows][cols] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

bool rowOrdered = true; bool colOrdered = true; int rowViolationRow = -1; int rowViolationCol = -1; int colViolationRow = -1; int colViolationCol = -1;

// Check row-wise order for (int i = 0; i < rows; i++) { for (int j = 0; j < cols - 1; j++) { if (arr[i][j] > arr[i][j + 1]) { rowOrdered = false; rowViolationRow = i; rowViolationCol = j; break; } } }

// Check column-wise order for (int j = 0; j < cols; j++) { for (int i = 0; i < rows - 1; i++) { if (arr[i][j] < arr[i + 1][j]) { colOrdered = false; colViolationRow = i; colViolationCol = j; break; } } }

// Print the result if (rowOrdered && colOrdered) { std::cout << "The sequence is ordered." << std::endl; } else { if (!rowOrdered) { std::cout << "Row violation at coordinates (" << rowViolationRow << ", " << rowViolationCol << ")." << std::endl; } if (!colOrdered) { std::cout << "Column violation at coordinates (" << colViolationRow << ", " << colViolationCol << ")." << std::endl; } }

return 0; } ```

Please note that this is just a sample code snippet to demonstrate the approach. You may need to modify it based on your specific requirements and input format.

I hope this helps you get started with solving the problem! Let me know if you have any further questions.

0 0

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

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

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

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