Advertisement
makispaiktis

All the elements of an array's rows have sum = 1

Jan 3rd, 2020 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <ctime>
  4. #include <math.h>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12.     srand(time(NULL));
  13.     // 1. Ask the user to give the array's  dimensions
  14.     cout << "Give me the dimensions of an array (m and n must be >= 2).\n";
  15.     int m,n;
  16.     cout << "First, give me m = number of rows: ";
  17.     cin >> m;
  18.     while(m < 2){
  19.         cout << "First, give me m = number of rows: ";
  20.         cin >> m;
  21.     }
  22.     cout << "Now, give me n = number of columns: ";
  23.     cin >> n;
  24.     while(n < 2){
  25.         cout << "Give me n = number of columns: ";
  26.         cin >> n;
  27.     }
  28.     cout << endl << endl;
  29.  
  30.     // 2. Initialize the array's elements by giving them a first random value
  31.     double matrix[m][n];
  32.     for(int i=0; i<m; i++){
  33.         for(int j=0; j<n; j++){
  34.             // Firstly, each element will be an integer number from 0 to 99 (inclusive)
  35.             matrix[i][j] = rand() % 100;
  36.         }
  37.     }
  38.  
  39.     // Now, I will show the elements of the array
  40.     cout << "The array in the beginning is this one: " << endl;
  41.     for(int i=0; i<m; i++){
  42.         for(int j=0; j<n; j++){
  43.             cout << matrix[i][j] << " ";
  44.         }
  45.         cout << endl;
  46.     }
  47.     cout << endl << endl;
  48.  
  49.     // 3. Now, I will create a vector with "m" values = the "m" sums of the "m" rows of the array
  50.     vector <int> sums = vector <int> ();
  51.     for(int i=0; i<m; i++){
  52.         int sum = 0;
  53.         for(int j=0; j<n; j++){
  54.             sum += matrix[i][j];
  55.         }
  56.         sums.push_back(sum);
  57.     }
  58.  
  59.     // 4. I divide every single element with its row's sum of elements
  60.     for(int i=0; i<m; i++){
  61.         for(int j=0; j<n; j++){
  62.             matrix[i][j] /= sums[i];
  63.         }
  64.     }
  65.  
  66.     // Now, I will display the final array: each row has sum = 1
  67.     cout << "The final array is this one: " << endl;
  68.     for(int i=0; i<m; i++){
  69.         for(int j=0; j<n; j++){
  70.             cout << matrix[i][j] << " ";
  71.         }
  72.         cout << endl;
  73.     }
  74.     cout << endl << endl;
  75.  
  76.     // 5. Explaining
  77.     cout << "Because: \n";
  78.     for(int i=0; i<m; i++){
  79.         for(int j=0; j<n; j++){
  80.             if(j != n-1){
  81.                 cout << matrix[i][j] << " + ";
  82.             }
  83.             else{
  84.                 cout << matrix[i][j] << " = 1" << endl;
  85.             }
  86.         }
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement