Advertisement
parad0xxxxx

pozitīvu elementu summu kur ir negs

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