hi everybody,
I am struggling to write a function which takes an int n as parameter, and inside the function I need to initilize a n-dimensional matrix and return the pointer to the initialized matrix.
If you know any online sources where I can read through the related matter then do care to provide me the same. As its been a long time since I am struggling with this problem and so far I havent been able to find a convincing answer.
Thanks
12 4069
What you will have to do is allocate a piece of memory large enough to hold the elements, then return the address of that memory location. By n-dimensional, do you mean an n x n matrix, or a matrix with n dimensions (i.e. 3-dimensional, 4-dimensional, etc)?
What you will have to do is allocate a piece of memory large enough to hold the elements, then return the address of that memory location. By n-dimensional, do you mean an n x n matrix, or a matrix with n dimensions (i.e. 3-dimensional, 4-dimensional, etc)?
n*n is a 2d matrix, what I am looking for is a n-dimesional matrix, Was thinking of recursively allocating it... but couldnt figure out how to do that.
n*n is a 2d matrix, what I am looking for is a n-dimesional matrix, Was thinking of recursively allocating it... but couldnt figure out how to do that.
Are you using C or C++? In C you coutld use malloc and in C++ you could use the new operator.
Let me further clarify things ..
Whatever I pass the value of n in the function, I want to initialize a matrix of that dimension in the function.
Like if the value of the n is 2 then it should be 2*2 matrix
if the value of n is 3 then 2*2*2 matrix
for n=4 it should be 2*2*2*2 matrix and so on ....
I hope this explanation helps the experts for guiding me.
Are you using C or C++? In C you coutld use malloc and in C++ you could use the new operator.
C or C++ doesnt make any difference here.
Its the logic I am struggling with not with the incompetencies of new or malloc.
aint any experts interested to show me path ??
or is it so naive a problem to be asked in this forum ???
The problem probably isn't that the question is a bad question, but that you are asking too much of us. Your past three responses have been within an hour of each other - experts come here to volunteer their time when they can, not at your bidding. Please allow a little more time between responses for people from around the world to try to help.
As for the question itself, let's try to find a pattern. Let n be the argument you pass. Then if n = 1, you need an array of length 2, so enough memory for 2 ints must be allocated. If n = 2, you need a 2x2 matrix - enough room for 4 ints. Following this pattern, we get the following chart: - n | Number of ints
-
--------------------
-
1 | 2
-
2 | 4
-
3 | 8
-
4 | 16
-
...|...
-
n | 2^n
So the function needs to allocate 2^n * sizeof(int) memory. Is this any help?
Banfa 9,065
Expert Mod 8TB
C or C++ doesnt make any difference here.
Its the logic I am struggling with not with the incompetencies of new or malloc.
The is very little logic to it calculate the number of entries in the matrix, a 2x2 matrix has 4 entries, a 2x2x2 matrix has 8 entries, in fact it is
pow(2, n);
or
1 << n;
Then in C malloc this times the size of each entry so asssuming ints -
int *data = malloc((1<<n) * sizeof *data);
-
C++ helps a bit by removing the need to calculate the data size -
int *data = new int[(1<<n)];
-
int *data = new int[(1<<n)];
Unfortunately, this creates a one dimensional array.
A 2x2x2 is declared as:
This array has two elements, not eight. It is the first index that determines the number of elements in the array, In this case, two. The other indexes just describe the elements. In this case, each element is an array of two int arrays of two int.
Next, in C and C++, the name of the array is the address of element 0. In this case, arr is the address of its element 0. Therefore, the name arr is the address of an array of two elements where each element is an array of two int.
That is, you need a pointer to a 2x2 array of int.
You declare this array as you normally do. You use new and ask for the number of 2x2 arrays that you want (in this case 2) and you assign the address returned from new to a pointer to a 2x2 array of int: -
int (*ptr)[2][2] = new int[2][2][2];
-
and you have your array.
The problem probably isn't that the question is a bad question, but that you are asking too much of us. Your past three responses have been within an hour of each other - experts come here to volunteer their time when they can, not at your bidding. Please allow a little more time between responses for people from around the world to try to help.
As for the question itself, let's try to find a pattern. Let n be the argument you pass. Then if n = 1, you need an array of length 2, so enough memory for 2 ints must be allocated. If n = 2, you need a 2x2 matrix - enough room for 4 ints. Following this pattern, we get the following chart: - n | Number of ints
-
--------------------
-
1 | 2
-
2 | 4
-
3 | 8
-
4 | 16
-
...|...
-
n | 2^n
So the function needs to allocate 2^n * sizeof(int) memory. Is this any help?
Dude I will request you to relax a little and think a little.
I agree the function need to allocate 2^n * sizeof(int) memory, but how to do that so that I will have "n-dimesional matrix" interface.
I am not concerned here with the memory layouts, but with the interface.
int *p = (int *)malloc(2*2*sizeof(int))
is valid for a 2-d array but I cant access this memory with p[i][j]
I will need some more logic to do that.
Unfortunately, this creates a one dimensional array.
A 2x2x2 is declared as:
This array has two elements, not eight. It is the first index that determines the number of elements in the array, In this case, two. The other indexes just describe the elements. In this case, each element is an array of two int arrays of two int.
Next, in C and C++, the name of the array is the address of element 0. In this case, arr is the address of its element 0. Therefore, the name arr is the address of an array of two elements where each element is an array of two int.
That is, you need a pointer to a 2x2 array of int.
You declare this array as you normally do. You use new and ask for the number of 2x2 arrays that you want (in this case 2) and you assign the address returned from new to a pointer to a 2x2 array of int: -
int (*ptr)[2][2] = new int[2][2][2];
-
and you have your array.
I think you are getting what I am trying to achieve.
Your explanation of arrays and pointers is fine
and it will really help me if you can help to generalize what you have done above
so that I can n-dimensional matrix.
what if i pass value 10 to my function then by your method (which I have already given thought to) I need to have some 9 nested loops to initialize a 10-d matrix
Banfa 9,065
Expert Mod 8TB
Unfortunately, this creates a one dimensional array.
Not unfortunately, purposefully.
Reading mesalcut post immediately before this it is not possible to generalise the problem to produce a function that allocates the correct data and returns a pointer of the right type for any value of n given at run time (for a value N given at compile time I would suggest a template class).
In this case I would fall back to a single dimensioned array and handle calculating the required index for a given cell in the matrix explicitly.
Unless you can think of a way for a function to take an integer n and return an allocated array with that many dimensions.
The problem boils down to you can't have a type of a function return or variable that is dynamic at run time, it must be fixed when the code is compiled. In this case it needs to be fixed at the lowest common denominator which is a single dimensioned array.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Peter |
last post by:
Hi,
So many times, I have seen compile warning: "you used a char* without
initilize it", probably on the code like this:
------------
char* ptr;
func(..., ptr);
----------
|
by: Mark Hannon |
last post by:
I am trying to initialize an array only once so it can be seen & used by
any functions that need it. As I understand it, if a variable is
declared by itself outside of any functions, its scope is...
|
by: Jim Langston |
last post by:
What I want to do: have a vector of ints in my class initialized with 0 to
499 which will later be pushed/popped out of the vector by instances.
What I have:
class CParticleStream // Yes, I...
|
by: Andi.Martin |
last post by:
Hi,
how is it possible, to only initialize parts of a structure.
Example:
typedef struct{
int a, b;
... (huge lot of members);
double x,y;
|
by: Bryan |
last post by:
I'm using the UIP App block and I have overridden the Initialize
method, but my override never gets called. Here's the code:
public override void Initialize(TaskArgumentsHolder args,...
|
by: bryan |
last post by:
I'm using the UIP App block and I have overridden the Initialize
method, but my override never gets called. Here's the code:
public override void Initialize(TaskArgumentsHolder args,...
|
by: thinktwice |
last post by:
char a = { 0 }
is it ok?
|
by: vduber6er |
last post by:
Lets say I have this structure:
typedef struct numbers {
double first = 0.0;
double second = 0.0;
double third = 0.0;
} MYVALUES;
and I initialize an array of MYVALUES like the following
|
by: Bram Kuijper |
last post by:
Hi all,
as a C++ newbie, I got some question on the initialization of static
reference data members.
Since it isn't possible to initialize static members of a class in the
constructor, I...
|
by: Ramon |
last post by:
Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
Thankx
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |