0
 #include <iostream>
using namespace std;
const int N = 3;
void swap(double matrix[N][N + 1], int i, int j)
{
    ...
}
int forwardElim(double mat[N][N + 1])
{
    ...
}
void backSub(double mat[N][N + 1])
{
    ...
}
void gaussianElimination(double mat[N][N + 1])
{
    ...
}
int main()
{
    int i, j;
    double matrix[N][N+1];
    int k = 1;
    for (i = 0; i < N; i++)
    {
        cout << "Enter value for equation " << k << ": " << endl;
        for (j = 0; j < N +1; j++)
        {
            cout << "[" << i << "]" << "[" << j << "] = ";
            cin >> matrix[i][j];
        }
        k++;
    }

    cout << "Your equation is: " << endl;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N+1; j++) {
            cout << matrix[i][j] << "   ";
        }
        cout << endl;
    }
    gaussianElimination(matrix);
    system("pause");
}

I'm a newbie so can anyone help me with this basic question, please. How can I get any value of N from the keyboard but outside the main function? Because I still have some function outside of main using N

Kuan N
  • 3
  • 4
  • 6
    If `N` is not `const` then you need to use `std::vector` as C++ does not have (standardized) variable length arrays like that. – tadman Sep 17 '20 at 07:17
  • 1
    `N` can be passed to the function as a parameter instead of being global. That said, as @tadman said, use `std::vector` which also always knows its size. – Quimby Sep 17 '20 at 07:23
  • int N; cin >> N; double ***matrix; matrix = new double* [N]; for (int i = 0; i < N; i++) { matrix[i] = new double[N + 1]{0}; } In any case, I also recommend you to use `std::vector`. – Zeus Sep 17 '20 at 07:24

3 Answers3

2

You can easily do the required using std::vector instead of static arrays. You can get n by using std::vector::size. You can simplify your code by using range-based for loops. Avoid using using namespace std;. Below is some self explanatory code :

#include <iostream>
#include <vector>

/// some other function
void foo(std::vector<std::vector<double>>
             &matrix) {  // <-- make function arguments like this, instead of
                         // double matrix[N][N+1]
  int n = matrix.size(); // <-- put this line inside the function if you need
                         // the value of n
  std::cout << "n = " << n;
  // do something here
}

int main() {
  int n;
  std::cout << "Enter n: ";
  std::cin >> n;

  std::vector<std::vector<double>> matrix(n, std::vector<double>(n + 1));

  for (int i = 0; i < n; ++i) {
    std::cout << "Enter value for equation " << i + 1 << ":\n";
    for (int j = 0; j < n + 1; ++j) {
      std::cout << "[" << i << "]"
                << "[" << j << "] = ";
      std::cin >> matrix[i][j];
    }
  }

  std::cout << "Your equation is:\n";
  for (auto &&row : matrix) {
    for (auto &&ele : row)
      std::cout << ele << '\t';
    std::cout << '\n';
  }

  foo(matrix); // <-- function call remains the same
}

Approach using a global variable n (you need to change just only 4-5 lines of your code if you plan on using using this method):

#include <iostream>
#include <vector>

int n; // make n global, non-const

/// some other function
void foo(std::vector<std::vector<double>>
             &matrix) { // <-- make function arguments like this, instead of
                        // double matrix[N][N+1]

  // no need for int n = n.size(); here
  std::cout << "n = " << n;
  // do something here
}

int main() {
  // int n; <-- no need to declare it here, it's available globally
  std::cout << "Enter n: ";
  std::cin >> n;

  std::vector<std::vector<double>> matrix(n, std::vector<double>(n + 1));

  // rest code is same as the above...
}
brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • I just edited my code. Can I still use array for that? – Kuan N Sep 17 '20 at 07:42
  • @QuanThanh Yeah you can use vector for that also. Just change `double matrix[N][N + 1]` in your function arguments by `std::vector> &matrix`. The rest of code will remain same. If you want `n` in all functions and do not want to write line `int n = matrix.size();` in every function then just make `n` global (and non-constant), then everything will work fine. – brc-dd Sep 17 '20 at 07:44
  • I got it. Thank you so much! – Kuan N Sep 17 '20 at 07:59
0

As suggested in the comments using vectors would be better. So as N is declared globally, main can alter the value of N, you just need to make sure it is not const. As scope of the variable N is global, it can be used and edited in any of the functions.

#include <iostream>
#include <vector>

using namespace std;
int N = 3;  // Putting some default value
void swap(std::vector<std::vector<double>> &matrix, int i, int j)
{
    /*Wherever in the function you are using N , you can use it as normal or you can also get the size as */

   // Alternative way to get size if using vectors
    int size= matrix.size();
    ...
}
int forwardElim(std::vector<std::vector<double>> &matrix)
{
    ...
}
void backSub(std::vector<std::vector<double>> &matrix)
{
    ...
}
void gaussianElimination(std::vector<std::vector<double>> &matrix)
{
    ...
}
int main()
{
    int i, j;
    cout<<"Enter value of N"<<endl;
    cin>>N;  // Taking value of N from user 
    
/* Declaring 2D matrix using vectors of size n*n+1 */
    vector<vector<double> > matrix (N,vector<double> (N+1)); 

/* Rest of the program stays the same */
    int k = 1;
    for (i = 0; i < N; i++)
    {
        cout << "Enter value for equation " << k << ": " << endl;
        for (j = 0; j < N +1; j++)
        {
            cout << "[" << i << "]" << "[" << j << "] = ";
            cin >> matrix[i][j];
        }
        k++;
    }

    cout << "Your equation is: " << endl;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N+1; j++) {
            cout << matrix[i][j] << "   ";
        }
        cout << endl;
    }
   gaussianElimination(matrix);
    system("pause");
return 0;
}

Otherwise you can simply do this, which will work but is not advised

#include <iostream>
#include <vector>

using namespace std;
int N; //Removed Const

int main()
{
    int i, j;
    cout<<"Enter value of N"<<endl;
    cin>>N;
    double matrix[N][N+1];
//Rest of your code which stays the same
Mohit Sharma
  • 338
  • 2
  • 13
  • Does anyway that I can do it outside main function? because I still have some function outside main using that variable. I just edit my code above. Please take a look – Kuan N Sep 17 '20 at 07:40
  • Yes you can still use the value of n outside the main function. Also you can use `matrix.size()` to get the size of matrix in any other function. The function definitions would change a little, I will edit my answer to show. – Mohit Sharma Sep 17 '20 at 07:46
0

In C++ arrays have a compile time fixed size. If you want a variable size, then you need to use dynamic memory. This means that you can use a pointer to allocate manually like this:

int N;

int main()
{
  cin >> N;
  int** matrix = new int*[N];
  for(int i = 0; i < N; i++)
  {
    matrix[i] = new int[N + 1];
  }
}

Note that here you have to also manually release this memory

for(int i = 0; i < N; i++)
{
  delete[] matrix[i];
}
delete[] matrix;

Obviously this complicates things, so people advise against new/delete. A more convenient way to deal with the problem is to use a dynamic container class like the std::vector or a smart pointer class.

#include <iostream>
#include <vector>

int N;
int main()
{
  cin >> N;
  std::vector<std::vector<int>> matrix; // this made us a matrix with dimensions [0][0] so we need to reserve the dimensions
  for(int i = 0; i < N; i++)
  {
// this adds a row of size (N + 1)
    matrix.push_back(std::vector<int>(N+1));
  }
}

and it doesn't need manual deletion or anything like that and it would work similar to how an array would work so things like this

matrix[0][0] += 10;

still work.

Also note that there are more efficient ways to deal with the problem of multidimensional arrays.

vonamedog
  • 50
  • 5