fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7.  
  8. int main()
  9. {
  10. srand(time(0));
  11. int rows = 3;
  12. int cols = 3;
  13. int arr[rows][cols];
  14. for (int i=0; i<rows; i++)
  15. {
  16. for (int j=0; j<cols; j++)
  17. {
  18. arr[i][j] = 1 + (rand() % 9 + (-2));
  19. cout << arr[i][j] << " ";
  20.  
  21. }
  22. }
  23. int sum = 0;
  24. putchar('\n');
  25. for (int i = 0; i < rows; ++i)
  26. {
  27. for (int j = 0; j < cols; ++j)
  28. {
  29. for (int k = i; k < rows; ++k)
  30. {
  31. for (int l = (k == i ? j + 1 : 0); l < cols; ++l)
  32. {
  33. if (arr[i][j] == arr[k][l])
  34. {
  35. cout << "Matching element: " << arr[i][j] << " ";
  36. }
  37. }
  38. }
  39. }
  40. }
  41. for (int i = 0; i < rows; ++i)
  42. {
  43. bool negN = false;
  44. for (int j = 0; j < cols; ++j)
  45. {
  46. if (arr[i][j] < 0)
  47. {
  48. putchar('\n');
  49. cout << "Founded negN in [" << i << "][" << j << "]" << "\n";
  50. negN = true;
  51. break;
  52. }
  53. }
  54. if (negN)
  55. {
  56. for (int j = 0; j < cols; ++j)
  57. {
  58. sum += arr[i][j];
  59. }
  60. }
  61. }
  62. putchar('\n');
  63. cout << "Sum of Neg Numbers : " << sum;
  64. return 0;
  65. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
0 -1 2 -1 -1 0 4 -1 4 
Matching element: 0 Matching element: -1 Matching element: -1 Matching element: -1 Matching element: -1 Matching element: -1 Matching element: -1 Matching element: 4 
Founded negN in [0][1]

Founded negN in [1][0]

Founded negN in [2][1]

Sum of Neg Numbers : 6