напиши код на языке c++ для решения этой задачи: Создать целочисленную матрицу А…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
напиши код на языке c++ для решения этой задачи: Создать целочисленную матрицу А размером MxN и заполнить ее однозначными случайными числами из диапазона от 0 до 9. Матрица должна иметь тип int[]. Значения M и N вводятся с клавиатуры. Исходную матрицу построчно выдать на экран. Поменять местами первую ( инд=0) строку массива и строку с минимальной суммой элементов. Переформированный массив построчно выдать на экран. Смену местами одной строки с другой оформить в виде метода.
- Assistant
#include #include #include
using namespace std;
void swap(int M, int N, int arr[M][N], int row1, int row2){ int temp; for(int i = 0; i < N; i++){ temp = arr[row1][i]; arr[row1][i] = arr[row2][i]; arr[row2][i] = temp; } }
int main(){ int M, N; cout<<"Enter the number of rows of the matrix: "; cin>>M; cout<<"Enter the number of columns of the matrix: "; cin>>N; int A[M][N]; srand(time(NULL)); for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ A[i][j] = rand() % 10; cout<<A[i][j]<<" "; } cout<<endl; } int minSum = INT_MAX, minRow; for(int i = 0; i < M; i++){ int sum = 0; for(int j = 0; j < N; j++){ sum += A[i][j]; } if(sum < minSum){ minSum = sum; minRow = i; } } swap(M, N, A, 0, minRow); cout<<"After swapping:"<<endl; for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ cout<<A[i][j]<<" "; } cout<<endl; } return 0; }
- User
исправь этот код,нетспользуя функцию arr
- Assistant
#include #include #include
using namespace std;
int main(){ int M, N; cout<<"Enter the number of rows of the matrix: "; cin>>M; cout<<"Enter the number of columns of the matrix: "; cin>>N; int A[M][N]; srand(time(NULL)); for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ A[i][j] = rand() % 10; cout<<A[i][j]<<" "; } cout<<endl; } int minSum = INT_MAX, minRow = 0; for(int i = 1; i < M; i++){ int sum = 0; for(int j = 0; j < N; j++){ sum += A[i][j]; } if(sum < minSum){ minSum = sum; minRow = i; } } int temp; for(int i = 0; i < N; i++){ temp = A[0][i]; A[0][i] = A[minRow][i]; A[minRow][i] = temp; } cout<<"After swapping:"<<endl; for(int i = 0; i < M; i++){ for(int j = 0; j < N; j++){ cout<<A[i][j]<<" "; } cout<<endl; } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.