Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5. /*
  6. Задача 2: Запълване на масива.
  7. Запълване на двумерен динамичен масив със стойности също въведени от потребителя.
  8. */
  9. /*
  10. Check if any row of the array has duplicated elements. This functions is used... TODO
  11. */
  12. bool setHasDupes (int *arr, unsigned sz){
  13. for (int i = 0; i < sz; i++) {
  14. for (int j = i+1; j < sz; j++) {
  15. if (arr[i] == arr[j]) {
  16. return true;
  17. }
  18. }
  19. }
  20. return false;
  21. }
  22.  
  23. void fillArray (int **arr, int rows, int cols){
  24. for (unsigned i=0; i<rows; ++i) {
  25. for (unsigned j=0; j<cols; ++j) {
  26. cin >> arr[i][j];
  27. }
  28. if(setHasDupes(arr[0], cols)){
  29. i--;
  30. }
  31. }
  32. }
  33.  
  34. /*
  35. Тестова функция.
  36. отпечатване на масива в следния вид по редове
  37. row 1 : {1, 2, 3, 4},
  38. row 2 : {5, 6, 7, 8}.
  39. */
  40. void printArray (int **arr, int rows, int cols){
  41. for (unsigned i=0; i<rows; ++i) {
  42. cout << "row " << i+1 << ": || ";
  43. for (unsigned j=0; j<cols; ++j) {
  44. cout << arr[i][j] << ((j==cols-1)?" || ":" | ");
  45. }
  46. cout << ((i==rows-1)?" || ":" | ") << endl;
  47. }
  48.  
  49. }
  50.  
  51. /*
  52. Задача 3:
  53. Проверка дали функцията е инекция.
  54. Взема стойност от първи ред и сравнява със всияки стойности от втори ред,
  55. като търси дали въпросната стойнос се среща брой различен от 1 път,
  56. в такъв случай стойност false, в противен случай връща true;
  57. */
  58. bool isInjection(int **arr, int rows, int cols){
  59. unsigned cnt = 0;
  60. for (int i = 0; i < cols; i++){
  61. cnt = 0;
  62. for (int j = 0; j < cols; j++){
  63. if(arr[0][i] == arr[1][j]){
  64. cnt++;
  65. }
  66. }
  67. if(cnt != 1){
  68. return false;
  69. }
  70. }
  71. return true;
  72. if(setHasDupes(arr[1], cols)){
  73. return false;
  74. } else {
  75. return true;
  76. }
  77. }
  78. /*
  79. Задача 4:
  80. Проверка дали функцията е сюрекция.
  81. Взема стойност от втори ред и сравнява със всияки стойности от първи ред,
  82. като търси дали въпросната стойнос се среща поне 1 път път,
  83. в такъв случай връща стойност false, в противен случай връща true;
  84. */
  85. bool isSurjection(int **arr, int rows, int cols){
  86. unsigned cnt = 0;
  87. for (int i = 0; i < cols; i++){
  88. cnt = 0;
  89. for (int j = 0; j < cols; j++){
  90. if(arr[1][i] == arr[0][j]){
  91. cnt++;
  92. }
  93. }
  94. if(cnt == 0){
  95. return false;
  96. }
  97. }
  98. return true;
  99. if(!setHasDupes(arr[1], cols) && setHasDupes(arr[0], cols)){
  100. return false;
  101. } else {
  102. return true;
  103. }
  104. }
  105.  
  106. /*
  107. Задача 5:
  108. Проверка дали функцията е бинекция.
  109. Взема за стойности резултатите от булевите функции за сюрекция и инекция и ако и двете дават за резултат true,
  110. следва, че функцията е бинекция. Ако поне един от резултатите е false, следва, че не е бинекция.
  111. */
  112.  
  113. bool isBijection(int **arr, int rows, int cols, bool(*isSur)(int**, int, int), bool(*isInj)(int**, int, int)){
  114. if((isSur(arr, rows, cols)) && (isInj(arr, rows, cols))){
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. }
  120.  
  121. /*6 задача*/
  122. unsigned permutationNum( unsigned n ){
  123. if( n <= 1 ) return 1;
  124. return n * permutationNum( n-1 );
  125. }
  126. /*7*/
  127. bool nepodvijNATochka(int **arr, int rows, int cols){
  128. for (int i = 0; i < cols; i++){
  129. if(arr[0][i] == arr[1][i]){
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. /*8*/
  136. unsigned numberOfNT(int **arr, int rows, int cols){
  137. unsigned cnt = 0;
  138. for (int i = 0; i < cols; i++){
  139. if(arr[0][i] == arr[1][i]){
  140. cnt++;
  141. }
  142. }
  143. return cnt;
  144. }
  145.  
  146. /*9*/
  147. bool hasIdentities(int **arr, int rows, int cols){
  148. if(numberOfNT(arr, 2, cols) == cols){
  149. return true;
  150. } else {
  151. return false;
  152. }
  153.  
  154. }
  155.  
  156. int getIndex(int *set, int size, int element) {
  157. for (int i = 0; i < size; i++) {
  158. if (set[i] == element) {
  159. return i;
  160. }
  161. }
  162. return -1;
  163. }
  164.  
  165. int getFirstFreeIndex(int *passedIndexes, int size) {
  166. for (int i = 0; i < size; i++) {
  167. if (passedIndexes[i] == 0) {
  168. return i;
  169. }
  170. }
  171. return -1;
  172. }
  173.  
  174. void printElementsCntPerCycle(int *permutation, int *universal, int size) {
  175. int elementsPerIndependentCycle[size]{0};
  176. int currentIndependentCycle = 0;
  177.  
  178. int cyclesCnt = 0;
  179.  
  180. int passedIndexes[size]{0};
  181.  
  182. int wantedIndex = 0;
  183. int wanted = universal[wantedIndex];
  184.  
  185. int operatingIndex = 0;
  186. int upElement = universal[operatingIndex];
  187. int downElement = permutation[operatingIndex];
  188.  
  189. while (cyclesCnt < size) {
  190. elementsPerIndependentCycle[currentIndependentCycle]++;
  191. while (downElement != wanted) {
  192. elementsPerIndependentCycle[currentIndependentCycle]++;
  193. passedIndexes[operatingIndex] = -1;
  194. operatingIndex = getIndex(universal, size, downElement);
  195. upElement = universal[operatingIndex];
  196. downElement = permutation[operatingIndex];
  197. cyclesCnt++;
  198. }
  199. currentIndependentCycle++;
  200. passedIndexes[operatingIndex] = -1;
  201. operatingIndex = getFirstFreeIndex(passedIndexes, size);
  202. if (operatingIndex == -1) {
  203. break;
  204. }
  205. wanted = universal[operatingIndex];
  206. upElement = universal[operatingIndex];
  207. downElement = permutation[operatingIndex];
  208. }
  209.  
  210. for(int i = 0; i < currentIndependentCycle; i++){
  211. cout << "Cycle " << i + 1 << " has " << elementsPerIndependentCycle[i] << " elements." << endl;
  212. }
  213.  
  214. }
  215.  
  216. void printCycles(int *permutation, int *universal, int size) {
  217. int cyclesCnt = 0;
  218.  
  219. int passedIndexes[size]{0};
  220.  
  221. int wantedIndex = 0;
  222. int wanted = universal[wantedIndex];
  223.  
  224. int operatingIndex = 0;
  225. int upElement = universal[operatingIndex];
  226. int downElement = permutation[operatingIndex];
  227.  
  228. while (cyclesCnt < size) {
  229. cout << "(";
  230. while (downElement != wanted) {
  231. passedIndexes[operatingIndex] = -1;
  232. cout << upElement << " ";
  233. operatingIndex = getIndex(universal, size, downElement);
  234. upElement = universal[operatingIndex];
  235. downElement = permutation[operatingIndex];
  236. cyclesCnt++;
  237. }
  238. cout << upElement << ")";
  239. passedIndexes[operatingIndex] = -1;
  240. operatingIndex = getFirstFreeIndex(passedIndexes, size);
  241. if (operatingIndex == -1) {
  242. break;
  243. }
  244. wanted = universal[operatingIndex];
  245. upElement = universal[operatingIndex];
  246. downElement = permutation[operatingIndex];
  247. }
  248. cout << endl;
  249.  
  250. }
  251.  
  252.  
  253.  
  254. int main(){
  255.  
  256.  
  257.  
  258. /*
  259. Задача 1:
  260. Дефиниране на двумерен масив с 2 реда и брой колони въведени от потребителя.
  261. */
  262. unsigned rows = 2, cols;
  263. cout << "Please enter number of columns: ";
  264. cin >> cols;
  265. int ** arr = NULL;
  266. arr = new int* [rows];
  267. for (unsigned i=0; i<rows; ++i) {
  268. arr[i] = new int[cols];
  269. }
  270.  
  271.  
  272.  
  273. fillArray(arr, 2, cols);
  274. printArray(arr, 2, cols);
  275. printCycles(arr[1], arr[0], cols);
  276. printElementsCntPerCycle(arr[1], arr[0], cols);
  277.  
  278.  
  279.  
  280. /*
  281. cout << "PERMUTATIONS" << endl;
  282. cout << ((isInjection(arr, 2, cols))?"Yes, the function is injective!":"No, the function isn't injective!") << endl;
  283. cout << ((isSurjection(arr, 2, cols))?"Yes, the function is surjective!":"No, the function isn't surjective!") << endl;
  284. /*
  285. Задача 5:
  286. Ако една функция е едновременно инекция и сюрекция, от тя е Бинекция.
  287. *//*
  288. cout << ((isBijection(arr, 2, cols, isSurjection, isInjection))?"Yes, the function is bijective!":"No, the function isn't bijective!") << endl;
  289. cout << "The amount of permutations of set with 5 elements is: " << permutationNum(cols) << endl;
  290.  
  291.  
  292.  
  293.  
  294.  
  295. cout << "NEPODVIJNI TOCHKI" << endl;
  296. if (nepodvijNATochka(arr, 2, cols) == true && numberOfNT(arr, 2, cols) != cols){
  297. cout << "Yes it has " << numberOfNT(arr, 2, cols) << " nepodvijni tochki." << endl;
  298. } else {
  299. cout << "It doesn't have nepodvijni tochki" << endl;
  300. }
  301.  
  302. cout << ((hasIdentities(arr, 2, cols))?"Yes, the function has identities!":"No, the function hasn't identities!") << endl;
  303.  
  304.  
  305.  
  306. cout << "INDEPENDET LOOPS" << endl;
  307.  
  308.  
  309. // Освобождава се заделената памет
  310. for (unsigned i=0; i<rows; ++i) {
  311. delete[] arr[i];
  312. }
  313. delete[] arr;
  314. arr = NULL;*/
  315. return 0;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement