How can I assign an array to a fixed matrix index? - c++

Don't kill me: I'm a C++ noob.
Here's the code:
const int lengthA = 3;
const int lengthB = 4;
int main() {
double matrix[lengthA][lengthB];
double temp[lengthB];
for (int i = 0; i < lengthB; i++) {
temp[i] = i;
}
matrix[1] = temp;
}
How can I assign an array to a fixed index of a matrix that can contain it? Should I iterate each item on each (sequential) position? I hope I can simple past chunk of memory...

You don't directly assign raw arrays but rather copy their contents or deal with pointers to arrays
int main() {
double* matrix[lengthA]; // Array of pointers, each item may point to another array
double temp[lengthB]; // Caveat: you should use a different array per each row
for (int i = 0; i < lengthB; i++) {
temp[i] = i;
}
matrix[1] = temp;
}
Keep in mind that this is not a modern C++ way of doing things (where you could be better off using std::array or std::vector)

You can not, arrays are not assignable.
Here are three possible way to solve it:
Use std::array (or std::vector) instead
Copy the elements from one array to the other (either through std::copy, std::copy_n or std::memcpy)
Make matrix an array of pointers instead
I recommend std::array (or std::vector) first, copying second, and using pointers only as a last resort.

You can use double *matrix[lengthB]; instead of double matrix[lengthA][lengthB];

Related

Reserve 2D Vector in C++ and copy data from array

I hope to use vector to process the 2d array data obtained by calling a third-party library.
Although I can simply use the loop to assign values one by one, But I prefer to use methods such as insert and copy to deal with this.
I found that reserve doesn't seem to work here. So I used resize instead.
double **a = new double *[1024];
for (int i = 0; i < 1024; ++i) {
a[i] = new double[512];
}
std::vector<std::vector<double>> a_v;
a_v.resize(1024, std::vector<double>(512));
// Copy a -> a_v
I made these attempts:
// Not Working, just 0 in vector
for (int i = 0; i < 1024; ++i){
a_v[i].insert(a_v[i].end(), a[i], a[i] + 512);
}
Is there any good way to solve this problem.
For a 1D array I write like this:
double *b = new double[1024];
std::vector<double> b_v;
b_v.reserve(1024);
b_v.insert(b_v.end(), b, b + 1024);
If the size of the source array is fixed, it is strongly recommended to use std::array instead of std::vector. std::array has continuous memory layout for multidimensional structures, thus std::memcpy can be used for copy if the source array is also continuous in memory.
Look back to the original question. If you want to construct a std::vector<std::vector<double>> from the source array, use a single loop to construct 1D vectors from the source:
std::vector<std::vector<double>> a_v;
a_v.reserve(1024);
for (int i = 0; i < 1024; ++i) {
a_v.emplace_back(std::vector<double>(&(a[i][0]), &(a[i][512])));
}
If there is already a std::vector<std::vector<double>> with the proper size, and you literally just want to do a copy from the source, use the assign member function:
for (int i = 0; i < 1024; ++i) {
a_v[i].assign(&(a[i][0]), &(a[i][512]));
}
std::vector<std::vector<double>> a_v;
a_v.resize(1024, std::vector<double>(512));
is just
std::vector<std::vector<double>> a_v{1024, std::vector<double>(512)};
Unfortunately there is no vector constructor that takes over ownership of a C-style array. So you have to copy all 1024 * 512 doubles. And with the above definition of the vector you needlessly initialize all the doubles before you overwrite them.
You can do it with reserve so none of the double get initialized before you overwrite them and no vector gets copied or moved:
std::vector<std::vector<double>> a_v;
a_v.reserve(1024);
for (std::size_t i = 0; i < 1024; ++i) {
a_v.emplace_back();
std::vector<double> &b_v = a_v.back();
b_v.reserve(512);
b_v.insert(b_v.end(), a[i], a[i] + 512);
}

C++ Dynamic Array 1 dimension non dynamic

