Advertisement
Guest User

Zad8

a guest
Jan 19th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. // Libraries
  2. #include "pch.h"
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <fstream>
  6. #include <string>
  7. #include <stdlib.h>  
  8. #include <cstdlib>
  9. #include <sstream>
  10. #include <time.h>
  11.  
  12. using namespace std;
  13.  
  14. // Zmienne do "sterowania" programem
  15. const int w = 4, k = 4;
  16. const double X = 5, D = 3, G = 10;
  17.  
  18. // Funkcja wypełniająca dany array wartościami losowymi od min do max
  19. void fillArray(double customArray[w][k], double min, double max)
  20. {
  21.     srand(time(NULL));
  22.    
  23.     for (int i = 0; i < w; i++)
  24.     {
  25.         for (int j = 0; j < k; j++)
  26.         {
  27.             customArray[i][j] = min + (max - min) * rand() / double(RAND_MAX);
  28.         }
  29.     }  
  30. }
  31.  
  32. // Funkcja drukująca dany array z daną precyzją
  33. // (+ dodatkowy width do zwiększenia czytelności)
  34. void printArray(double arrToPrint[w][k], int precision)
  35. {
  36.     for (int i = 0; i < w; i++)
  37.     {
  38.         for (int j = 0; j < k; j++)
  39.         {
  40.             cout << fixed << setprecision(precision) << setw(7) << arrToPrint[i][j];
  41.         }
  42.  
  43.         cout << endl;
  44.     }
  45. }
  46.  
  47. // Funkcja autorska odwracająca kolejność elementów
  48. // w wierszu tablicy dwuwymiarowej (typu double)
  49. double reverseRow(double arrToReverse[w][k], int rowToReverse)
  50. {
  51.     double tempLeft;
  52.     double tempRight;
  53.  
  54.     int start = 0;
  55.     int end = w - 1;
  56.  
  57.     while (start < end)
  58.     {
  59.         tempLeft = arrToReverse[rowToReverse][start];
  60.         tempRight = arrToReverse[rowToReverse][end];
  61.  
  62.         arrToReverse[rowToReverse][end] = tempLeft;
  63.         arrToReverse[rowToReverse][start] = tempRight;
  64.  
  65.         start++;
  66.         end--;
  67.     }
  68.  
  69.     return arrToReverse[w][k];
  70. }
  71.  
  72. // Funkcja odwracająca kolejność elementów w wierszach zaczynających się
  73. // liczbą mniejszą od danej wartości
  74. int changeArr(double arrToModify[w][k], double numToCheck)
  75. {
  76.     int colCounter = 0;
  77.  
  78.     for (int i = 0; i < w; i++)
  79.     {
  80.         if (arrToModify[i][0] < numToCheck)
  81.         {
  82.             reverseRow(arrToModify, i);
  83.             colCounter++;
  84.         }
  85.     }
  86.  
  87.     return colCounter;
  88. }
  89.  
  90. // Wywołanie programu
  91. int main()
  92. {
  93.     double A[w][k];
  94.     double B[w][k];
  95.  
  96.     fillArray(A, -X, X);
  97.     printArray(A, 1);
  98.     cout << endl;
  99.  
  100.     fillArray(B, D, G);
  101.     printArray(B, 2);
  102.     cout << endl;
  103.  
  104.     int rowsCounterA = changeArr(A, 0);
  105.     int numToModifyArr;
  106.  
  107.     cout << "Podaj liczbe dla jakiej tablica ma byc zmodyfikowana: ";
  108.     cin >> numToModifyArr;
  109.  
  110.     while (cin.fail())
  111.     {
  112.         cin.clear();
  113.         cin.ignore(1000, '\n');
  114.         cout << "To nie jest liczba!" << endl;
  115.         cout << "Sprobuj ponownie: " << endl;
  116.         cin >> numToModifyArr;
  117.     }
  118.  
  119.     int rowsCounterB = changeArr(B, numToModifyArr);
  120.  
  121.     printArray(A, 1);
  122.     cout << endl;
  123.     printArray(B, 2);
  124.     cout << endl;
  125.  
  126.     if (rowsCounterA == rowsCounterB)
  127.     {
  128.         cout << "Zamieniono taka sama liczbe wierszy";
  129.     }
  130.     else
  131.     {
  132.         cout << "Zamieniono wiecej wierszy w tablicy: ";
  133.         (rowsCounterA > rowsCounterB) ? cout << "A" : cout << "B";
  134.     }
  135.  
  136.     cout << endl;
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement