1

The information that i have found on the internet for this is not explained very well. I would like to know how to go about implementing a 2-D array of double-precision floating point numbers. I would like the dimensions of the array to be variable so if I say .double 100, then I am wasting a lot of memory because that may not be the size of the array e.g. could be 5 x 5. Would I have to use the stack to solve this problem? Or how else could I go about it? Also if someone could explain how to efficiently fill the array it would be much appreciated!

1
  • Is the issue you're having about dynamically allocating memory (use sbrk or the stack) or is it about using the memory as a 2d array (pick a row order and index accordingly as shown here)? These are two separate problems. Commented Dec 3, 2020 at 16:06

1 Answer 1

2

You can implement any rectangular 2D array as 1D array using row-major order, the only different would be is in calculating the address of the element.

For example if you have a 3×5 array of double's and you want to access the element array[x][y], you can calculate its address using the formula:

address of array[x][y] = base of array + 8 * (5 * x + y)

where base of array is the base address of array or array[0][0], 8 is the size of an element sizeof(double) and 5 is the number of columns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.