Saturday, January 21, 2012

Processing With Aray


PROCESSING WITH ARRAY
For an illustration, consider the following declaration,
          int a[200];
It reserves memory for 200 integer variables, which are written as
a[0], a[1], . . .a[199] in which one can assign elements as shown below:

10

20

3

..

..






       <  elements

a[0]       1            2          3          4           5          6               <  position
  In the above illustration, the maximum size of the array is 200 numbers. If one would like to avail more then the declared size, then the compiler will treat only the first n elements as significant and the rest will be omitted.
   The user can access these variables in any order he likes and use them in the same way as simple variables. For example, one can write
          a[15] = 3;
        a[83] = 2;
        cout << a[83]*a[15]+a;
   which will print 7 instead of contents of 83.
    Any integer expression can be used as a subscript in the array.
    For example,
         int i, j;
         for (i = 0; i <= 100; ++i) {
              a[i] = 0;




            }
   Some invalid array index
  #include <iostream.h>
   void main ()
   {
        float x;
        float a[100];
        for ( x =0; x<=10.11; ++0.1)  {
             a[x] = 10;


                
          }
     }
In the array declaration the number of elements (here, 100) has to be specified as a constant. However this constant need not necessarily be written as a sequence of digits. It is generally considered good programming practice to use named constants for this purpose. In C++, named constants are defined by a #define preprocessor control line.
    For example,
          #defined MAX_SIZE 100
        void main ()
        {
              int a [MAX_size) ;
                 for (i=0; i<= MAX_SIZE-1; ++i)
                   a[i] = 1;


                                    
           }
  In C++, simple operations involving entire arrays not permitted. So every character must be treated as a separate variable for processes like assignment operations, comparison operations and so on. For example, though a and b are two arrays having the same storage class, data type, and maximum size, assignment and comparison operations should be carried out only on element basis.
   For example,
          int a[4] = { 4,5,6,7 };
        int b[4] = {1,2,3,4 };
The following operations are invalid:
(1)
      if (a = b)
     cout <<“ Array elements are different \n”;
(2)
      while (a > b) // a and b are array types
     {
           cout <<” array processing \n”;


            
      }              
PROGRAM
A program to initialize a set of numbers in the array and to display it in a standard output device.
    // example 5.1
    #include <iostream.h>
    void main (void)
    {
        int a[7] = { 11,12,13,14,15,16,17 };
        int i;
        cout <<” Contents of the array \n”;
        for (i=0; i<6; ++i) {
             cout << a[i] << ‘\t’;
        }
    }
Output of the above program
     Contents of the array
    11  12  13  14  15  16  17
PROGRAM
A program to read n numbers from the keyboard (where n is defined by the programmer) to store it in an one dimensional array and to display the contents of that array onto the video screen.
      // example 5.2
     #include <iostream.h>
     void main (void)
     {
          int a [100];
          int i, n;
          cout << “ How many numbers are in the array ? “ << endl;
          cin >> n;
          cout << “Enter the elements “ << endl;
          for (i = 0; i <= n-1, ++i) {
               cin >> a[i];
          }
          fout << “ Contents of the array “ << endl;
          for (i= 0; i <= n-1; ++i) {
               cout << a[i] << ‘t’;
          }
      }
Output of the above program
    How many numbers are in the array?
   5
   Enter the elements
   1  2  3  4  5 
Contents of the array
1  2  3  4  5

PROGRAM
A program to read a set of numbers from the keyboard and to find out the largest number in the given array (the numbers are stored in a random order).
     //example 5.3
    #include <iostream.h>
    void main (void)
    {
        int a[100];
        int i,n, larg;
        cout <<” How many numbers are in the array ?” << endl;
        cin >> n;
        cout << “Enter the elements “ << endl;
        for (i = 0; i <= n-1; ++i)  {
             cin >> a[i];
             }
        cout << “Contents of the array “ << endl;
        for (i = 0; i <= n-1; ++i) {
             cout << a[i] << ‘\t’;
             }
        cout << endl;
        large = a[0];
        for (i = 0; i <= n-1; ++i)  {
             if (larg < a[i])
                 larg = a[i];
             }
       cout << “Largest value in the array =” << larg;
   }
Output of the above program
    How many numbers are in the array?
    5
    Enter the elements
11    3  45  67  3
Contents of the array
 11 3  45  67  3
Largest value in the array =67

PROGRAM
A program to read a set of numbers from the standard input device and to sort them in ascending order.
     //example 5.4
    #include <iostream.h>
    void main (void)
    {
        int a[100];
        int i,j,n, temp;
        cout <<” How many numbers are in the array ?” << endl;
        cin >> n;
        cout << “Enter the elements “ << endl;
        for (i = 0; i <= n-1; ++i)  {
             cin >> a[i];
             }
        cout << “Contents of the array (unsorted form) “ << endl;
        for (i = 0; i <= n-1; ++i) {
             cout << a[i] << ‘\t’;
             }
        cout << endl;
        for (i = 0; i <= n-1; ++i)  {
             for (j = 0; j <= n-1; ++j)
                  if (a [i] < a[j]) {
                      temp = a[i];
                                  a[i] = a [j];
                                  a[j] = temp;
                       }
             }
         cout << “Contents of the array (sorted form) “ << endl;
         for (i = 0; i <= n-1; ++i) {
               cout << a[i] << ‘\t’;
               }
Output of the above program
    How many numbers are in the array?
    6
    Enter the elements
    11  23  -45  67  -89  34
    Contents of the array (unsorted form)
    11  23  -45  67  -89  34
    Contents of the array (sorted form)
    -89  -45  11  23  34  67

No comments:

Post a Comment