Advertisement
parad0xxxxx

LW6 diagonals

Jan 24th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <iomanip>
  4. #include <windows.h>
  5. using namespace std;
  6.  
  7. void setRed()
  8. {
  9. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
  10. }
  11.  
  12. void setGreen()
  13. {
  14. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
  15. }
  16.  
  17. void setWhite()
  18. {
  19. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  20. }
  21.  
  22. int main()
  23. {
  24. int arrayIndexSize, arraySize, randRangeFrom, randRangeTo;
  25. bool isPositive = true;
  26. char v(0);
  27.  
  28. cout << "Enter array\'s first index\' value: ";
  29. cin >> arrayIndexSize;
  30.  
  31. cout << "Enter array\'s second index\' value: ";
  32. cin >> arraySize;
  33.  
  34. if (arrayIndexSize < 1 || arraySize < 1) {
  35. cout << "\n\nBoth values must be posivive integers.\n\n";
  36. return 0;
  37. }
  38.  
  39. int **arr = new int *[arrayIndexSize];
  40.  
  41. for (int i = 0; i < arrayIndexSize; i++)
  42. {
  43. arr[i] = new int[arraySize];
  44. }
  45.  
  46. cout << "\n\nHow would You like to fill in array, (M)anually or (A)utomatically? \n:";
  47. cin >> v;
  48.  
  49. if (v == 'M') {
  50. cout << "\nnice =)\n\n";
  51.  
  52. for (int i = 0; i < arrayIndexSize; i++)
  53. {
  54. cout << "Entering row " << i + 1 << ".\n\n";
  55. for (int j = 0; j < arraySize; j++)
  56. {
  57. cout << "Enter element " << j + 1 << ": ";
  58. cin >> arr[i][j];
  59.  
  60. }
  61. cout << endl;
  62. }
  63. }
  64.  
  65. else if (v == 'A') {
  66. cout << "\nYou\'re lazy =)\n\n";
  67.  
  68. cout << "Enter initial range value: ";
  69. cin >> randRangeFrom;
  70.  
  71. cout << "Enter end range value: ";
  72. cin >> randRangeTo;
  73. cout << endl;
  74.  
  75. srand(time(NULL));
  76. for (int i = 0; i < arrayIndexSize; i++)
  77. {
  78. for (int j = 0; j < arraySize; j++)
  79. {
  80. arr[i][j] = rand() % (randRangeTo - randRangeFrom + 1) + randRangeFrom;
  81. }
  82. }
  83. }
  84.  
  85. else {
  86. cout << "\nI expected \"M\" or \"A\".\nCome on, Cutie.\n\n";
  87. return 0;
  88. }
  89.  
  90. cout << "Your array is ready and served:\n\n";
  91.  
  92. for (int i = 0; i < arrayIndexSize; i++)
  93. {
  94. for (int j = 0; j < arraySize; j++)
  95. {
  96. if (i == j && arr[i][i] < 0)
  97. {
  98. setRed();
  99. cout << setw(5) << arr[i][j];
  100. setWhite();
  101. isPositive = false;
  102. }
  103. else if (i == j && arr[i][i] >= 0)
  104. {
  105. setGreen();
  106. cout << setw(5) << arr[i][j];
  107. setWhite();
  108. }
  109. else
  110. {
  111. cout << setw(5) << arr[i][j];
  112. }
  113. }
  114. cout << endl;
  115. }
  116.  
  117. if (isPositive)
  118. {
  119. setGreen();
  120. cout << "\nAll main diagonal elements are posive. Hoorray!" << endl;
  121. setWhite();
  122. }
  123. else
  124. {
  125. setRed();
  126. cout << "\nNot all main diagonal elements are posive. Sorry to tell You that, Cutie." << endl;
  127. setWhite();
  128. }
  129.  
  130. for (int i = 0; i < arrayIndexSize; i++)
  131. {
  132. delete[] arr[i];
  133. }
  134.  
  135. delete[] arr;
  136.  
  137. return 0;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement