I am currently using the following code to get the current row and column of an element in a 2D int array:
const int num_rows = 5;
const int num_columns = 7;
int a[num_rows][num_columns];
int *p = &a[2][4];
int row = (p - &a[0][0]) / num_columns;
int col = (p - &a[row][0]);
This works fine but now I need to change the code to take the number of rows and columns as a parameter. As far as I know, this means I need to create the 2D array dynamically:
int** ary = new int*[sizeX];
for(int i = 0; i < sizeX; ++i)
ary[i] = new int[sizeY];
If I create the 2D array this way, the above code to find the row/column breaks. What can I do?
int *ary = new int [sizeX * sizeY]; // allocate memory for the 2d array
for(int i = 0; i < sizeX; ++I)
for(j = 0; j < sizeY; ++j)
ary[i * sizeY + j] = 0;
// to get the value of collumn c and row r: use ary[c * sizeY + r];
int row = (p - &a[0][0]) / num_columns;
int col = (p - &a[row][0]);
It's a very good idea to stay way from pointer arithmetic. Don't do this! You are essentially subtracting the pointer and divide that number by num_columns, and that number will be random.
If you want to get the row/column of an element, search the array one element at a time.
for(int row=0; row<num_rows; ++row) {
for(int column=0; column<num_columns; ++column) {
if (a[row][column] == element) {
// we got it!
}
}
}
Related
I have a dynamically allocated 2d array and would like to loop through it with pointer arithmetic because I won't know the number of rows and number of cols before runtime.
I know how to do this with a 1d array:
int *arr = new int[size];
and to loop through it:
for (int *i = arr; i < arr + arr.size(); i++){
*i = 20; //sets all elements to 20
}
However, it's at the 2d level that I get stuck.
Here's what I have so far:
int **arr = new int *[row];
for(int i = 0; i<row; i++)
arr[i] = new int[col];
To loop through all values:
for(int **i=arr; i < arr + row; i++){
for(int *j=*i; j < j + col; j++){
*j = 20; // set all values to 20
}
}
The second loop is obviously incorrect, I just don't know what else to try.
You should do the same thing to j as you did to i.
for(int **i=arr; i < arr + row; i++){
for(int *j=*i; j < *i + col; j++){
*j = 20; // set all values to 20
}
}
I have a matrix of 1*5
I defined:
int **mat = new int*[5]; // define the matrix
int* ptr = *mat;
ptr++ gives me the pointer of the next row of the matrix.
I want to get the pointer of the second element of the first row (the location of [0][1]).
how can I do it?
C and C++ have no multi-dimensional arrays. So to do this, you'll have to do extra allocations:
First, allocate your memory:
int rows = 1;
int columns = 5;
int** mat = new int*[columns];
for(int i = 0; i < columns; ++i)
{
mat[i] = new int[rows];
}
Index your matrix:
int x = 1; // Column number, x-movement
int y = 0; // Row number, y-movement
//mat[x][y] = ???;
Don't forget to free memory that you allocated:
for(int i = 0; i < columns; ++i)
{
delete [] mat[i];
}
delete [] mat;
Or, use std::vector. It's much easier to use and understand:
#include <vector>
int rows = 1;
int columns = 5;
std::vector<std::vector<int > > mat = std::vector<std::vector<int > >();
mat.resize(columns);
for(int i = 0; i < 5; ++i)
{
mat[i].resize(rows);
}
//mat[1][0] = 100;
IN MY CODE IT WORKED LIKE THIS(the previous code has a problem for #columns>3 and #rows>3 especially for double precision vectors
#include vector
int rows=1;
int columns=5;
std::vector<std::vector<int > > mat = std::vector<std::vector<int > >();
mat.resize(rows);
for(int i = 0; i <rows; ++i)
{
mat[i].resize(columns)
}
I have a float** array that contains num_rows rows and num_cols columns. I'd like to determine the number of occurrences of every number between 0-9 columnwise. To do this, I thought of using another 2D array of size [10][num_cols], so that for each column the number corresponding to an element is the number of occurrences of that number in the original table.
Example: if the original table contains 1 2 3 1 1 in the fifth column, then in the second column, the values should be like: 1-> 3, 2 -> 1, 3 -> 1.
I tried using the function as follows, but it gives me a pointer error. I tried using vectors but that too brings no luck.
int ** attribute_count(float * * input, int row_num, int col_num) {
int ** arr_2 = new int * [10];
int * arr = new int[10 * col_num];
int counter = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < col_num; j++) {
arr_2[i][j] = 0;
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < col_num; j++) {
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
EDIT:
I tried your suggestions. The new code is:
int** attribute_count(float** input, int row_num, int col_num) {
int** arr_2 = new int* [10];
int* arr = new int[10 * col_num];
int counter = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < col_num; j++) {
arr_2[i] = new int[col_num];
}
}
for (int i = 0; i < 11; i++) {
for (int j = 0; j < col_num; j++) {
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
This still gives me memory errors. The function is being called in the .cpp like this:
int** attr = attribute_count(training_data, 10, num_cols_train);
cout<<attr[5][1];
Any idea what I'm doing wrong even now?
I think your problem is in incorrect allocation of the 2D array. Try
int ** arr_2 = new int* [row_num];
for (int i = 0; i < row_num; i++)
arr_2[i] = new int[col_num];
You've only allocated one dimension of arr_2. You need to loop through and allocate an array of ints on each one to get the 2nd dimension.
EDIT: Also, what's up with arr? You allocate it, never use it, don't return it, and don't deallocate it. That's how we spell memory leak.
arr_2 is defined and allocated as an array of pointers to int, but you don't actually assign/allocate those pointers.
Here's a stab at correcting your code - however I'm not convinced you have rows and columns the right way around...
int ** attribute_count(float ** input, int row_num, int col_num)
{
int ** arr_2 = new int * [10];
for (int i = 0; i < 10; i++)
{
arr_2[i] = new int[col_num];
for(int j = 0 ; j < col_num ; j++)
{
arr_2[i][j] = 0;
}
}
for (int i = 0; i < row_num; i++)
{
for (int j = 0; j < col_num; j++)
{
int temp = input[i][j];
arr_2[temp][j]++;
}
}
return arr_2;
}
I would like to get one dimensional array from my double int pointer. The memory for pointer is allocated like this :
listOfTxtsContents = new int *[ROWS];
for( int i = 0 ; i < ROWS ; i++ )
{
listOfTxtsContents[i] = new int[COLUMNS];
for(int j = 0; j < COLUMNS; ++j)
listOfTxtsContents[i][j] = 0;
}
Then each row contains some data where I would like to put it in the array in such a way so that the 1D array will be filled with consecutive rows.
Pretty sure something like this should work:
array[ROWS * COLUMNS];
for(size_t i = 0; i < ROWS; ++i)
memcpy(array + i * COLUMNS, listOfTxtsContents[i], COLUMNS * sizeof(int));
Allocate a 1D array of ints of size ROWS*COLUMNS; assign each listOfTxtsContents[i][j] to element i*COLUMNS+j.
there is a topic about this subject which is working with arrays but I can't make it work with matrices.
(Topic: C++ array size dependent on function parameter causes compile errors)
In summary:
long rows, columns;
cin >> rows >> columns;
char *a = new char [rows];
compiles great in visual studio, but:
char **a = new char[rows][columns];
or
char *a[] = new char[rows][columns];
or
char *a[] = new char[rows][columns]();
or
char **a = new char[rows][columns]();
don't compile at all.
Any help? Thank you
The array-new operator only allocates 1-dimensional arrays. There are different solutions, depending on what sort of array structure you want. For dimensions only known at runtime, you can either create an array of arrays:
char **a = new char*[rows];
for (int i = 0; i < rows; ++i) {
a[i] = new char[columns];
}
or an array of elements with an associated array of pointers to the first element of each row (requiring just two hits to the memory allocator):
char *a = new char[rows*columns];
char **a = new char*[rows];
for (int i = 0; i < rows; ++i) {
a[i] = a + i*columns;
}
Either one will let you access matrix elements via a[row][column].
An alternate solution is to just use the one-dimensional array and generate indexes by hand:
char *a = new char[rows*columns];
...
a[columns*row + column]
This is probably faster than the double-indirection required in the first two solutions.
You could of course wrap this in a class to preserve a semblance of 2D indexing syntax:
class MatrixWrapper {
...
char& operator()(int row, int column) { return a_[columns*row + column]; }
...
};
...
a(row, column)
A n-dimensional array can not be directly allocated as you are trying to. Here is a way to do it.
int main(){
unsigned int **p;
unsigned int rows = 10;
unsigned int cols = 20;
p = new unsigned int* [rows];
for(size_t row = 0; row < rows; row++){
p[row] = new unsigned int [cols];
}
for(size_t row = 0; row < rows; row++){
delete [] p[row];
}
delete [] p;
}
If you're going to use c-arrays in c++ (instead of std::vector), you're stuck doing something like this:
long rows = 0;
long columns = 0;
cin >> rows >> columns;
// declaration
long** a = new long* [rows];
for(long i = 0; i < rows; ++i)
{
a[i] = new long[cols];
}
// initialization
for(long j = 0; j < rows; ++j)
{
for(long i = 0; i < rows; i++)
{
a[i][j] = 0;
}
}
// delete sub-arrays
for(long i = 0; i < rows; ++i)
{
delete[] a[i];
}
// delete array
delete[] a;