#include #include <algorithm> using namespace std; void sorting(int** arr, int row, int…
- User
#include #include
using namespace std;
void sorting(int** arr, int row, int col) { for (int i = 0; i < row; ++i) { sort(arr[i], arr[i] + col); } }
void display_arr(int** arr, int row, int col) { cout << “LESSON 1: Sorted Arr:\n”; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { cout << arr[i][j] << “\t”; } cout << endl; } }
int sum_row(int** arr, int row, int col) { int rowSum = 0; for (int j = 0; j < col; ++j) { rowSum += arr[row][j]; } return rowSum; }
int sum_col(int** arr, int row, int col) { int colSum = 0; for (int i = 0; i < row; ++i) { colSum += arr[i][col]; } return colSum; }
int total_sum(int** arr, int row, int col) { int totalSum = 0; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { totalSum += arr[i][j]; } } return totalSum; }
void display_table(int** arr, int row, int col) { cout << “LESSON 2: CREATE TABLE:\n”; for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { cout << arr[i][j] << “\t”; }
cout << “|” << “\t” << sum_row(arr, i, col) << endl;
}
for (int i = 0; i < col + 7; i++) { cout << “----”; } cout << endl;
for (int i = 0; i < col; i++) { cout << sum_col(arr, row, i) << “\t”; }
cout << “|\t” << total_sum(arr, row, col); }
int main() { int row, col; cout << "Enter rows: "; cin >> row; cout << "Enter cols: "; cin >> col;
int** arr = new int*[row];
for (int i = 0; i < row; i++) { arr[i] = new int[col]; cout << "Enter elements for row " << i + 1 << “:\n”;
for (int j = 0; j < col; j++) { cin >> arr[i][j]; } }
bubble_sort(arr,row,col);
display_arr(arr, row, col);
display_table(arr, row, col);
for (int i = 0; i < row; i++) { delete[] arr[i]; } delete[] arr;
return 0; } напиши функцию bubble_sort(); для сортировки элементов в каждой строчке динамического двумерного массива с помощью сортировки пузырьком
- Assistant
void bubble_sort(int** arr, int row, int col) { for (int i = 0; i < row; ++i) { for (int j = 0; j < col - 1; ++j) { for (int k = 0; k < col - j - 1; ++k) { if (arr[i][k] > arr[i][k + 1]) { swap(arr[i][k], arr[i][k + 1]); } } } } }