Advertisement
parad0xxxxx

LW6 pozitīvu elementu reizinājumu

Jan 24th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int arrayIndexSize, arraySize, randRangeFrom, randRangeTo, multiply;
  9. bool isSecondPositive, isFirst;
  10. char v(0);
  11.  
  12. cout << "Enter array\'s first index\' value: ";
  13. cin >> arrayIndexSize;
  14.  
  15. cout << "Enter array\'s second index\' value: ";
  16. cin >> arraySize;
  17.  
  18. if (arrayIndexSize < 1 || arraySize < 1) {
  19. cout << "\n\nBoth values must be posivive integers.\n\n";
  20. return 0;
  21. }
  22.  
  23. int** arr = new int* [arrayIndexSize];
  24.  
  25. for (int i = 0; i < arrayIndexSize; i++)
  26. {
  27. arr[i] = new int[arraySize];
  28. }
  29.  
  30. cout << "\n\nHow would You like to fill in array, (M)anually or (A)utomatically? \n:";
  31. cin >> v;
  32.  
  33. if (v == 'M') {
  34.  
  35. for (int i = 0; i < arrayIndexSize; i++)
  36. {
  37. cout << "Entering row " << i + 1 << ".\n\n";
  38. for (int j = 0; j < arraySize; j++)
  39. {
  40. cout << "Enter element " << j + 1 << ": ";
  41. cin >> arr[i][j];
  42.  
  43. }
  44. cout << endl;
  45. }
  46. }
  47.  
  48. else if (v == 'A') {
  49.  
  50. cout << "Enter initial range value: ";
  51. cin >> randRangeFrom;
  52.  
  53. cout << "Enter end range value: ";
  54. cin >> randRangeTo;
  55. cout << endl;
  56.  
  57. srand(time(NULL));
  58. for (int i = 0; i < arrayIndexSize; i++)
  59. {
  60. for (int j = 0; j < arraySize; j++)
  61. {
  62. arr[i][j] = rand() % (randRangeTo - randRangeFrom + 1) + randRangeFrom;
  63. }
  64. }
  65. }
  66.  
  67. else {
  68. cout << "\nI expected \"M\" or \"A\".\n\n";
  69. return 0;
  70. }
  71.  
  72. cout << "Your array:\n\n";
  73. // Aprēķināt pozitīvu elementu reizinājumu tajās masīva rindās, kurās ir vismaz divi pozitīvie elementi.
  74.  
  75. for (int i = 0; i < arrayIndexSize; i++) {
  76. multiply = 0;
  77. isSecondPositive = false;
  78. isFirst = false;
  79. for (int j = 0; j < arraySize; j++)
  80. {
  81. if (!isFirst && arr[i][j] >= 0)
  82. {
  83. cout << setw(5) << arr[i][j];
  84. multiply = arr[i][j];
  85. isFirst = true;
  86. }
  87. else if (isFirst && arr[i][j] >= 0)
  88. {
  89.  
  90. cout << setw(5) << arr[i][j];
  91. multiply *= arr[i][j];
  92. isSecondPositive = true;
  93. }
  94. else
  95. {
  96. cout << setw(5) << arr[i][j];
  97. }
  98. }
  99.  
  100. cout << " | ";
  101.  
  102. if (isSecondPositive)
  103. {
  104. cout << multiply;
  105. }
  106. else if (!isFirst)
  107. {
  108. cout << "no positives";
  109. }
  110. else
  111. {
  112. cout << "no second positive";
  113. }
  114.  
  115. cout << endl;
  116. }
  117.  
  118.  
  119. for (int i = 0; i < arrayIndexSize; i++)
  120. {
  121. delete[] arr[i];
  122. }
  123.  
  124. delete[] arr;
  125.  
  126. return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement