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...
7 2791
this is an intrestin question anyhow what kind of errors d u see
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
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: -
int* array = new int[x * x];
-
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:
That is an array of 5 elements each of which is an int.
won't compile. You need to declare the number of elements.
Second, this array:
is still an array of 5 elements. Each element is an array of 10 int.
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.
won't compile. You need to declare the number of elements.
Third, the name of an array is the address of element 0
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*.
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: -
int array[5][10];
-
-
int (*ptr)[10] = array;
-
Fourth, when the number of elements is not known at compile time, you create the array dynamically: -
int* array = new int[value];
-
int (*ptr)[10] = new int[value][10];
-
int (*ptr)[10][15] = new int[value][10][15];
-
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: -
int** ptr = new int[value][10]; //ERROR
-
new returns the address of an array of 10 int and that isn't the same as an int**.
Likewise: -
int*** ptr = new int[value][10][15]; //ERROR
-
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: -
int array[10] = {0,1,2,3,4,5,6,7,8,9};
-
has a memory layout of
0 1 2 3 4 5 6 7 8 9
Wheras this array: -
int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
-
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.
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: -
void main(){
-
-
int three = 3, five = 5, seven = 7, nine = 9;
-
int square3[three][three];
-
int square5[five][five];
-
int square7[seven][seven];
-
int square9[nine][nine];
-
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.
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?
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].
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,
|
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 =-----...
|
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...
|
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)...
|
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...
| |
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);
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
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 ...
|
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...
| |