How can I convert the index of 1D array into 2D array? I know how to convert a 2D array into 1D (i*the size of row+j). I want the opposite of that.
What you need to know is: How many columns should the 2D array have:
Lets say you have an array of 20 columns and 10 rows (array[20,10]):
int index = 47;
int numberOfColumns = 20;
int column = index % numberOfColumns;
int row = index / numberOfColumns;
// column == 7
// row == 2
You can just do the opposite. if n is the length of the row and x is index in 1D. You can index like
array[x/n][x%n]
Related
So I'm suppose to search my 2d array and find the locations (row and column number) where the search value occurred and store the row and column number in successive rows of a two dimension array
my instructions
"If the value is found in the data array, store the location (row and columns offset) where found in the data array into the locations array. Each time you find the value in the data array the location will be stored in the next row of the locations array. The row offset will be stored in column 0 and the column offset will be stored in column 1 of the locations array. Search the two dimension data array by rows."
This is where I'm confused
locations[Rsize][Rsize]=data[i][j];
Rsize++;
I don't know how to move the row and column from data array to the new locations array
locations array row 0 col 0 should have the data row
& locations array row 0 col 1 should have the data column
void find_value(short data[][15], short rows, short cols, short locations[] [2], short &Rsize, short searchValue)
{
short i,j;
Rsize=0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(data[i][j]==searchValue)
{
locations[Rsize][Rsize]=data[i][j];
Rsize++;
}
}
}
}
You are close. Remember, locations is just an array of arrays. You just want to append an array of size 2 to locations.
You want to change this:
locations[Rsize][Rsize]=data[i][j];
to this:
locations[Rsize][0] = i;
locations[Rsize][1] = j;
I am currently trying to fill up a 2D array with 16 values from 1-16. I want to fill up the array with non-repeating random values.
For example, if I have an array int array[4][4] how would I go through a loop filling the array while checking to make sure that another identical random value isn't placed into another location in the array?
int array[4][4];
int* p = &array[0][0];
std::iota(p, p+16, 1);
std::random_device r;
std::default_random_engine g(r());
std::shuffle(p, p+16, g);
Use int randomValue = rand() % 16 + 1; and store it in another helper 2D array for check purpose. you should be done.
Create a 1-d array, say arr of size 16, populate it with values from 1 to 16. Use shuffle on arr. Traverse through the array to fill the 2-D array, say arr2.
for(int i = 0; i < 16; i++){
arr2[i/4][i%4] = arr[i];
}
I have a 2D array:
A[65][4];
Is it possible to increment all the value of a specific column with looping through the row?
i.e. an equivalent command to:
for (int i = 0 ; i < 65 ; i++)
A[i][2]++;
Thanks
I initially had a 2D array. The results were taking time to get back the results. So, I converted the 2D array into 1D array but still there is not much improvement in speed of my program.
Here is my code:
for( counter1=0; counter1< size ; ++counter1)
{
y=buffer[counter1];
x=buffer[counter1+1];
IndexEntries index= OneDTable[x*256+y];
SingleTableEntries NewNextState=NewSingleTable[Next*blocksize+index];
Next=NewNextState.Next_State;
if(NewNextState.accept[index.serialnumber]=='1' )
{
return 1;
}
}
In my code above: OneDTable is a 1D array generated from a 2D array of 256 * 256 elements.
NewSingleTable is a 1D array generated from a 2D array of blocksize* (Total Next Elements).
Actually , I was expecting large speed gains after converting into 1D arrays. Is this the right way to extract value from a 1D array or certain improvements can be done to the above code?
More Details:
Both 2D arrays are of structure type:
Structure type of IndexEntries consists of:
int
int
Structure type of NewSingleTable consists of:
int
vector<char>
You could gain something changing from a vector of vector to a plain vector. E.g. from:
std::vector<std::vector<my_struct>> table(total_rows,
std::vector<my_struct>(total_columns,
my_struct()));
// do something with table[row][column]...
to
std::vector<my_struct> table(total_rows * total_columns);
// do something with table[row * total_columns + column]...
This because a vector of vector is not really a matrix and you lose data locality.
Changing from:
my_struct table[total_rows][total_columns];
to
my_struct table[total_rows * total_columns];
is worthless since the memory layout between the two is (usually) precisely the same.
The only difference is the semantic type of the array and the fact that you now have to implement the 2D element lookup yourself (of course changing from table[row * 256 + column] to table[row << 8 + column] is useless since any decent compiler will automatically perform this "optimization").
The 1D array could be a bit faster when you have to perform an operation on every element. This because of the simpler for loop:
for (unsigned row(0); row < total_rows; ++row)
for (unsigned column(0); column < total_columns; ++column)
// do something with table[row][column]
const unsigned stop(total_rows * total_columns);
for (unsigned i(0); i < stop; ++i)
// do something with table[i]
but this isn't your case.
As laune said in is comment, copying a NewSingleTable just to extract a couple of integers is bad:
SingleTableEntries NewNextState=NewSingleTable[Next*blocksize+index];
From your example it seems that a const reference should be enough:
...
const SingleTableEntries &NewNextState(NewSingleTable[Next * blocksize + index]);
if (NewNextState.accept[index.serialnumber] == '1' )
return 1;
Next = NewNextState.Next_State;
...
I have a 2D array called resolvedStress which can take m x n rows and columns. I would like to define a new variable called criticalData which is a 1D array containing just one row of resolvedStress. If I want to assign the 4th row to criticalData, would it be correct to write:
float* criticalData = &resolvedStress[4 * n];
I'm new to C++ so I'm not very confident yet!
Arrays are itself a static pointer, no need to use '&', again as you said you wana point to 4th row so it would be 3 and not 4 as array index starts from 0.
float* criticalData = resolvedStress[3];
Assuming your array is organised as follows:
[ row0 | row1 | row2 | .... ]
That is, a 1D array containing the rows of the matrix in order, then &resolvedStress[4 * n] will be a pointer to row4 - which is actually the 5th row of the array (gotta love zero indexing).
If you want to access fourth row of m X n array, you can access the each row as
resolvedStress[0] will point to first row of the 2D array.
resolvedStress[1] will point to second row of the 2D array.
...
resolvedStress[m-1] will point to mth row of the 2D array.
for your case it would be
float* criticalData = resolvedStress[3];