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.
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
}
}
This question already has answers here:
Returning multidimensional array from function
(7 answers)
Closed 9 years ago.
I want to use a two dimensional int array which is returned from a function
how should I define the function return value ?
I used int** but the compiler gave error:
int** tableCreator(){
int** table=new int[10][10];
for(int xxx=1;xxx<10;xxx++){
for(int yyy=1;yyy<10;yyy++){
table[xxx][yyy]=xxx*yyy;
}
}
return(table); //Here:cannot convert from 'int (*)[10]' to 'int **'
}
Try this:
#include <cstdio>
#include <cstdlib>
int** createTable(int rows, int columns){
int** table = new int*[rows];
for(int i = 0; i < rows; i++) {
table[i] = new int[columns];
for(int j = 0; j < columns; j++){ table[i][j] = (i+j); }// sample set value;
}
return table;
}
void freeTable(int** table, int rows){
if(table){
for(int i = 0; i < rows; i++){ if(table[i]){ delete[] table[i]; } }
delete[] table;
}
}
void printTable(int** table, int rows, int columns){
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
printf("(%d,%d) -> %d\n", i, j, table[i][j]);
}
}
}
int main(int argc, char** argv){
int** table = createTable(10, 10);
printTable(table, 10, 10);
freeTable(table, 10);
return 0;
}
You need the second loop to allocate a 2-d array in C and similar operation to free it. a two-D array is in essence an array of arrays so can be expressed as a pointer array. the loop initializes the arrays pointed to the pointers.
Clarifying as per conversation with #Eric Postpischil below: changed createTable to take row/column count for truly dynamic allocation.
int** table=new int[10][10];
this is wrong. you cannot allocate space for 2D dynamic array in this way in C/C++.
Meanwhile, you declared array size as 10, so indices are from 0-9, but you are trying to assign values to index 10 in your nested for loops, which is not right too.
You may do the following for allocation:
int** table = new int*[10];
for (int i = 0; i < 10; ++i)
{
table[i] = new int[10];
}
Usually, the type used to point to an array is a pointer to an element of the array. Since a two-dimensional array of int is an array of array of int, you want a pointer to array of int. The C++ syntax for this type is int (*)[N], for some dimension N. This code demonstrates:
#define N 10
int (*tableCreator())[N]
{
int (*table)[N] = new int[N][N];
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
table[i][j] = i*j;
return table;
}
#include <iostream>
int main()
{
int (*t)[N] = tableCreator();
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
std::cout << t[i][j] << ' ';
std::cout << '\n';
}
delete [] t;
return 0;
}
I. Arrays are not pointers.
II. Why not vector<vector<int> >?
III. If not, then:
typedef int Int10Array[10];
Int10Array *arr = new Int10Array[10];
IV. Why write past the bounds? Do you want explicit nasal demons?
for(int xxx = 0; xxx < 10; xxx++)
^^^ ^^^^
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 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!
}
}
}
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;