0

I tried this but it is not working ! can any one help me please this is very important :(

#include <iostream>
using namespace std;
int a[100][100];

void read(int a[][100],int n)
{
  int i,j;
  for(i=0;i<n;i++)
       for(j=0;j<n;j++)
     cin>>a[i][j];
}

int main ()
{
    int n;
    cin>>n;
    int a[n][n];
   read(a,n);
}
10
  • 1
    int a[n][n]; is VLA and not standard C++. Commented May 5, 2017 at 9:31
  • You should switch the parameters. If you write int n before, you can use the value in the array size: void read(int n, int a[][n]) Commented May 5, 2017 at 9:32
  • You've conflicting declarations of a[][] Commented May 5, 2017 at 9:32
  • It's c++. Why don't you use std::vector? It's designed exactly for such cases. Commented May 5, 2017 at 9:33
  • @RohanKumar all a are in separate scopes, so there is no conflict. Commented May 5, 2017 at 9:34

2 Answers 2

2

The unclear syntax to pass array by reference is:

void read(int (&a)[100][100], int n)

resulting in

#include <iostream>

void read(int (&a)[100][100], int n)
{
  for(int i = 0; i < n; i++)
       for(int j = 0; j < n; j++)
           std::cin >> a[i][j];
}

int main ()
{
    int n;
    std::cin >> n;
    int a[100][100];
    read(a, n);
}

but you might prefer std::vector:

#include <iostream>
#include <vector>

void read(std::vector<std::vector<int>> &mat)
{
    for (auto& v : mat) {
        for (auto& e : v) {
            std::cin >> e;
        }
    }
}

int main ()
{
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> mat(n, std::vector<int>(n));
    read(mat);
}
Sign up to request clarification or add additional context in comments.

1 Comment

yeah I will try this
1

Since this is tagged C++. I'd like to suggest usage of std::vector. It's dynamic container which is very useful. You can resize it, clear it fill it with ease. Once you understand it's basic usage, they would come really handy in your future C++ development. I modified your code slightly:

#include <iostream>
#include <vector>
using namespace std;

void read(vector<vector<int> >& arr,int n)
{
  int i,j;
  for(i=0;i<n;i++)
       for(j=0;j<n;j++)
           cin>>arr[i][j];
}
int main ()
{
    int N;
    cin>>N;
    vector<vector<int> > arr(N, vector<int>(N));
    read(arr, N);
}

They have many advantages over the primitive arrays like they can be initialized easily, suppose you want to initialize all to zero :

vector<vector<int> > arr(N, vector<int>(N, 0));

You don't have to worry about adding the array size whenever passing in functions. vector can easily handle this:

for(i = 0; i < arr.size(); i++) {
  for(j = 0; j < arr[i].size(); j++) {
    // do stuff
  }
}

Moreover with the added methods of the Standard template library like fill, swap. Many operations can be handled easily.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.