Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Двумерный массив.doc
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
288.26 Кб
Скачать

Двумерный массив:

  1. Дана матрица целых чисел А[N][M]. Выполнить транспонирование матрицы относительно главной диагонали. Вывести на экран исходную и транспонированную матрицы.

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, int arr[][SIZE]){

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Vivod(int n, int m, int arr[][SIZE]){

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void main(){

int A [SIZE][SIZE];

int n,m;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

for (int i=0; i<n ;++i){

for (int j=0; j<i; j++){

int buf =A[i][j];

A[i][j] = A[j][i];

A[j][i] = buf;

}

}

cout << "\nMassiv A" << endl;

Vivod(n,m, a);

cout << endl;

}

  1. Отсортировать по убыванию i-ую строку матрицы действительных чисел X[M][N]. Число i ввести с клавиатуры. Исходную и преобразованную матрицы вывести на экран.

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, double arr[][SIZE])

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Vivod(int n, int m, double arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void main(){

double A [SIZE][SIZE];

int n,m;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

int J;

cout<<"Nomer stroki:";

cin>>J;

if(J > m && J < 0){

cout<<"Error "; system("pause"); return;

}

for (int K=1; K<m ;++K){

for (int i=0; i<n-K; i++){

if(A[i][J] < A[i+1][J]){

double buf =A[i][J];

A[i][J] = A[i+1][J];

A[i+1][J] = buf;

}

}

}

cout << "\nMassiv A" << endl;

Vivod(n,m, A);

cout << endl;

}

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Vivod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void main(){

int A [SIZE][SIZE];

int n,m;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

int I;

cout<<"Nomer stroki:";

cin>>I;

if(I > n && I < 0){

cout<<"Error "; system("pause"); return;

}

for (int K=1; K<m ;++K){

for (int j=0; j<m-K; j++){

if(A[I][j] > A[I][j+1]){

int buf =A[I][j];

A[I][j] = A[I][j+1];

A[I][j+1] = buf;

}

}

}

cout << "\nMassiv A" << endl;

Vivod(n,m, A);

cout << endl;

}

  1. Дана матрица вещественных чисел A[N][M]. Сформировать массив Y[K] из положительных элементов матрицы.

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Vivod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void Vivod(int n, int *arr)

{

for(int i = 0; i < n; ++i){

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

}

cout<<endl;

}

void main(){

int A [SIZE][SIZE], Y[SIZE];

int n,m, N=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

if( A[i][j] > 0){

Y[N++]=A[i][j];

}

}

cout<<endl;

}

cout << "\nMassiv Y" << endl;

Vivod(N, Y);

cout << endl;

}

  1. Дана матрица вещественных чисел A[M][N]. Сформировать массив Y[K] из отрицательных элементов матрицы, просматривая матрицу по строкам.

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Vivod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void Vivod(int n, int *arr)

{

for(int i = 0; i < n; ++i){

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

}

cout<<endl;

}

void main(){

int A [SIZE][SIZE], Y[SIZE];

int n,m, N=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

if( A[i][j] < 0){

Y[N++]=A[i][j];

}

}

cout<<endl;

}

cout << "\nMassiv Y" << endl;

Vivod(N, Y);

cout << endl;

}

  1. Дана матрица целых чисел A[N][M]. Сформировать массив Y[K] из отрицательных четных элементов матрицы, просматривая матрицу по столбцам.

#include <iostream>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Vivod(int n, int m, int arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void Vivod(int n, int *arr)

{

for(int i = 0; i < n; ++i){

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

}

cout<<endl;

}

void main(){

int A [SIZE][SIZE], Y[SIZE];

int n,m, N=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

for(int j = 0; j < m; ++j)

for(int i = 0; i < n; ++i){

{

if( A[i][j] %2 == 0 && A[i][j] < 0){

Y[N++]=A[i][j];

}

}

cout<<endl;

}

cout << "\nMassiv Y" << endl;

Vivod(N, Y);

cout << endl;

}

  1. Дана матрица вещественных чисел A[N][M]. Сформировать матрицу B[N][M], поместив на место первой строки строку с максимальным по модулю первым элементом.

#include <iostream>

#include <math.h>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, double arr[][SIZE]){

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

cout << "[" << i << "]["<< j <<"]: "; cin >> arr[i][j];

}

}

}

void Max(int n, int m, double arr[][SIZE], int& i_max, int & j_max){

for(int i = 0; i < n; ++i)

for(int j = 0; j < m; ++j){

if(fabs(arr[i][j]) > fabs(arr[i_max][j_max])){

i_max = i; j_max = j;

}

}

}

}

void Vivod(int n, int m, double arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void main(){

double A [SIZE][SIZE], B[SIZE][SIZE], max;

int n,m, i_max=0, j_max=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

Max( n, m, A, i_max, j_max);

max = A[i_max][j_max];

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

for(int j = 0; j < m; ++j) B[0][j] = max;

for(int i = 1; i < n; ++i)

for(int j = 0; j < m; ++j)B[i][j]=A[i][j];

cout << "\nMassiv B" << endl;

Vivod(n,m, B);

cout << endl;

}

  1. Дана матрица вещественных чисел A[N][M]. Сформировать матрицу B[N][M], поместив на место первой строки и первого столбца те, на пересечении которых находится максимальный по модулю элемент.

#include <iostream>

#include <math.h>

#define SIZE 100

using namespace std;

void Vvod(int n, int m, double arr[][SIZE])

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

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

cin >> arr[i][j];

}

}

}

void Max(int n, int m, double arr[][SIZE], int& i_max, int & j_max)

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j){

if(fabs(arr[i][j]) > fabs(arr[i_max][j_max]))

{

i_max = i; j_max = j;

}

}

}

}

void Vivod(int n, int m, double arr[][SIZE])

{

for(int i = 0; i < n; ++i){

for(int j = 0; j < m; ++j){

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

}

cout<<endl;

}

cout<<endl;

}

void main(){

double A [SIZE][SIZE], B[SIZE][SIZE], max;

int n,m, i_max=0, j_max=0;

cout << "ROW" << endl;

cin >> n;

cout << "COL" << endl;

cin >> m;

if((n > 100 && n <= 0) || (m > 100 && m <= 0 )){

cout<<"Error size"; system("pause"); return;

}

cout << "Massiv A" << endl;

Vvod(n,m, A);

Max( n, m, A, i_max, j_max);

max = A[i_max][j_max];

cout << "\nMassiv A" << endl;

Vivod(n, m, A);

for(int j = 0; j < m; ++j) B[0][j] = A[i_max][j];

for(int i = 0; i < n; ++i) B[i][0] = A[i][j_max];

for(int i = 1; i < n; ++i)

for(int j = 1; j < m; ++j)B[i][j]=A[i][j];

cout << "\nMassiv B" << endl;

Vivod(n,m, B);

cout << endl;

}

  1. Дана матрица целых чисел A[M][N]. Отсортировать элементы каждой строки матрицы в порядке возрастания. Полученную матрицу вывести на экран.

#include <iostream>

#include <math.h>

#define SIZE 100

using namespace std;