Been awhile since I've programmed in C++ however I have a slight problem that I'm trying to figure out.
Is it possible to make a 2 by 2 dynamic array where 1 dimension is not dynamic?
For example
array[2][Dynamic]?
It seems like a waste to make array[dynamic][dynamic] and when I only need to use the first [0][dynamic] and second [1][dynamic] values.
Should I use another data structure?
Thanks.
Arrays and pointers are basically equivalent, so you can achieve this with an array of pointers:
int* array[2];
array[0] = new int[x];
array[1] = new int[y];
You can still access it as you would multidimensional array:
array[0][x-1] = z;
This works in C++11:
std::array<std::vector<MyClass>,2> arr;
Or you could use a c-style array of vectors
std::vector<MyClass> arr[2];
Sure but it has to be dynamic in the first dimension.
Like this for instance
typedef int two_int_array[2];
two_int_array* a = new two_int_array[n];
for (int i = 0; i < n; ++i)
{
a[i][0] = 1;
a[i][1] = 2;
}
Of course the better way in general is to use vectors. Since you can't have a vector of arrays, a vector of structs might be better for your case.
struct two_int_struct
{
int value[2];
};
std::vector<two_int_struct> a(n);
for (int i = 0; i < n; ++i)
{
a[i].value[0] = 1;
a[i].value[1] = 2;
}

pushback data from vector to vector of map

