Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-20-2003, 09:10 AM   #1
Registered: May 2002
Distribution: Mandrake 10.0
Posts: 78

Rep: Reputation: 15
Question C++ Six Element Array

In the following code I don't understand why the variable index is needed. Also in the 'for loop' when the array is ouputed to the vdu via cout, why does it need to be 'number[index].'

#include <iostream.h>

int number[6]; // an array of integers
int index;

number[5] = 42; //initialize the arrray
number[4] = 2001;
number[3] = 7;
number[2] = 180;l
number[1] = 99;
number[0] = 1993;

for (index = 0; index <= 5; index++)
cout << number[index] << endl;

Thanks in advance my book didn't explain this section of code.
Old 08-20-2003, 09:31 AM   #2
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
I'll try to explain it step by step:

index = 0: sets the index variable to 0 (duh)
index <= 5: as long as this condition is met, the for loop will be executed
index ++: after each loop, the index variable is increased by 1

number[index] is the location location in the number array, so when index = 0, number[index] = 1993, when index = 1, number[index] = 99 and so on.

cout << number[index] << endl

will print the content of the array at the location of index to the screen and it will do this as long as index <= 5.
The endl is a newline so that the next element will be printed on a new line.

I hope this explains things to you...
Old 08-20-2003, 12:08 PM   #3
Registered: May 2002
Distribution: Mandrake 10.0
Posts: 78

Original Poster
Rep: Reputation: 15
Talking Ty!

Thanks I understand now!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
index of an element in the array ? thelonius Programming 1 09-24-2005 01:41 PM
number of element in a array of pointer? os2 Programming 7 03-09-2005 12:50 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 09:07 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 03:24 PM
return index of element in java multidimensional array dave bean Programming 8 11-28-2003 12:00 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:52 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration