lab7

Run Settings
LanguageC++
Language Version
Run Command
#include <iostream> #include <ctime> #include <iomanip> #include <string> #include <cmath> using namespace std; void task905() { srand(time(0)); int N; cout << "Enter N: "; cin >> N; int* x = new int[N]; int* y = new int[N]; double MaxDistance = 0, distance, point1, point2; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // Заполнение массивa x[i] = rand() % 100; x[j] = x[i]; } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // Заполнение массивa y[i] = rand() % 100; y[j] = y[i]; } } for (int i = 0; i < N; i++) for (int j = i; j < N; j++) { distance = sqrt(pow((double)(x[i] - x[j]), 2) + pow((double)(y[i] - y[j]), 2)); if (distance > MaxDistance) { MaxDistance = distance; point1 = i; point2 = j; } } cout << "Farthest points " << point1 + 1 << " and " << point2 + 1 << endl; } void task940() { cout << "Task(a)" << endl; int const k = 10; int matrixA[k][k]; int a, b; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { matrixA[i][j] = rand() % 100; cout << setw(3) << matrixA[i][j]; } cout << endl; } a = rand() % k; b = rand() % k; cout << "Coordinates first point:[" << a << "," << a << "]" << endl; cout << "Coordinates second point:[" << b << "," << b << "]" << endl; cout << "Amount:"<<matrixA[a][a] + matrixA[b][b] << endl; cout << "Task(b)" << endl; int matrixB[k][k]; int a1, a2, b1, b2; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { matrixB[i][j] = rand() % 100; cout << setw(3) << matrixB[i][j]; } cout << endl; } a1 = rand() % k; b1 = k - a1; a2 = rand() % k; b2 = k - a2; cout << "Coordinates first point:[" << a1 << "," << b1 << "]" << endl; cout << "Coordinates second point:[" << a2 << "," << b2 << "]" << endl; cout <<"composition:"<< matrixB[a1][b1] * matrixB[a2][b2] << endl; } void task975() { int n, count = 0; cout << "Enter n: "; cin >> n; if (n > 0) { double* arr1 = new double[n]; for (int i = 0; i < n; i++) { arr1[i] = rand() % 10; cout << setw(3) << arr1[i]; } double** arr2 = new double* [n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) arr2[i] = new double[j]; cout << endl; cout << "Matrix:" << endl; for (int i = 0; i < n; i++) { count = i; for (int j = 0; j < n; j++) { if (count >= n) count = 0; arr2[i][j] = arr1[count] * 1.0; count++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << setw(3) << arr2[i][j]; cout << endl; } } else cout << "n does not meet the condition" << endl; } void task1010() { const int sizeY = 10; const int sizeX = 10; char chars[sizeY][sizeX];// z y x const int sizeSimb = 4; char simb[sizeSimb] = { '#','=','+','?' }; bool freeSpace = true; int tStartX, tEndX, tStartY, tEndY; int sizeShift = 1;// отступ между квадратами for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { cout << (chars[y][x] = '.') << " "; } cout << endl; } cout << endl << endl; int sharpcount = 0, ravnocount = 0, pluscount = 0, voproscount = 0; for (int i = 0; i < 1000; i++) { int simbol = rand() % sizeSimb;// выбор символа tStartX = rand() % sizeX; tStartY = rand() % sizeY; do { tEndX = rand() % sizeX; tEndY = rand() % sizeY; } while (tStartX > tEndX || tStartY > tEndY); // отступы вокруг (логика если возле края массива) // X int shiftLeftX = (tStartX == 0) ? 0 : sizeShift; int shiftRightX = (tEndX == (sizeX - 1)) ? 0 : sizeShift;// ? // Y int shiftTopY = (tStartY == 0) ? 0 : sizeShift; int shiftBottomY = (tEndY == (sizeY - 1)) ? 0 : sizeShift; for (int y = (tStartY - shiftTopY); y <= (tEndY + shiftBottomY); y++) { for (int x = (tStartX - shiftLeftX); x <= (tEndX + shiftRightX); x++) { if (chars[y][x] != '.')freeSpace = false; } } if (freeSpace)// запись если место свободно { for (int y = tStartY; y <= tEndY; y++) { for (int x = tStartX; x <= tEndX; x++) { chars[y][x] = simb[simbol]; } } if (simbol == 0)sharpcount++; if (simbol == 1)ravnocount++; if (simbol == 2)pluscount++; if (simbol == 3)voproscount++; } freeSpace = true; //cout << "STartX: " << tStartX << " " << "ENd: " << tEndX << " " << simb[simbol] << endl; //cout << "STartY: " << tStartY << " " << "ENd: " << tEndY << " " << simb[simbol] << endl; //cout << shiftLeftX << " " << shiftRightX << " " << shiftTopY << " " << shiftBottomY; //cout << endl; } cout << endl; for (int y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++) { cout << chars[y][x] << " "; } cout << endl; } cout << "# rectangle: " << sharpcount << endl; cout << "? rectangle: " << voproscount << endl; cout << "+ rectangle: " << pluscount << endl; cout << "= rectangle: " << ravnocount << endl; } void task1045() { int const k = 10; int arr[k][k]; int n, count = 0, sum = 0; cout << "Our array: " << endl; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { arr[i][j] = rand() % 100; cout << setw(3) << arr[i][j]; if (j == 9)cout << endl; } } cout << "Enter n :"; cin >> n; cout << "Task(a)" << endl; int arr1[k]; for (int j = 0; j < k; j++) { count = 0; arr1[j] = 0; for (int i = 0; i < k; i++) { if (n < arr[i][j]) { count++; arr1[j] = count; } } cout << arr1[j] << setw(3); } cout << endl << endl; cout << "Task(b)" << endl; int arr2[k]; for (int i = 0; i < k; i++) { sum = 0; arr2[i] = 0; for (int j = 0; j < k; j++) { if (n > arr[i][j]) { sum += arr[i][j]; } } arr2[i] = sum; cout << arr2[i] << setw(5); } cout << endl << endl; } int main() { int a; while (true) { cout << "Numbers of tasks are 905 940 975 1010 1045\n"; cin >> a; cout << endl; switch (a) { case 905: task905(); break; case 940: task940(); break; case 975: task975(); break; case 1010: task1010(); break; case 1045: task1045(); break; case 0: return(0); break; default: cout << "Incorrect number, try again" << endl; break; } } } /* 905 940 975 1010 1045 */
Editor Settings
Theme
Key bindings
Full width
Lines