C++ create array with dynamic size - c++

How can I create a array with dinamic size like this:
int sentLen = sentences.size();
double a[sentLen][sentLen];
for (int i = 0; i < sentLen; i++)
{
for (int j = 0; j < sentLen; j++)
{
a[i][j] = somefunction(i, j);
}
}
My research led me to malloc which isn't recommended or other too complicated methods. After I realised that size must be constant, I tried using unordered_map, and I have tried the following:
std::unordered_map <int, int, double> a;
for (int i = 0; i < sentLen; i++)
{
for (int j = 0; j < sentLen; j++)
{
a.insert({ i, j, somefunc(i, j) });
}
}
but still unsuccessful.

You don't really want to use arrays.
std::vector<std::vector<double>> a{
sentLen, std::vector<double>{ sentLen, 0.0 } };
for (int i = 0; i < sentLen; ++i)
{
for (int j = 0; j < sentLen; ++j)
{
a[i][j] = somefunc(i, j);
}
}

You're getting an error because you can't use variables as static array sizes. They must be known at compile time. You have to allocate dynamically or use a vector instead.

Related

Iterate over vectors with n and n*n elements

I have this Google test assertions
ASSERT_FLOAT_EQ(longerVector[0], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[1], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[2], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[3], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[4], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[5], shorterVector[2]);
ASSERT_FLOAT_EQ(longerVector[6], shorterVector[0]);
ASSERT_FLOAT_EQ(longerVector[7], shorterVector[1]);
ASSERT_FLOAT_EQ(longerVector[8], shorterVector[2]);
as you can see, there are two vectors, shorter with n elements and longer with n*n elements (in this case 3 and 9 respectively).
How could I create two nested for loops, which would make my code shorter and simpler?
I tried for example this but it failed. I understand why but I was not able to come up with anything better.
for(size_t i = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector(i*j), shorterVector(j);
}
}
Thank you very much for any help and I apologize for maybe a dumb question.
Use this instead
for(size_t i = 0; i < longerVector.size(); i++)
{
ASSERT_FLOAT_EQ(longerVector[i], shorterVector[i%3]);
}
for(size_t i = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector[i*shorterVector.size() + j], shorterVector[j]);
}
}
You should assert before that shorterVector.size() * shorterVector.size() == longerVector.size().
If you do not want to have the multiplication in the inner loop body you can do:
for(size_t i = 0, k = 0; i < shorterVector.size(); i++)
{
for(size_t j = 0; j < shorterVector.size(); j++)
{
ASSERT_FLOAT_EQ(longerVector[k++], shorterVector[j]);
}
}

How do I create a function for adding two matrices? What will it return?

So for my homework I have to create a function for adding two matrices, the numbers are given by the user, and I think I have the function somewhat down, I just have no idea what to return because I know you can't return an array, so I tried making a variable to set it equal to but it's not working? Can someone help me out with this?
This is the part I'm stuck on:
int add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols)
{
int matrix_total = c[0][0];
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < Anum_cols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
c[i][j] = matrix_total;
}
return matrix_total;
}
The last j in the c array is underlined with red also in visual studio.
Your code already does what it needs, it stores the resulting matrix in c, which, since arrays are passed as pointers, will still contain the updated values when add_matrix() returns. You just have extra code in the function, which should really not be needed to complete the matrix add operation:
void add_matrix(int a[][10], int b[][10], int c[][10], int Anum_rows, int Anum_cols) {
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < Anum_cols; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
Good point about Anum_cols. There is no need to pass in the number of columns when it is specified in the argument definition and it could lead to future errors.
#define NUM_COLS 10
void add_matrix(int a[][NUM_COLS], int b[][NUM_COLS], int c[][NUM_COLS], int Anum_rows)
{
for (int i = 0; i < Anum_rows; ++i) {
for (int j = 0; j < NUM_COLS; ++j) {
c[i][j] = a[i][j] + b[i][j];
}
}
}

Matrix multiplication using 2d vector

Following is my code:
void matrix(int rowsInA, int columnsInA, int columnsInB){
std::vector< vector<int> > a;
std::vector< vector<int> > b;
for (int i = 0; i < rowsInA; i++) {
vector<int> myRow(1);
a.push_back(myRow);
for (int j = 0; j < columnsInA-1; j++) {
int x = rand() % 100;;
myRow.push_back(x);
}
}
for (int i = 0; i < rowsInA; i++) {
vector<int> myRow(1);
b.push_back(myRow);
for (int j = 0; j < columnsInB-1; j++) {
int x = rand() % 100;;
myRow.push_back(x);
}
}
std::vector< vector<int> > c = multiply(a, b);
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
cout << c[i][j];
}
}
}
std::vector< vector<int> > multiply(std::vector< vector<int> > a , std::vector< vector<int> > b) {
int rowsInA = 9;
int columnsInA = 9; // same as rows in B
int columnsInB = 9;
std::vector< vector<int> > d;
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
vector<int> myRow;
myRow.push_back(a[i][0]);
d.push_back(myRow);
for (int k = 0; k < columnsInA; k++) {
myRow.push_back(myRow[i]+ a[i][k] * b[k][j]);//error here
}
}
}
return d;
}
first function matrix() creates two vector within a vector and assigns random values to it and then call multiply in which vectors are multiplied.
It is giving vector subscript out of range error
Your code:
for (int i = 0; i < rowsInA; i++) {
vector<int> myRow(1);
a.push_back(myRow);
for (int j = 0; j < columnsInA-1; j++) {
int x = rand() % 100;;
myRow.push_back(x);
}
}
Constructs a single element vector, pushes it on to a, appends some
values to your temporary, and then throws it away. C++ is not Java or C#. The vector at the back of a is not changed by changes to myRow.
You also seem to be putting a fixed value as the first element of myRow, and
then appending randomness to it. Is that what you meant?
What you need is:
for (int i = 0; i < rowsInA; i++) {
vector<int> myRow;
myRow.reserve(columnsInA);
myRow.push_back(0); // First element fixed.
for (int j = 1; j < columnsInA; j++) {
const int x = rand() % 100;;
myRow.push_back(x);
}
a.push_back(myRow);
}
Alternatively, making all the values random, and working directly with the vector:
a.reserve(rowsInA);
for (int i = 0; i < rowsInA; i++) {
a.push_back( {} );
a.back().reserve(columnsInA);
for (int j = 0; j < columnsInA; j++) {
const int x = rand() % 100;;
a.back().push_back(x);
}
}
You have a similar problem with initializing B, and your multiply loop needs to accumulate a[i][k] * b[k][j] into a temporary sum, then push that onto myRow, and finally push myRow onto d.
Finally, when you have got this working, look up how to write a Matrix class that stores all the data in a vector with rows*cols elements and then uses an indexing function to refer to it. Your cache will thank you.

