Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Libraries
- #include "pch.h"
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <stdlib.h>
- #include <cstdlib>
- #include <sstream>
- #include <time.h>
- using namespace std;
- // Zmienne do "sterowania" programem
- const int w = 4, k = 4;
- const double X = 5, D = 3, G = 10;
- // Funkcja wypełniająca dany array wartościami losowymi od min do max
- void fillArray(double customArray[w][k], double min, double max)
- {
- srand(time(NULL));
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < k; j++)
- {
- customArray[i][j] = min + (max - min) * rand() / double(RAND_MAX);
- }
- }
- }
- // Funkcja drukująca dany array z daną precyzją
- // (+ dodatkowy width do zwiększenia czytelności)
- void printArray(double arrToPrint[w][k], int precision)
- {
- for (int i = 0; i < w; i++)
- {
- for (int j = 0; j < k; j++)
- {
- cout << fixed << setprecision(precision) << setw(7) << arrToPrint[i][j];
- }
- cout << endl;
- }
- }
- // Funkcja autorska odwracająca kolejność elementów
- // w wierszu tablicy dwuwymiarowej (typu double)
- double reverseRow(double arrToReverse[w][k], int rowToReverse)
- {
- double tempLeft;
- double tempRight;
- int start = 0;
- int end = w - 1;
- while (start < end)
- {
- tempLeft = arrToReverse[rowToReverse][start];
- tempRight = arrToReverse[rowToReverse][end];
- arrToReverse[rowToReverse][end] = tempLeft;
- arrToReverse[rowToReverse][start] = tempRight;
- start++;
- end--;
- }
- return arrToReverse[w][k];
- }
- // Funkcja odwracająca kolejność elementów w wierszach zaczynających się
- // liczbą mniejszą od danej wartości
- int changeArr(double arrToModify[w][k], double numToCheck)
- {
- int colCounter = 0;
- for (int i = 0; i < w; i++)
- {
- if (arrToModify[i][0] < numToCheck)
- {
- reverseRow(arrToModify, i);
- colCounter++;
- }
- }
- return colCounter;
- }
- // Wywołanie programu
- int main()
- {
- double A[w][k];
- double B[w][k];
- fillArray(A, -X, X);
- printArray(A, 1);
- cout << endl;
- fillArray(B, D, G);
- printArray(B, 2);
- cout << endl;
- int rowsCounterA = changeArr(A, 0);
- int numToModifyArr;
- cout << "Podaj liczbe dla jakiej tablica ma byc zmodyfikowana: ";
- cin >> numToModifyArr;
- while (cin.fail())
- {
- cin.clear();
- cin.ignore(1000, '\n');
- cout << "To nie jest liczba!" << endl;
- cout << "Sprobuj ponownie: " << endl;
- cin >> numToModifyArr;
- }
- int rowsCounterB = changeArr(B, numToModifyArr);
- printArray(A, 1);
- cout << endl;
- printArray(B, 2);
- cout << endl;
- if (rowsCounterA == rowsCounterB)
- {
- cout << "Zamieniono taka sama liczbe wierszy";
- }
- else
- {
- cout << "Zamieniono wiecej wierszy w tablicy: ";
- (rowsCounterA > rowsCounterB) ? cout << "A" : cout << "B";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement