Saturday, 12 April 2014

Array Using C++

Array: An array is a variable that holds multiple values of the same type.
Arrays and pointers have a special relationship as arrays use pointers to reference memory locations. Syntax is :
datatype arrayname[array size];

#include <iostream.h>   
#include <iomanip.h>   //we use this header file for set w OR Width 

main()
{
   int arr[ 10 ]; // its an integer type array of size 10

/*initialize elements of (array) i.e start with 0 and n-1 means 0to9         
  for sum using for loop*/
  
   for ( int i = 0; i < 10; i++ )
   {
     arr[ i ] = i + 100;
   // i.e calculation 100 =0 + 100 to 109= 9+100

   }
   
cout << "Array Members" << setw( 12 ) << "Values" << endl;

   // again for loop for output/ display                      

   for ( int j = 0; j < 10; j++ )
   {
      cout << setw( 6 )<< j << setw( 17 ) << arr[ j ] << endl;
   }

   return 0;

}

Output:



No comments:

Post a Comment