How do I copy the elements of a 2D array onto a 1D vector?

So i keep trying to transfer the elements but it keeps giving me repeated elements, it fails to properly copy the 2D array onto a 1D vector
// This was one of my attempts
vector<int> rando(int rowsize, int columnsize)
{
int elements = rowsize*columnsize;
vector<int> x(elements);
int matrix[100][100];
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
srand((int)time(0));
matrix[i][j]= -10 + rand() % 21;
for(int n=0; n < elements; n++)
x[n]=matrix[i][j];
}
// Ive also tried this
for(int n=0; n < elements; n++)
{
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
x[n]=matrix[i][j];
}
}
}
}
return x;
}
Why do you want to store data into the matrix first and copy it into the vector afterwards? Use the vector from the start.
std::vector<int> rando(std::size_t rowsize, std::size_t columnsize)
{
std::vector<int> v(rowsize*columnsize);
std::mt19937 mt{std::random_device{}()};
std::uniform_int_distribution<int> rand_dist(-10, 10);
for (auto & e : v) e = rand_dist(mt);
return v;
}
If you want to transfer data from a matrix into a vector you must calculate the proper index or just increment a single variable as Thomas Matthews suggests.
constexpr std::size_t n = 100, m = 100;
int matrix[n][m];
// do stuff with matrix
std::vector<int> v(n*m);
for (std::size_t i=0; i<n; ++i)
{
for (std::size_t j=0; j<m; ++j)
{
v[i*m + j] = matrix[i][j];
}
}
THe general copy should loop through the 2 dimensions, and just increment the target index at each iteration (no third nested loop):
int n=0;
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
...
x[n++]=matrix[i][j]; // not in an additional for loop !!
}
} // end of initialisation of matrix
If your matrix is a 2D array (i.e. contiguous elements) you can also take the following shortcut using <algorithm>:
copy (reinterpret_cast<int*>(matrix), reinterpret_cast<int*>(matrix)+elements, x.begin());
Try this:
unsigned int destination_index = 0;
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
x[destination_index++]=matrix[i][j];
}
}
The destination index is incremented after each assignment to a new slot.
No need for a 3rd loop.
It is enough to use two loops.
For example
srand((int)time(0));
for(int i = 0; i < rowsize; i++)
{
for(int j = 0; j < columnsize; j++)
{
matrix[i][j]= -10 + rand() % 21;
x[i * columnsize + j] = matrix[i][j];
}
}
In general if you have a two-dimensional array and want to copy nRows and nCols of each row elements in a vector then you can use standard algorithm std::copy declared in header <algorithm>
For example
auto it = x.begin();
for ( int i = 0; i < nRows; i++ )
{
it = std::copy( matrix[i], matrix[i] + nCols, it );
}

Trouble initializing multidimensional array inside class

So I count with the following excerpt of code:
template<class DT>
class AdjMat
{
protected:
DT** myMatrix;
int noOfNodes;
int noOfEdges;
public:
AdjMat(int _noOfNodes, int _noOfEdges);
//Destructor, constructors and other methods
};
template<class DT>
AdjMat<DT> :: AdjMat(int _noOfNodes, int _noOfEdges)
{
myMatrix = new DT*[_noOfNodes+1];
for(int i = 0; i < _noOfNodes+1; i++)
{
myMatrix[i] = new DT[_noOfNodes+1];
for(int j = 0; j < noOfNodes+1; j++)
myMatrix[i][j] = 0;
}
noOfNodes = _noOfNodes;
noOfEdges = _noOfEdges;
}
int main()
{
adjMat<int> m(5, 9);
}
The problem occurs in the constructor. The array doesn't seem to be initialized at all and much less the inner arrays, I have tried to go in different ways, but it won't initialize anything. Could anyone give me a hint of whatever I am doing wrong?
The error is in this line.
for(int j = 0; j < noOfNodes+1; j++)
noOfNodes has not been initialized yet. You probably meant:
for(int j = 0; j < _noOfNodes+1; j++)
You can avoid such errors by following a safer practice. Initialize as many members as you can in the initializer list.
template<class DT>
AdjMat<DT> :: AdjMat(int _noOfNodes, int _noOfEdges) : noOfNodes(_noOfNodes),
noOfEdges(_noOfEdges),
myMatrix(new DT*[_noOfNodes+1])
{
for(int i = 0; i < noOfNodes+1; i++)
{
myMatrix[i] = new DT[noOfNodes+1];
for(int j = 0; j < noOfNodes+1; j++)
myMatrix[i][j] = 0;
}
}