I have this vector of int from which I have to save the values to a 2D array.I though it was straight forward, but it seems that for i+j, when j becomes 0, the next integer to be saved is saved over the last one.They overlap. Can you please tell me how to fix it ? Here is the code:
vector<int> temp_table;//filled it in previous code, just for info
int** arr_table =new int* [number_of_states];
for(int i = 0; i < number_of_states; i++)
{
arr_table[i] = new int[alphabet.size()];
}
for(int i=0;i<number_of_states;i++)
{
for(int j=0;j<alphabet.size();j++)
{
arr_table[i][j]=temp_table.at(i+j);//This is where the overlapping occurs
//when j=0.How to fix it to save the correct data?
}
}
for(int i=0;i<number_of_states;i++)
{
for(int j=0;j<alphabet.size();j++)
{
cout<<arr_table[i][j]<<" ";
}
cout<<endl;
}
You want scale your i indexing by alphabet.size() to take into account the rows that have already been added:
for(int i=0;i<number_of_states;i++)
for(int j=0;j<alphabet.size();j++)
arr_table[i][j]=temp_table.at(i * alphabet.size() + j);
Related
I am trying to create a huge 2D array in c++ like this :
float array[1000000][3];
I have to store some values in this array which has 3 components. This will be done iteratively by two for loops of 1000 loops.
When I try to run this I get an error :
Segmentation fault
Here is a bit of my code :
int m=0,c=0;
float error;int count=0;
array<array<float, 1000000>, 3> errors;
for(int i=0; i<1000; i++){
for(int j=0; j<1000; j++){
error=distance(j+20,m,c,1000-matrix(1,j));
errors[count][0]=error;
errors[count][1]=m;
errors[count][2]=c;
c++;
count++;
}
m++;
c=0;
}
sort(errors.begin(), errors.end());
cout<<errors[0][0]<<endl<<errors[0][1]<<endl<<errors[0][2]<<endl;
The error continues even after commenting out the sort...
matrix(1,j) is a matrix and I am accessing its elements using this method.
I want minimum value of error and the set of values of m and c for which error is minimum.
Is there any way I can achieve this?
Thanks in advance.
You can use array to easily perform your task. Here what you can:
#include<array>
using namespace std;
Then, let create your data array:
const int N = 1000000;
const int M = 3;
array<array<float, N>, M> my_array;
You can fill this newly created array by doing:
for(int i=0; i < N; i++){
for(int j=0; j < M; j++){
my_array[i][j] = 0.0f; // Just for example
}
}
For more information on how to use array library, please see here
If you don't want to use array, you can also proceed as follows:
const int N = 10000000;
const int M = 3;
float *my_array
my_array = new float[N*M]; // give it a memory on the stack
if(my_array == nullptr){
cerr << "ERROR: Out of memory" << endl;
}
for(int i=0; i < N; i++){
for(int j=0; j < M; j++){
*(my_array + i*N + j) = 0.0f; // Just for example
}
}
Make sure you release the memory you acquired by doing:
delete [] my_array;
I'm trying to create two dimensional array and assign objects into it. What's important is, that I want to do this using POINTERS. I want to achieve it like this:
Create pointer which will point to array of pointers.
Having array pointers I create next 10 cells in memory for example to store there individual object.
This is my code:
I create basic pointer for storing the address for array of pointers:
SpecialPoint **arrayOfPointsOnTheMap = NULL;
Next I initialize this array:
arrayOfPointsOnTheMap = new SpecialPoint*[size];
And then create cells in memory:
for (int i = 0; i < szer; i++) {
arrayOfPointsOnTheMap[i] = new SpecialPoint[wys];
}
And for the end I want to assign object to this newly created array:
SpecialPoint *pontInTable;
for (int i = 0; i < szer; i++) {
pontInTable = arrayOfPointsOnTheMap[i];
for (int j = 0; j < wys; j++) {
pontInTable[j] = new SpecialPoint();
}
}
But I get error when trying to create new object int array. I'm a little confused about it. Can anyone help?
What you want to do in the assignment part is not very clear so I will answer based on my best guess. Let`s say your class is as follows:
class SpecialPoint
{
public:
int x;
int y;
SpecialPoint(int xx, int yy)
{
x=xx;
y=yy;
}
SpecialPoint()
{
x=0;
y=0;
}
};
Then you can use the following code to create and initialize your 2d array:
int size = 4;
int wys = 3;
SpecialPoint** arrayOfPointsOnTheMap = new SpecialPoint*[size];
for(int i = 0; i < size; i++)
{
arrayOfPointsOnTheMap[i] = new SpecialPoint[wys];
} // you have your array at that point
SpecialPoint fakePoint(5,6); // create a special point
for(int i=0; i < size; i++)
{
for(int j=0; j < wys; j++)
{ // Assign your special point instance to all the array cells.
arrayOfPointsOnTheMap[i][j] = fakePoint;
}
}
After you are done with the array, do not forget to clean memory using:
for(int i = 0; i < size; i++) {
delete [] arrayOfPointsOnTheMap[i];
}
delete [] arrayOfPointsOnTheMap;
Good luck!
I'm working on a school project written in c++. I am trying to generate an array of rgb colours based on user input, for a program that generates fractals(irrelevant).
I found the need to use a pointer to a 2D array. The pointer has a global scope because it needs to be accessed across multiple files, in multiple functions.
There is no problem with the declaration of the pointer. It is as such:
in "global.h"
int **ptr;
int palette[10][3];
//all the statements like #ifndef GLOBAL_H_INCLUDED, etc are present
in main.cpp
extern int **ptr;
extern int palette[10][3];
void gen_array();
int main()
{
//initialize values of palette
//palette is basically list of colors that the user selects for the
//desired blend of colors to appear on the screen
gen_array();
}
in void gen_array(), in main.cpp
void gen_array()
{
int i=0, size=0, size_arr=0;
//size is a variable helpful in calculating "size_arr", which is the size of the dynamic array to be initialized
while(palette[i][0]!=256)
{ //calculates "size" }
for(int i=0; i<size-1; i++)
{
//to find "size_arr"
}
ptr= new int*[size_arr]();
for(int i = 0; i < size_arr; ++i)
{
ptr[i] = new int[3];
}
//the following piece of code enter's values column-wise into the array
int s=0;
for(int i=0; i<size-1; i++)
{
for(int k=0; k<3; k++)
{
int x=-1, a=0, b=0;
a= palette[i][k] - palette[i+1][k];
b= palette[i][k];
if(a<0)
{
a= -a;
x=1;
}
for(int j=0; j<=a; j++, s++)
{
ptr[i][k] = b;
//cout<<*(*(ptr+i)+k)<<' ';
b= b+ x;
}
b= b-x;
for(int j=a+1; j<=diff[i]; j++, s++)
{
ptr[i][k] = b;
cout<<ptr[i][k]<<' ';
}
cout<<endl;
}
}
//this output's the array that the pointer points to, on the screen
for(int i=0; i<size_arr; i++)
{
for(int j=0; j<3; j++)
{
cout<<ptr[i][j]<<' ';
}
cout<<endl;
}
}
The problem is that, the data is getting generated and inputted in the array correctly [in the second-last for loop], but when outputting it [the last for loop] I'm getting junk values.
The data inputted [the correct values] as obtained from the cout statements [that are now commented] is this [printed column-wise] whereas the O/P given out is this [printed row-wise].
If it is required to be know, "size" (in this case) =2, size_arr=256 and palette[0]={0, 0, 255} and palette[1]={0,255,0}.
Can anybody point out what the problem is, and whether it has something to do with the pointer initialization?
Even without reading all the program code like
for(int j=0; j<=a; j++, s++)
{
ptr[i][k] = b;
//cout<<*(*(ptr+i)+k)<<' ';
b= b+ x;
}
looks clearly wrong. At each iteration you are writing to ptr[i][k] and the loop doesn't change ptr, i or k. This means that it will keep writing to the very same location during the loop.
Look at your int i in the loop you're using to fill int** ptr. If size = 2 then you're never going to advance ptr far enough to fill the array. Consider filling the array like this:
for(int i = 0; i < size_arr; ++i){//for every pixel in the main array
for(int k = 0; k < 3; ++k){//for every subpixel
int my_value = ...;//calculate the value for this entry
*(*(ptr+i)+k) = my_value;//assign
Better yet, use a struct named pixel containing Uint8 r,g,b and allocate as pixel* ptr = new pixel[size] and iterate over pixels and assign ptr[i].r ptr[i].g ptr[i].b
I am a beginner in C++ programming. I want to construct a dynamic 2D array with varying column size which is not known in advance.
For example, from an array A[9]={1,2,3,0,4,0,1,2,1}.
Each time an element with value = 0 is encountered, a new row is created. Basically when a 0 is encountered, row value is increased and column value is reset to 0. If a non-zero value is encountered, the row value is maintained and column value is increased. I want to also find out the row and column size for the 2D array. I also want to be able to store the column and row value in other variables.
Based on above example, my desired 2D array should look as follows.
1 2 3
0 4
0 1 2 1
The following is the program that I am working on. I don’t know how to relate the change of row and column values with my 2D array.
int nRows = 0;
int **X = new int *[nRows];
int *S = new int [nRows];
int nCols=-1;
int array[9]={1,2,3,0,4,0,1,2,1};
for(int i=0; i<9; i++)
{if (array[i]==0)
{nRows++;
nCols=0;}
else
nCols++;}
for(int i=0; i<nRows; i++)
{
X[i] = new int[nCols];
S[i] = nCols;
cout<<"\n"<<S[i];}
I have searched other related questions but to my understanding, all of them assign the column size for each row themselves instead of depending on other formulations. Please excuse my English and let me know if I could clarify my question.Thanks.
This is working good:
int givenArray[9]={1,2,3,0,4,0,1,2,1};
vector<vector<int>> my2dArray;
for(int i=0;i<9;i++) //iterate through all elements of the given array
{
if (i==0) //adding the first element
{
my2dArray.resize(my2dArray.size()+1);
my2dArray.back().push_back(givenArray[i]);
continue;
}
if (givenArray[i] == 0) //re-size if 0 is encountered
{
my2dArray.resize(my2dArray.size()+1);
}
my2dArray.back().push_back(givenArray[i]);
}
For printing the output in 2d manner :
for (auto vec1d : my2dArray){
{
for (auto i : vec1d){
cout << i << " ";
}
cout << endl;
}
}
Something like this should work:
typedef std::vector<int> Row;
typedef std::vector<Row> TwoDArray;
TwoDArray arr;
int array[9]={1,2,3,0,4,0,1,2,1};
Row row;
for(int i=0; i<9; i++)
{
if (array[i]==0)
{
arr.push_back(row);
row.clear();
row.push_back(0);
}
else
{
row.push_back(array[i]);
}
}
int array[9]={1,2,3,0,4,0,1,2,1};
vector<vector<int>> result;
for(int i=0; i < sizeof(array)/sizeof(array[0]); ++i)
{
if (array[i] == 0 || i == 0)
result.resize(result.size()+1);
result.back().push_back(array[i]);
}
In my code, I defined a 3D array to store the date on CUDA kernel.The code just like this:
if(k<2642){
double iCycle[100], jCycle[100];
int iCycleNum = 0, jCycleNum = 0;
for(double i=0; i<=1; i+=a, ++iCycleNum){
iCycle[iCycleNum] = i;
for(double j=0; j+i<=1; j+=c, ++jCycleNum){
jCycle[jCycleNum] = j;
[...]
r=(int)color[indexOutput];
g=(int)color[indexOutput+1];
b=(int)color[indexOutput+2];
d_RGB[k][iCycleNum][jCycleNum].x=r;//int3 (*d_RGB)[100][100]
d_RGB[k][iCycleNum][jCycleNum].y=g;
d_RGB[k][iCycleNum][jCycleNum].z=b;
}
}
}
In every cycle, there is an r,g,b. I want to store the r,g,b in d_RGB[k][iCycleNum][jCycleNum],then I need to pass them to the host. But in this case, every k has a different iCycleNum and jCycleNum, and I do not know the value of them, so the 3D array here is so awaste of space and may be it could bring some bugs. I wonder if it is a way to change the 3D array into a 1D one like this: d_RGB[k+iCycleNum*x+jCycleNum*x*y].
Sorry, my English is not so good to decribe it clearly, so if you can not get what I mean, please add a comment. Thank you.
Actually a "classic" 3D array is organized in the memory as a 1D array (since the memory is 1D oriented).
The Code:
int aiTest[5][5][5] = {0};
int* piTest = (int*)aiTest;
for (int i = 0; i < 125; i++)
{
piTest[i] = i;
}
does not make any memory violations - and the element aiTest[4][4][4] will have the value of 124.
So the answer: just cast it to the right type you need.
int arr[5][6][7];
int resultant_arr[210];
int count=0;
for(int i=0;i<5;i++)
{
for(int j=0;j<6;j++)
{
for(int k=0;k<7;k++)
{
if(count<210)
{
resultant_arr[count]=arr[i][j][k];
count++;
}
}
}
}