Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <stdafx.h>
- #include "stdlib.h"
- #include <iostream>
- #include <iomanip>
- using namespace std;
- void arr_swap_columns (int **A, int N1, int N2) {
- int Temp;
- for (int k = 0; k < N2; k += 4)
- for (int i = 0; i < N1; i++) {
- if ((k + 2) < N2) {
- Temp = A[i][k];
- A[i][k] = A[i][k + 2];
- A[i][k + 2] = Temp;
- }
- if ((k + 3) < N2) {
- Temp = A[i][k + 1];
- A[i][k + 1] = A[i][k + 3];
- A[i][k + 3] = Temp;
- }
- }
- }
- void arr_swap_columns (float **A, int N1, int N2) {
- float Temp;
- for (int k = 0; k < N2; k += 4)
- for (int i = 0; i < N1; i++) {
- if ((k + 2) < N2) {
- Temp = A[i][k];
- A[i][k] = A[i][k + 2];
- A[i][k + 2] = Temp;
- }
- if ((k + 3) < N2) {
- Temp = A[i][k + 1];
- A[i][k + 1] = A[i][k + 3];
- A[i][k + 3] = Temp;
- }
- }
- }
- void arr_swap_columns (char **A, int N1, int N2) {
- char Temp;
- for (int k = 0; k < N2; k += 4)
- for (int i = 0; i < N1; i++) {
- if ((k + 2) < N2) {
- Temp = A[i][k];
- A[i][k] = A[i][k + 2];
- A[i][k + 2] = Temp;
- }
- if ((k + 3) < N2) {
- Temp = A[i][k + 1];
- A[i][k + 1] = A[i][k + 3];
- A[i][k + 3] = Temp;
- }
- }
- }
- void declare_2d_arr (int **&A, int &N1, int *N2, char ch = 'A', bool re_enter = false) {
- if ((N1 < 1) || re_enter) {
- do {
- system("cls");
- cout << endl << " int " << ch << "[";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> N1) || (N1 < 1));
- }
- if ((*N2 < 1) || re_enter) {
- do {
- system("cls");
- cout << endl << " int " << ch << "[" << N1 << "][";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> *N2) || (*N2 < 1));
- }
- A = new int *[N1];
- for (int i = 0; i < N1; i++)
- A[i] = new int [*N2];
- system("cls");
- cout << endl << " int " << ch << "[" << N1 << "][" << *N2 << "] was declared" << endl;
- for (int i = 0; i < N1; i++)
- for (int j = 0; j < *N2; j++)
- A[i][j] = 0;
- }
- void declare_2d_arr (float **&A, int &N1, int *N2, char ch = 'A', bool re_enter = false) {
- if ((N1 < 1) || re_enter) {
- do {
- system("cls");
- cout << endl << " float " << ch << "[";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> N1) || (N1 < 1));
- }
- if ((*N2 < 1) || re_enter) {
- do {
- system("cls");
- cout << endl << " float " << ch << "[" << N1 << "][";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> *N2) || (*N2 < 1));
- }
- A = new float *[N1];
- for (int i = 0; i < N1; i++)
- A[i] = new float [*N2];
- system("cls");
- cout << endl << " float " << ch << "[" << N1 << "][" << *N2 << "] was declared" << endl;
- for (int i = 0; i < N1; i++)
- for (int j = 0; j < *N2; j++)
- A[i][j] = 0.0;
- }
- void declare_2d_arr (char **&A, int &N1, int *N2, char ch = 'A', bool re_enter = false) {
- if ((N1 < 1) || re_enter) {
- do {
- system("cls");
- cout << endl << " char " << ch << "[";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> N1) || (N1 < 1));
- }
- if ((*N2 < 1) || re_enter) {
- do {
- system("cls");
- cout << endl << " char " << ch << "[" << N1 << "][";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> *N2) || (*N2 < 1));
- }
- A = new char *[N1];
- for (int i = 0; i < N1; i++)
- A[i] = new char [*N2];
- system("cls");
- cout << endl << " char " << ch << "[" << N1 << "][" << *N2 << "] was declared" << endl;
- for (int i = 0; i < N1; i++)
- for (int j = 0; j < *N2; j++)
- A[i][j] = '.';
- }
- void cout_2d_arr (int **A, int N1, int N2) {
- int max = A[0][0]; for (int i = 0; i < N1; i++) {for (int j = 0; j < N2; j++) if (A[i][j] > max) max = A[i][j];}
- int k = 1; while (max > 10) {max = max / 10; k++;}
- for (int i = 0; i < N1; i++) {
- cout << " ";
- for (int j = 0; j < N2; j++) {
- cout.width(k+1);
- cout << A[i][j] << " ";}
- cout << endl;}
- }
- void cout_2d_arr (float **A, int N1, int N2) {
- int max = (int)A[0][0]; for (int i = 0; i < N1; i++) {for (int j = 0; j < N2; j++) if ((int)A[i][j] > max) max = (int)A[i][j];}
- int k = 1; while (max > 10) {max = max / 10; k++;}
- for (int i = 0; i < N1; i++) {
- cout << " ";
- for (int j = 0; j < N2; j++) {
- cout << fixed << right;
- cout.precision(3);
- cout.width(k+5);
- cout << A[i][j] << " ";}
- cout << endl;}
- }
- void cout_2d_arr (char **A, int N1, int N2) {
- for (int i = 0; i < N1; i++) {
- cout << " ";
- for (int j = 0; j < N2; j++) {
- cout.width(2);
- cout << A[i][j] << " ";}
- cout << endl;}
- }
- void cin_2d_arr (int **A, int N1, int N2) {
- for (int i = 0; i < N1; i++)
- for (int j = 0; j < N2; j++)
- do {
- system("cls");
- cout << endl << " Input:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- cout << endl << " ";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> A[i][j]));
- system("cls");
- cout << endl << " Input:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- }
- void cin_2d_arr (float **A, int N1, int N2) {
- for (int i = 0; i < N1; i++)
- for (int j = 0; j < N2; j++)
- do {
- system("cls");
- cout << endl << " Input:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- cout << endl << " ";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> A[i][j]));
- system("cls");
- cout << endl << " Input:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- }
- void cin_2d_arr (char **A, int N1, int N2) {
- for (int i = 0; i < N1; i++)
- for (int j = 0; j < N2; j++)
- do {
- system("cls");
- cout << endl << " Input:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- cout << endl << " ";
- cin.clear();
- cin.sync();
- }
- while(!(cin >> A[i][j]));
- system("cls");
- cout << endl << " Input:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- }
- int main() {
- int N1 = 0, N2 = 0, Temp;
- int **A;
- declare_2d_arr(A, N1, &N2, 'A', false);
- cout << endl << " Enter the elements (one at time):" << endl << endl;
- system("pause");
- cin_2d_arr(A, N1, N2);
- arr_swap_columns(A, N1, N2);
- cout << endl << " Result:" << endl << endl;
- cout_2d_arr(A, N1, N2);
- for (int i = 0; i < N1; i++)
- delete [] A[i];
- delete [] A;
- cout << endl;
- system("pause");
- float **B;
- declare_2d_arr(B, N1, &N2, 'B', false);
- cout << endl << " Enter the elements (one at time):" << endl << endl;
- system("pause");
- cin_2d_arr(B, N1, N2);
- arr_swap_columns(B, N1, N2);
- cout << endl << " Result:" << endl << endl;
- cout_2d_arr(B, N1, N2);
- for (int i = 0; i < N1; i++)
- delete [] B[i];
- delete [] B;
- cout << endl;
- system("pause");
- char **C;
- declare_2d_arr(C, N1, &N2, 'C', true);
- cout << endl << " Enter the elements (one at time):" << endl << endl;
- system("pause");
- cin_2d_arr(C, N1, N2);
- arr_swap_columns(C, N1, N2);
- cout << endl << " Result:" << endl << endl;
- cout_2d_arr(C, N1, N2);
- for (int i = 0; i < N1; i++)
- delete [] C[i];
- delete [] C;
- cout << endl << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement