wintest

МАТРИЦА: Максимална сума на ред

Jan 14th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <clocale>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. #define M 5
  9. #define N 5
  10.  
  11. int maxSumInRow(int array[][N], size_t rows);
  12. void fillTheArray(int array[][N], size_t rows);
  13. void printTheArray(int array[][N], size_t rows);
  14.  
  15. int main(){
  16.     setlocale(LC_ALL, "Bulgarian");
  17.     srand(time(NULL));
  18.  
  19.     int array[M][N];
  20.     fillTheArray(array, M);
  21.     printTheArray(array, M);
  22.     cout << endl;
  23.     cout << "Максималната сума на ред е " << maxSumInRow(array, M) << endl;
  24.  
  25.     return 0;
  26. }
  27.  
  28. //Е) намиране на реда на матрицата, който има максимална сума;
  29. int maxSumInRow(int array[][N], size_t rows){
  30.     int max=0;
  31.     int tmp=0;
  32.     for (size_t i = 0; i < M; i++)
  33.     {
  34.         for (size_t j = 0; j < N; j++){
  35.             tmp += array[i][j];  // 1 2 3 4 = 13 =tmp
  36.                                  // 5 6 7 8 = 26
  37.         }
  38.         if (tmp > max){
  39.             max = tmp;
  40.         }
  41.         tmp = 0;
  42.     }
  43.     return max;
  44. }
  45. //слагам стойности
  46. void fillTheArray(int array[][N], size_t rows){
  47.     for (size_t i = 0; i < M; i++)
  48.     {
  49.         for (size_t j = 0; j < N; j++){
  50.             array[i][j] = rand() % 5;
  51.  
  52.         }
  53.     }
  54. }
  55. //принтирам масив
  56. void printTheArray(int array[][N], size_t rows){
  57.     cout << "Оригиналната матрица е това : " << endl;
  58.  
  59.     for (size_t i = 0; i < M; i++)
  60.     {
  61.         for (size_t j = 0; j < N; j++){
  62.             cout << array[i][j] << "\t";
  63.         }
  64.         cout << endl;
  65.     }
  66. }
Add Comment
Please, Sign In to add comment