 |

07-10-2002, 08:58 PM
|
|
|
array concept
|
when we r using arrays, in arrays concept, it start from 0(index), but now i would like to start from 1, is that logic? i just write my program like this(multi dimension)
for(i=1;i<=10;i++)
for(j=1;j<=10;j++)
is that logic? how about the value of a[0][0], is that garbage value? we don't have to border ?or we should start our index 0?
|
|

07-10-2002, 09:48 PM
|
|
Centurion
|
|
Join Date: Feb 2002
Location: NY
Posts: 143
|
|
|
from wat ive learned so far about c++, i think the array will go out of bound if u get the index larger than the array's length:
ie. if u got an array such as this:
int array1[10,10];
then u should have allocated memory for array1[0,0] to array1[9.9]
if u try to access the variable of array1[10,10], i think it may then access the memory that is in use by other program -- not somethin u wanna do...
|
__________________
Richard Chan -- the wanderer in the world of insanity.
My website, for those who care.
|

07-11-2002, 12:36 AM
|
|
|
|
but if you start from array1[1][1], i think it will have some value in array1[10][10]?
|
|

07-11-2002, 01:02 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,885
|
|
If you declare an array in C(++) it's range will be from 0 to maximum value -1.
Will give you these 5 locations:
Code:
array1[0]
array1[1]
array1[2]
array1[3]
array1[4]
Unlike in VB you can't make an array start at position 1.
|
|

07-12-2002, 01:04 AM
|
|
|
|
but can i do like this
int array[5]
array[1]=1
array[2]=2
array[3]=3
array[4]=4
array[5]=5
is this logic?
|
|

07-12-2002, 01:26 AM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,885
|
|
Huh????
You are really missing the point.
I told you when you do:
Code:
int array[5]
array[0]=1
array[1]=2
array[2]=3
array[3]=4
array[4]=5
array[5] does NOT exist
|
|

07-13-2002, 07:19 AM
|
 |
C# Lover
* Expert *
|
|
Join Date: Jan 2002
Location: 00-80-C8-C3-2E-52
Posts: 1,899
|
|
ArountV is correct. array[5] does not exist. If you attempt to access it, you are going to get an unpredictable garbage value with most compilers). This is because you are pointing to a memory address that has not been given any value... so you will never know what is in there.
You *could* start the index at 1 if you absolutely wanted to. Use something like:
Code:
int intElements;
cout << "How many elements in the array? ";
cin >> intElements;
//since you want to skip the 0 index,
//you must carve out an array with
//its size being one element larger than normal
int *wow = new int[intElements+1];
//for demonstration purposes,
//give the array elements a value that
//corresponds with its index
//(notice that index 0 is skipped and never initialized)
for (int x=1; x<=intElements; x++)
wow[x] = x;
//for demonstatration purposes,
//display the values by accessing the
//corresponding index
for (int x=1; x<=intElements; x++)
cout << wow[x] << endl;
//just for the heck of it, see what's in the 0 index
cout << wow[0];
Here's the output I got with my Borland compiler.
Code:
How many elements in the array? 5
1
2
3
4
5
23981055
|
__________________
"Man is still the best computer we can put aboard a spacecraft...and the only one that can be mass produced with unskilled labor." - Wernher von Braun
Last edited by Rezner; 07-13-2002 at 07:26 AM.
|

09-04-2002, 07:13 PM
|
|
|
|
In order to force the array to start at 1, include this statement:
Option Base 1
Regards,
Bobbi (Junebug)
|
|

09-04-2002, 07:22 PM
|
 |
Paranoid Coder
Retired Leader
|
|
Join Date: Mar 2001
Location: Canada
Posts: 2,716
|
|
|
"Option Base 1" is for Visual Basic, not C++.....
|
|

09-04-2002, 08:01 PM
|
 |
Contributor
|
|
Join Date: Aug 2002
Location: Kansas
Posts: 502
|
|
|
If you're a Computer Science student you had better learn to start counting from zero. It will just come back to haunt you over and over again if you don't. It's a concept that is best learned early. You get used to it after a while. Also, if you plan on doing any programming at all, you should get comfortable with the idea...not just if you're a computer science student.
|
__________________
"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
|

09-05-2002, 05:31 AM
|
|
Junior Contributor
|
|
Join Date: Sep 2001
Location: Washington, D.C.
Posts: 302
|
|
|
While Rezner provides a creative way to make a 1 based array, if you're ever going to write code that has a chance of being seen by anyone other than yourself, please don't do this. Most people are used to, and quite comfortable with, 0-based arrays in C++ to do otherwise would only serve to confuse people.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|