Go Back  Xtreme Visual Basic Talk > Other Languages > Miscellaneous Languages > array concept


Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2002, 08:58 PM
kinki
Guest
 
Posts: n/a
Default 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?
Reply With Quote
  #2  
Old 07-10-2002, 09:48 PM
rich2kchan rich2kchan is offline
Centurion
 
Join Date: Feb 2002
Location: NY
Posts: 143
Default

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.
Reply With Quote
  #3  
Old 07-11-2002, 12:36 AM
kinki
Guest
 
Posts: n/a
Default

but if you start from array1[1][1], i think it will have some value in array1[10][10]?
Reply With Quote
  #4  
Old 07-11-2002, 01:02 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

If you declare an array in C(++) it's range will be from 0 to maximum value -1.
Code:
int array1[5]
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.
Reply With Quote
  #5  
Old 07-12-2002, 01:04 AM
kinki
Guest
 
Posts: n/a
Default

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?
Reply With Quote
  #6  
Old 07-12-2002, 01:26 AM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,885
Default

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
Reply With Quote
  #7  
Old 07-13-2002, 07:19 AM
Rezner's Avatar
Rezner Rezner is offline
C# Lover

* Expert *
 
Join Date: Jan 2002
Location: 00-80-C8-C3-2E-52
Posts: 1,899
Default

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.
Reply With Quote
  #8  
Old 09-04-2002, 07:13 PM
Junebug
Guest
 
Posts: n/a
Lightbulb

In order to force the array to start at 1, include this statement:
Option Base 1
Regards,
Bobbi (Junebug)
Reply With Quote
  #9  
Old 09-04-2002, 07:22 PM
orufet's Avatar
orufet orufet is offline
Paranoid Coder

Retired Leader
 
Join Date: Mar 2001
Location: Canada
Posts: 2,716
Default

"Option Base 1" is for Visual Basic, not C++.....
Reply With Quote
  #10  
Old 09-04-2002, 08:01 PM
noRulez's Avatar
noRulez noRulez is offline
Contributor
 
Join Date: Aug 2002
Location: Kansas
Posts: 502
Default

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
Reply With Quote
  #11  
Old 09-05-2002, 05:31 AM
quwiltw quwiltw is offline
Junior Contributor
 
Join Date: Sep 2001
Location: Washington, D.C.
Posts: 302
Default

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.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->