473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

real noob question

hi am new at c++ and am trying my best to learn but am having a little trouble understand one aspect that has been repeated in many text books all with very vague explanations.

i am hoping someone can clear some of this up for me.

my question is in regards to arrays and vectors.

i understand that c++ counts from 0+ and not the natural numbers 1+

so an array of test(23) would be in the range 0-22?

this part i understand, however the text book's i have makes use of terms like test.size() -1 to retrieve the last element of an array, which i am having trouble understand.

anyone able to do a simple example to make it clearer?

thank you in advance
Sep 23 '06 #1
3 1595
Banfa
9,065 Expert Mod 8TB
If you have an array with 20 elements

int test[20];

Then (as you say) the indexes for those elements are 0 - 19 inclusive, and you will find that there are 20 numbers in that range.

Expand|Select|Wrap|Line Numbers
  1.  0,  1,  2,  3,  4,  
  2.  5,  6,  7,  8,  9,
  3. 10, 11, 12, 13, 14
  4. 15, 16, 17, 18, 19
  5.  
So the array has 20 entries and the index of it's top entry is 19.

If you get the number of elements in the array by using test.size() if it's a class or (sizeof test/sizeof test[0]) if it's a simple array then you will always be able to access the last element of the array using size - 1.

This is true for any size array, an array of 1000 entries will have indexes numbered 0 - 999 inclusive, the last index = size - 1.
Sep 23 '06 #2
If you have an array with 20 elements

int test[20];

Then (as you say) the indexes for those elements are 0 - 19 inclusive, and you will find that there are 20 numbers in that range.

Expand|Select|Wrap|Line Numbers
  1.  0,  1,  2,  3,  4,  
  2.  5,  6,  7,  8,  9,
  3. 10, 11, 12, 13, 14
  4. 15, 16, 17, 18, 19
  5.  
So the array has 20 entries and the index of it's top entry is 19.

If you get the number of elements in the array by using test.size() if it's a class or (sizeof test/sizeof test[0]) if it's a simple array then you will always be able to access the last element of the array using size - 1.

This is true for any size array, an array of 1000 entries will have indexes numbered 0 - 999 inclusive, the last index = size - 1.
i see i think i am getting a clearer picture now

Expand|Select|Wrap|Line Numbers
  1. std::vector<double> test(10);
  2.  
would be an array with 10 elements, indexed from 0-9

so if one were to perform the action
Expand|Select|Wrap|Line Numbers
  1.  test.size() 
it would return an integer value of the size of the array which in the above example would be 10

we can therefore use the size() -1 to access the last index?

please correct me if i am wrong
Sep 23 '06 #3
Banfa
9,065 Expert Mod 8TB
please correct me if i am wrong
I can't because you are completely right :D
Sep 23 '06 #4

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

Similar topics

1
by: Dave Williams | last post by:
First off...total noob to VB. So far have learned a lot in 1.5 days and feel fairly comfortable at throwing screens up. However, I want to start writing forms that revolve around an access...
10
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
1
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is...
3
by: We need more power captain | last post by:
Hi, I know less than a noob, I've been asked to do some compiles in VC++ 6 without knowing too much at all. (I'm a COBOL program normally so this is all too much for me) I open VC++6, open...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
2
by: Dan McCollick | last post by:
Hi All, Noob question that I can not seem to figure out: I am trying to implement a screenscraper to pull data from two seperate websites, here is some test code so far: public static void...
0
by: AndyW | last post by:
Hey folks. I am trying to get a soap wsdl service working and have a bit of a noob php programming question for it. I'm using PHP 5.x btw. I have written a soap server that contains a...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
1
by: SCRIPT KEEPER | last post by:
Hello, I am a complete noob and just starting off with csharp so I apologize for my basic question. I am wanting to start powershell from inside a batch script and then to pass the powershell args...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...

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.