1

I'm pretty new to coding in c++ so I apologize if this question has a very apparent simple answer. I'm trying to create a matrix of random numbers from -1 to 1. These are the two functions I am using:

#include <iostream>
#include "matrix_fill_random.cpp"
using namespace std;

int main(int argc, int argv[]){

int n1, n2, n3;

if (argc != 4) {
    cerr << "This program requires 3 argument!" <<endl;
    return 1;
}
else{
    n1 = argv[1];
    n2 = argv[2];
    n3 = argv[3];

    double** a;

    matrix_fill_random(n1, n2, a);

    return 0;
}
}

and

#include <iostream>
using namespace std;

int matrix_fill_random(int n1, int n2, double** a){
for (int i=0; i<n1; i++){
    for (int j=0; j<n2; j++){
        double num = rand() % 2 - 1;
        a[i][j]=num;
    }
}
return 0;
}

Ultimately I'm trying to create two matrices and then multiply them together so n1, n2, and n3 represent the rows and columns of two matrices, but that isn't all too important right now. I think the error might be in how I declare my variables or pass them to other functions but I'm not entirely sure.

I feel like if I can understand the principle of creating one of the matrices then that would translate to the other functions I need to use.

5
  • 1
    matrix_fill_random is assigning values to a[i][j] but you haven't allocated any memory for a. Commented Sep 29, 2013 at 22:08
  • Also, the second argument to main is char *argv[] not int argv[]. This will cause your n1 = argv[1] to fail compilation (which it should since it's invalid). You need to convert argv[1] to int. Likewise for the other n's. Commented Sep 29, 2013 at 22:19
  • If using c++ do not use plain c-arrays (unless you have to). Prefer std::vector. Commented Sep 29, 2013 at 22:20
  • no one saw this ? #include "matrix_fill_random.cpp" should be .h or .hpp Commented Sep 29, 2013 at 22:20
  • @pyCthon I saw it, and it works (functions, not generating an error). It just defies convention. Commented Sep 29, 2013 at 22:23

4 Answers 4

1
double** a;

You haven't allocated memory for the pointer, so you're getting Undefined Behavior each time you dereference it using operator [].

You should allocate a once before passing it through the function...

double** a = new double*[n1];

and once more inside the for loop of the function:

for (int i = 0; i < n1; i++)
{
    a[i] = new double[n2];
    for (int j = 0; j < n2; j++)
    {
        double num = rand() % 2 - 1;
        a[i][j] = num;
    }
}

But don't forget to delete[] the pointer once you're done using it. You should delete the other pointers allocated inside the for loop as well.

Of course, this can all be avoided by using std::vector. Here is your program fitted with the Standard Library:

std::vector<std::vector<double>> a(n1, std::vector<double>(n2));

int matrix_fill_random(std::vector<std::vector<double>> a)
{
    for (int i = 0; i < a.size(); ++i)
    {
        for (int j = 0; j < a[i].size(); ++j)
        {
            double num = rand() % 2 - 1;
            a[i][j] = num;
        }
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Ok so I understand how this allocates memory for a so I can assign things to it and I understand how yo did that before it goes into matrix_fill_random and in the loops. However I am getting an error at the line double** a = new double[n1]; that says cannot convert ‘double*’ to ‘double**’ in initialization. Also I haven't seen the std::vector before, can you point me to a place where I could learn more about it?
@CalvinMiller Oops! That should have been double** a = new double*[n1]. Thanks for pointing that out. :)
Yes that seems to have fixed it. Thanks for the help
Yea sorry kind of new around here.
1

Allocate the memory for a. You haven't allocate the memory for a. Chane the statement double** a; to

  double** a = new double[n1];

and change the loop as follows

  for (int i = 0; i < n1; i++)
  {
    //Every row will be of size = number of columns.
    a[i] = new double[n2];
    for (int j = 0; j < n2; j++)
    {
        double num = rand() % 2 - 1;
        a[i][j] = num;
    }
  } 

Comments

0
double** a;
matrix_fill_random(n1, n2, a);

passes uninitialized pointer a to the function, which tries to initialize elements of two-dimensional array:

a[i][j]=num;

which invokes an undefined behavior. The simplest solution would be to allocate a single block of memory that will be big enough to hold the matrix:

double* a = new double[rows * cols];
matrix_fill_random(n1, n2, a);
delete[] a;
a = NULL;

...

// accessing element a[i][j] in the function's body:
a[i*cols + j] = num;

but most reasonable solution would be using std::vector instead of C-style arrays.

Comments

0

You need to allocate memory to a before passing it to matrix_fill_random().

double** a = new double[n1];

Since you're using C++ though, you should consider using a vector or other template container.

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.