parad0xxxxx

LW6 Juliette

Jan 24th, 2020
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 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;
  9. bool isPositive = true;
  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. cout << "Enter initial range value: ";
  50. cin >> randRangeFrom;
  51.  
  52. cout << "Enter end range value: ";
  53. cin >> randRangeTo;
  54. cout << endl;
  55.  
  56. srand(time(NULL));
  57. for (int i = 0; i < arrayIndexSize; i++)
  58. {
  59. for (int j = 0; j < arraySize; j++)
  60. {
  61. arr[i][j] = rand() % (randRangeTo - randRangeFrom + 1) + randRangeFrom;
  62. }
  63. }
  64. }
  65.  
  66. else {
  67. cout << "\nI expected \"M\" or \"A\".\n\n";
  68. return 0;
  69. }
  70.  
  71. cout << "Your array:\n\n";
  72.  
  73. for (int i = 0; i < arrayIndexSize; i++)
  74. {
  75. for (int j = 0; j < arraySize; j++)
  76. {
  77. cout << setw(5) << arr[i][j];
  78. }
  79. cout << endl;
  80. }
  81.  
  82. cout << "\nSum of odds:\n";
  83. int sum;
  84. for (int j = 0; j < arraySize; j++)
  85. {
  86. sum = 0;
  87. for (int i = 0; i < arrayIndexSize; i++)
  88. {
  89. if (arr[i][j] % 2 != 0)
  90. {
  91. sum += arr[i][j];
  92. }
  93. }
  94. cout << setw(5) << sum;
  95. }
  96.  
  97. cout << endl;
  98.  
  99. for (int i = 0; i < arrayIndexSize; i++)
  100. {
  101. delete[] arr[i];
  102. }
  103.  
  104. delete[] arr;
  105.  
  106. return 0;
  107. }
Add Comment
Please, Sign In to add comment