array

This is a discussion on array within the C++ Programming forums, part of the General Programming Boards category; i need to know how to make a program print the whole array out...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    Unhappy array

    i need to know how to make a program print the whole array out

  2. #2
    harryp
    Guest
    Code:
    int array[5] = { 5,10,15, 20, 25 };
    
    for (int i=0; i<5; i++)
         cout << "array[" << i << "]: " << array[i] << endl;
    That's all it takes.

    Brendan

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    can you or someone else explain how that works please
    Hooked On Phonics Didn't Work For Me!

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    The array has 5 places.
    The loop, is controlled by the integer i.

    Normally, with a loop
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void )
    {
    	int i;
    	for ( i = 0; i < 10; i++ )
    	{
    		cout << "Message " <<endl;
    	}
    	return 0;
    }
    This would print the string, Message, 10 times in a row.
    The following code, shows that you can change, or add to the string Message, each time you loop it (providing usefulness).
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void )
    {
    	int i;
    	for ( i = 0; i < 10; i++ )
    	{
    		cout << "Message " << i <<endl;
    	}
    	return 0;
    }
    That is what harryp's code is doing.
    He created a loop, and it will loop 5 times.
    It just happens to be that each time it loops, it will print, to the screen, the next chunk of information in the array.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21