ToniDev

Ex suma vecinilor < elementul actual din matrice

Oct 18th, 2023
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void populateMatrix(int m[][10], int n)
  5. {
  6.     int counter = 1;
  7.  
  8.     for (int i = 0; i < n; i++)
  9.     {
  10.         for (int j = 0; j < n; j++)
  11.         {
  12.             m[i][j] = counter;
  13.             counter++;
  14.         }
  15.     }
  16.  
  17.     cout << "Matrix populated successfully!" << endl;
  18. }
  19.  
  20. void showMatrix(int m[][10], int n)
  21. {
  22.     cout << "Matrix display: " << endl;
  23.  
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         for (int j = 0; j < n; j++)
  27.         {
  28.             cout << m[i][j] << "\t";
  29.         }
  30.         cout << endl;
  31.     }
  32. }
  33.  
  34. int calculate(int m[][10], int n)
  35. {
  36.     // Check the neighbour values for each element, and if the element is greater than all of them, sum it up
  37.     int sum = 0;
  38.  
  39.     for (int i = 1; i < n - 1; i++)
  40.     {
  41.         for (int j = 1; j < n - 1; j++)
  42.         {
  43.         }
  44.     }
  45.  
  46.     return sum;
  47. }
  48.  
  49. int main() {
  50.     int matrix[10][10];
  51.     int n; // Rows and columns, since it's a square matrix
  52.  
  53.     cout << "Enter the number of rows and columns: ";
  54.     cin >> n;
  55.  
  56.     populateMatrix(matrix, n);
  57.     showMatrix(matrix, n);
  58.  
  59.     cout << "The sum of the elements that are greater than all their neighbours is: " << calculate(matrix, n) << endl;
  60.  
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment