简体   繁体   中英

C++ Input from Keyboard

 #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

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...
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM