Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 75 additions & 11 deletions lab5.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,79 @@
#include <iostream> #include <math.h> #include <conio.h> #define _USE_MATH_DEFINES
#define n 5 #define m 5
#include <iostream>
#include <math.h>
#include <conio.h>
#define _USE_MATH_DEFINES
#define N_size 5
#define M_size 5

using namespace std;
class Matrix { private: double M[m]; public: friend void InputArray(Matrix Arr[]); friend void OutputArray(Matrix Arr[]); friend void SortArray(Matrix Arr[]); friend void CalcArray(Matrix Arr[]); };
void InputArray(Matrix Arr[]) { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j)
{ cin >> Arr[i].M[j]; } } void OutputArray(Matrix Arr[]) { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cout << Arr[i].M[j] << "\t"; } cout << endl; } } void SortArray(Matrix arr[]) { for (int i = 0; i < n; i++) { for (int BlockSizeIterator = 1; BlockSizeIterator < n; BlockSizeIterator *= 2) { for (int BlockIterator = 0; BlockIterator < n - BlockSizeIterator; BlockIterator += 2 * BlockSizeIterator) { int LeftBlockIterator = 0; int RightBlockIterator = 0; int LeftBorder = BlockIterator; int MidBorder = BlockIterator + BlockSizeIterator; int RightBorder = BlockIterator + 2 * BlockSizeIterator; RightBorder = (RightBorder < n) ? RightBorder : n; int* SortedBlock = new int[RightBorder - LeftBorder];
while (LeftBorder + LeftBlockIterator < MidBorder && MidBorder + RightBlockIterator < RightBorder) { if (arr[LeftBorder + LeftBlockIterator].M[i] < arr[MidBorder + RightBlockIterator].M[i]) { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[LeftBorder + LeftBlockIterator].M[i]; LeftBlockIterator++; } else { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[MidBorder + RightBlockIterator].M[i]; RightBlockIterator++; } } while (LeftBorder + LeftBlockIterator < MidBorder) { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[LeftBorder + LeftBlockIterator].M[i]; LeftBlockIterator++; } while (MidBorder + RightBlockIterator < RightBorder) { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[MidBorder + RightBlockIterator].M[i]; RightBlockIterator++; }
for (int MergeIterator = 0; MergeIterator < LeftBlockIterator + RightBlockIterator; MergeIterator++) { arr[LeftBorder + MergeIterator].M[i] = SortedBlock[MergeIterator]; } delete SortedBlock;

class Matrix {
private:
double M[M_size];
public:
friend void InputArray(Matrix Arr[]);
friend void OutputArray(Matrix Arr[]);
friend void SortArray(Matrix Arr[]);
friend void CalcArray(Matrix Arr[]);
};
void InputArray(Matrix Arr[]) {
for (int column = 0; column < N_size; ++column)
for (int row = 0; row < M_size; ++row)
{ cin >> Arr[column].M[row];
}
}
} } } void CalcArray(Matrix Arr[]) { double DobytokNadDopDia[n] = {1, 1, 1, 1, 1}; for(int i = 0; i < n; i++) { for(int j = 0; j < n-i-1; j++) { DobytokNadDopDia[i]*=(Arr[i].M[j]); }
} double haunt; for (int i = 0; i < n; i++) { haunt += DobytokNadDopDia[i]; std::cout << "f(" << i << "): " << DobytokNadDopDia[i] << std::endl; }
cout << "F(f(ij)): " << haunt << endl;
void OutputArray(Matrix Arr[]) {
for (int column = 0; column < N_size; ++column) {
for (int row = 0; row < M_size; ++row) {
cout << Arr[column].M[row] << "\t";
} cout << endl;
}
}
void SortArray(Matrix arr[]) {
for (int column = 0; column < N_size; column++) {
for (int BlockSizeIterator = 1; BlockSizeIterator < N_size; BlockSizeIterator *= 2) {
for (int BlockIterator = 0; BlockIterator < N_size - BlockSizeIterator; BlockIterator += 2 * BlockSizeIterator) {
int LeftBlockIterator = 0; int RightBlockIterator = 0; int LeftBorder = BlockIterator;
int MidBorder = BlockIterator + BlockSizeIterator;
int RightBorder = BlockIterator + 2 * BlockSizeIterator; RightBorder = (RightBorder < N_size) ? RightBorder : N_size;
int* SortedBlock = new int[RightBorder - LeftBorder];
while (LeftBorder + LeftBlockIterator < MidBorder && MidBorder + RightBlockIterator < RightBorder) {
if (arr[LeftBorder + LeftBlockIterator].M[column] < arr[MidBorder + RightBlockIterator].M[column]) { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[LeftBorder + LeftBlockIterator].M[column]; LeftBlockIterator++;
}
else { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[MidBorder + RightBlockIterator].M[column]; RightBlockIterator++;
}
}
while (LeftBorder + LeftBlockIterator < MidBorder) { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[LeftBorder + LeftBlockIterator].M[column]; LeftBlockIterator++; }
while (MidBorder + RightBlockIterator < RightBorder) { SortedBlock[LeftBlockIterator + RightBlockIterator] = arr[MidBorder + RightBlockIterator].M[column]; RightBlockIterator++; }
for (int MergeIterator = 0; MergeIterator < LeftBlockIterator + RightBlockIterator; MergeIterator++) { arr[LeftBorder + MergeIterator].M[column] = SortedBlock[MergeIterator]; }
delete SortedBlock;
}
}
}
}

void CalcArray(Matrix Arr[]) {
double DobytokNadDopDia[N_size] = {1, 1, 1, 1, 1};
for(int column = 0; column < N_size; column++) {
for(int row = 0; row < N_size-column-1; row++) {
DobytokNadDopDia[column]*=(Arr[column].M[row]); }
}
double Function;
for (int column = 0; column < N_size; column++) {
Function += DobytokNadDopDia[column];
std::cout << "f(" << column << "): " << DobytokNadDopDia[column] << std::endl; }
cout << "F(f(ij)): " << Function << endl;
}

int main()
{ Matrix Array[n]; cout << "Enter elements of matrix:\n" << endl; InputArray(Array); cout << "\nYour matrix:\n" << endl; OutputArray(Array); SortArray(Array); cout << "\nSorted matrix:\n" << endl; OutputArray(Array); cout << "\nCalculations:\n" << endl; CalcArray(Array); }
{ Matrix Array[N_size];
cout << "Enter elements of matrix:\n" << endl;
InputArray(Array);
cout << "\nYour matrix:\n" << endl;
OutputArray(Array);
SortArray(Array);
cout << "\nSorted matrix:\n" << endl;
OutputArray(Array);
cout << "\nCalculations:\n" << endl;
CalcArray(Array);
}