473,509 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multidimenstional array semi-dynamic declaration

beacon
579 Contributor
I haven't seen anything that would suggest this is possible, although I'm hoping it is, but is there a way to write a function that will return a value entered by the user and place that value as the indices for the array?

For example:

1. ReadArraySize asks the user for a number. If a number is entered it is returned. If no number is entered, the user is prompted again.
2. In main(), an integer array is declared with the returned value the user entered in ReadArraySize as the index for both dimensions of the array.
3. ReadArraySize returns 4.
4. In main(), int x = ReadArraySize();
5. The array is declared as int array [x][x];

I'm getting errors when I attempt this, but it would seem like it would be possible to achieve this since the value is unchanging in main().

I'm using C++, by the way...
Jan 29 '08 #1
7 2791
hoggy
9 New Member
this is an intrestin question anyhow what kind of errors d u see
Jan 29 '08 #2
beacon
579 Contributor
Here are the errors:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2087: '<Unknown>' : missing subscript
Jan 29 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Certainly, you can do this.

C++, like C, only has one dimensional arrays. That means the number of elements must be known when the array is created. That make shti code:

int array [x][x];
not compilable since the number of elements is unknown.

But you can do this:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[x * x];
  2.  
But wait, you say, I need a 2D array.

Read this:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
In each case value is the number of elements. Any other brackets only describe the elements.

Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

With the above in mind this array:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Kinda the same, right?

So if your disc file contains

0 1 2 3 4 5 6 7 8 9

Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

Therefore, when you do your read use the address of array[0][0] and read as though you have a
one-dimensional array and the values will be in the correct locations.
Jan 29 '08 #4
beacon
579 Contributor
Thanks for the help weakness.

I was aware that I could create the arrays dynamically with pointers, but I was hoping to avoid it for the sake of the assignment. It was my fault for not being specific.

If I go and use pointers, my professor may not accept it since we are just working with 2D arrays.

The following isn't my original code, but this is what I'm trying to do:
Expand|Select|Wrap|Line Numbers
  1. void main(){
  2.  
  3.     int three = 3, five = 5, seven = 7, nine = 9;
  4.     int square3[three][three];
  5.     int square5[five][five];
  6.     int square7[seven][seven];
  7.     int square9[nine][nine];
  8.  
Jan 29 '08 #5
Ganon11
3,652 Recognized Expert Specialist
I don't think there is a way to dynamically allocate memory without the use of pointers. That is, if you want to allow your program to support 2x2, 5x5, and 9x9 matrices, you must use pointers. You may be able to work around this by declaring the array as some large size (e.g. 10x10) and only using a certain portion of the array (say, the first 6 rows and columns), but this is overkill and not a good programming practice.
Jan 29 '08 #6
beacon
579 Contributor
That's actually probably more along the lines of what my professor had in mind, come to think of it Gannon.

If I was only going to use a portion of it, would I just have to test the location everytime in reference to a starting location to make sure I didn't travel outside of the grid that I'm using at that given time?

Also, if the previous question is true, would I just mod the current location by the size of the matrix to return to the opposite side?
Jan 29 '08 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
Keep in mind that when you use an array you are using a pointer. That is, the array name is the address of element 0. When you use the array name you are using the array address. *array is the same as array[0].
Jan 30 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1770
by: José Luis Ramírez | last post by:
Hi, I'm trying to recreate an array from a var_export()'ed array stored in a database, using eval(). Basically I have something this in my database: array ( 0 => 1, 1 => 2,
2
3763
by: Dennis M. Marks | last post by:
I am never sure of when a semi-colon is required in javascript. Is there a definite rule? -- Dennis M. Marks -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----...
6
1633
by: Treetop | last post by:
I get the following error in IE but not in Netscape. The code works, and the error only shows up if you click on the icon in the bottom left corner of the screen, next to where it says Error on...
1
1451
by: Andrew | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hi This VB script prints out a ragged VB array public function display(vbreply) For I = LBound(vbreply) To UBound(vbreply)...
9
8563
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of...
2
6428
by: Augusto Cesar | last post by:
Hello people. How can I Pass ASP Array variable to Javascript; I´m to trying this: <script language="JavaScript"> var x = new Array(10);
11
1605
by: Schraalhans Keukenmeester | last post by:
I have a substantial random stream of (numerical, long int) data (coming from several SCADA sources) that needs to be passed thru a function that tallies the occurrences of several different bit...
5
24302
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
20
34703
by: Acolyte | last post by:
Hey, I'm working on a project that involves sorting a two-dimensional array of integers using bubble sort. I've got it semi-working, my only issue is that it seems to be 'losing' values, I'm assuming...
1
1133
by: wojwoj17 | last post by:
hello everyone... can you help me to program this problem? i'm from philippines. Problem: TWENTY LOVELY LADIES ARE COMPETING IN A BEAUTY CONTEST. THEY ARE RATED FROM 1 TO 10. A SCORE EQUAL OR...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.