i have created a map called select_p and vector of this map is called pts. i have stored data in a array and i want to pushbcak these data into my vector of map. i tried this by inserting value of array into new vector and then pushback into my map.but it is not working please help me to correct these codes? thanks
#include<iostream>
#include<cstdlib>
#include <map>
#include <vector>
using namespace std;
int main()
{
int M=7;
int N=6;
int i=0;
int * temp;
map<int,vector<int> > select_p;
vector<int>pts;
for (int m=0; m<M; m++)
{
for (int n=0; n<N; n++)
{
vector<int>id;
if (n==0 && m==5)
{
temp = new int[3,i+N,i+N+1,i+1];
unsigned ArraySize = sizeof(temp) / sizeof(int);
id.insert(id.begin(),temp[0], temp[ArraySize]);
select_p[i].push_back(id);
}
i++;
}
}
delete[] temp;
system("PAUSE");
return 0;
}
for (int m=0; m<M; m++) {
for (int n=0; n<N; n++) {
if (n==0 && m==5) {
Why are you looping when you only actually do anything for a single pair of values of m and n? The loops are completely useless here; you would get the same effect by just setting n = 0 and m = 5.
temp = new int[3,i+N,i+N+1,i+1];
Whatever you think this does, that's not what it does. This is equivalent to temp = new int[i+1];. The rest of the expression inside of the [] has no effect.
That said, you should not use new to create arrays in your program. Use std::vector; it is far easier to use correctly.
unsigned ArraySize = sizeof(temp) / sizeof(int);
This does not work. When you dynamically allocate an array, you are responsible for keeping track of how many elements are in it. Given a pointer to a dynamically allocated array (like temp here) there is no way to determine the number of elements in the array.
What you have is equivalent to sizeof(int*) / sizeof(int), which is not going to do what you expect.
id.insert(id.begin(),temp[0], temp[ArraySize]);
std::vector::insert takes a range of iterators: you have provided it with two values. Presumably you want to use temp, which points to the initial element of the dynamically allocated array, and temp + i + 1, which points one past the end of the array. That said, since you haven't set the values of the elements in the array, you are copying uninitialized memory, which probably isn't what you mean to do.
select_p[i].push_back(id);
select_p[i] is a std::vector<int>. std::vector<int>::push_back() takes a single int that is appended to the sequence. Presumably you just mean to use assignment to assign id to select_p[i].
You should get a good introductory C++ book if you want to learn to program in C++. I am sorry to say that your program is nonsensical.

Initialization of 2D array with dynamic number of rows and fixed number of columns. C++

I'm having problem with creating my 2D dynamic array in C++. I want it to have dynamic number (e.g. numR) of "rows" and fixed (e.g. 2) number of "columns".
I tried doing it like this:
const numC = 2;
int numR;
numR = 10;
double *myArray[numC];
myArray = new double[numR];
Unfortunately, it doesn't work. Is it possible to do it in such a way?
Of course I could use double **myArray and initialize it as if both dimensions are dynamic (with numC used as limiter in loop) but I would like to avoid it if possible.
Thanks in advance.
Is it possible to do it in such a way?
Yes:
double (*myArray)[numC] = new double[numR][numC];
// ...
delete[] myArray;
This may look a little unusual, but 5.3.4 ยง5 clearly states:
the type of new int[i][10] is int (*)[10]
Note that many programmers are not familiar with C declarator syntax and will not understand this code. Also, manual dynamic allocation is not exception safe. For these reaons, a vector of arrays is better:
#include <vector>
#include <array>
std::vector<std::array<double, numC> > vec(numR);
// ...
// no manual cleanup necessary
Replace std::array with std::tr1::array or boost::array, depending on your compiler.
Why not use a std::vector, and take advantage of its constructor:
std::vector<std::vector<int> > my2Darray(2, std::vector<int>(10));
my2Darray[0][0] = 2;
There needs to be a loop since you need to create an array for every column.
I think what you're after is:
double *myArray[numC];
for (int i = 0; i < numC; i++) {
myArray[i] = new double[numR];
}
// some code...
// Cleanup:
for (int i = 0; i < numC; i++) {
delete [] myArray[i];
}
This declares an array of pointers (to double) with numC elements, then creates an array of doubles with numR elements for each column in myArray. Don't forget to release the memory when you're done with it or you'll have memory leaks.
Your indexes should be row, then column.
double** myArray = new double*[numR];
for( unsigned int i = 0; i < numR; i++ ) {
myArray[i] = new double[numC];
}
Access row 2, column 5:
myArray[2][5];

C++ two dimensional arrays with pointers

I have a problem with two dimensional arrays :( I feel very stupid and Visual C does not help me :( and I also think that my mistake is very stupid but still I can't find it :( I have this code:
double matrix[100][100]; //which is full with a matrix 3x4
double nVector[10000]; // for negative doubles
//I wanted to see if there are negative doubles in each row and column
//and I want this to happen with function
And this is my function:
double* negativeVector(double*nVector, double*fromVector, int m, int n){
int position = 0;
double *myNegArray = nVector;
double *myMatrix = fromVector;
for(int i = 0; i < m*n; i++)
if(*(*(myMatrix+i)) < 0){
*(myNegArray+position) = *(*(myMatrix+i));
position++;
}
return myNegArray;
}
//for double*nVector I'm passing nVector
//for double*fromVector I'm passing *matrix
Visual C tells me that I have an error C2100: illegal indirection here: *(*(myMatrix+i)) I hope someone can help me (happy)
Thanks in advance!
*(*(myMatrix+i)) is wrong. This is a common mistake.
2D matrix does not create an array of pointers which you can access this way. It is a different structure. Even though an array is a pointer, 2D array is not a pointer to pointer, and it cannot be dereferrenced twice. Nor you have any other way to access element at coordinates (x,y) without knowing the layout in memory, because pointers to every line are nowhere to be found. For instance, char **argv parameter of main() is not a 2D array. This is an array of pointers to arrays, which is something else.
There're two ways to fix it.
One is replace
double *myMatrix = fromVector;
by
double *myMatrix[100] = (appropriate cast)fromVector;
and index it as myMatrix[i/n][i%n]
But then remember that 100 is a constant expression, and it cannot be passed as a parameter. Alternatively, you can implement the indexing operation yourself:
Pass additional parameter: matrix line size (100)
Instead of *(*(myMatrix+i)), write:
int row = i/n;
int col = i%n;
*(myMatrix+row*line_size+col) is your element.
first you might wanna start a small struct like
struct tmp {
bool negative;
double value;
};
and make your own way up to the
tmp *myvars [100][100];
.
instead try using that struct and try the std::vectors instead of arrays if that's possible then try using pointers on decalring the variable "1 time only" when declaring the variable as i said above
then pass arguments
( tmp *mystructpointer )
mystructpointer->.......
access your matrix directly ... peice of cake :D
If you are passing *matrix, you are actually passing a double[100] (an array of 100 doubles), that happens to be passed as a pointer to its first element. If you advance further than those 100 doubles using i added to that pointer, you advance into the next array of 100 doubles, since the 100 arrays of 100 doubles are stored next to each other.
Background: A multi-dimensional array is an array whose element type is itself an array. An array like double a[100][100]; can be declared equivalently as typedef double aT[100]; aT a[100];. If you use an array like a pointer, a temporary pointer is created to the array's first element (which might be an array). The * operator is such an operation, and doing *a creates a pointer of type double(*)[100] (which is a pointer to an array of 100 doubles), and dereferences it. So what you end up with *matrix is a double[100]. Passing it to the negativeVector function will create a pointer to its first element, which is of type double*.
Your pointer parameters point to the start of each of two arrays of 100 doubles each. So you should rewrite the function as
double* negativeVector(double*nVector, double*fromVector, int m, int n){
int position = 0;
double *myNegArray = nVector;
double *myMatrix = fromVector;
for(int i = 0; i < m*n; i++)
if(*(myMatrix + i) < 0){
*(myNegArray + position) = *(myMatrix + i);
position++;
}
return myNegArray;
}
Notice that since your i iterates beyond the first of the 100 arrays stored in the 2d array, you will formally not be correct with this. But as it happens those arrays must be allocated next to each other, it will work in practice (and in fact, is recommended as a good enough work around for passing multi-dimensional arrays around as pointers to their first scalar element).
I have no clue why you are copying the arrays twice (once in the parameters of the function and a second time by declaring some new arrays)... You should also think of using the STL... std::vector will make the your life way easier ;)
double* negativeVector(double*nVector, double*fromVector, int m, int n){
int position = 0;
double *myNegArray = nVector;
double *myMatrix = fromVector;
for(int i = 0; i < m*n; i++)
if(*((myMatrix+i)) < 0){
*(myNegArray+position) = *((myMatrix+i));
position++;
}
return myNegArray;
}
is that homework? some templates - just for fun ;-)
double matrix[100][100];
double nVector[10000];
template< const int m, const int n >
double* negativeVector( double* myNegArray, const double (&myMatrix)[m][n] )
{
int position = 0;
for( int i = 0; i < m; ++i )
{
for( int j = 0; j < n; ++j )
{
const double value = myMatrix[ i ][ j ];
if ( value < 0 )
{
myNegArray[ position ] = value;
++position;
}
}
}
return myNegArray;
}
int main()
{
//...initialize matrix here...
negativeVector( nVector, matrix );
}
Perhaps rewrite this using std::vector to increase readability? (#):
#include <vector>
std::vector< std::vector<double> > matrix; //which is full with a matrix 3x4
std::vector<double> row;
row.resize(100,0);
matrix.resize(100,row);
std::vector<double> nVector; // for negative doubles, no size, we'll "push_back"
//I wanted to see if there are negative doubles in each row and column
//and I want this to happen with function
This is the stl enabled version of the function:
//I'm returning void because nvector contains the result,
//so I don't feel the need to return anything. vectors contain their
//own size so n and m are also not needed. Alsom pass in references
void negativeVector(std::vector<double>& nVector,
std::vector< std::vector<double> >& fromVector){
nVector.clear();
int i,j;
for(i = 0; i < fromVector.size(); i++) {
for(j = 0; j < fromVector[i].size(); j++) {
if(fromVector[i][j] < 0){
nVector.push_back(fromVector[i][j]);
}
}
}
}
call with:
negativeVector(nVector, matrix);
Once the function completes, nVector contains all negative numbers in matrix.
Read more about std::vector here.
(#) for people like me who are too lazy/stupid to comprehend code containing pointers.
Take a look at C++ Faq site:
How do I allocate multidimensional arrays using new?
link
And read until point [16.20] summarize all the answers you are getting and at the end you get a very useful Matrix template class.
Have a good read.