Tuesday, January 24, 2012

Character Arrays


CHARACTER ARRAY
The procedure for declaring character array is almost the same as for other data types such as integer or floating point. One can declare the character array by means of alphanumeric characters.
    The general format of the character array is,
                   storage_class character_data_type array_name [expression]
     The storage-class is optional and it may be either one of the scope of the variable such as automatic, external, static, or register; the name can be any valid C++ identifier and the expression a positive integer constant.
   For example,
        char page [40]
      char sentence [300];
      static char line [50];
   The basic structure of the character array is
                                

 R


A

V

I

C

\0
   Each element of the array is placed in a definite memory space and each element can be accessed separately. The array element should end with the null character as a reference for the termination of a character array.
Initializing the character array  Like a integer and floating point array, the character array also be initialized.
   For example,
        char colour [3] = “RED”;
  The elements would be assigned to each of the character array position in the following way:
      colour [0] = ‘R’;
      colour [1] = ‘e’;               
      colour [2] = ‘D’;
   The character array always terminate with the null character, that is, a back-slash followed by a letter zero (not letter ‘o’ or ‘O’), and the computer will treat them as a single character.
   The null character will be added automatically by the C++ compiler provided there is enough space to accommodate the character.
   For example,
       char name [5] = “ravic”  // error
   The following assignment would be for the each cell,
       name [0] = ‘r’;
       name [1] = ‘a’;               
       name [2] = ‘v’;               
       name [3] = ‘i’;               
       name [4] = ‘c’;               
  The above declaration is wrong because there is no space to keep the null character in the array as a termination character and it can be corrected by redefining the above array as
       char name [6] = “ravic”  // right
   The following assignment would be for the each cell,
       name [0] = ‘r’;
       name [1] = ‘a’;               
       name [2] = ‘v’;               
       name [3] = ‘i’;               
       name [4] = ‘c’;  
       name [5] = ‘\0’;
       Following is a valid array declaration:
        char line[] = “this is a test program”         
    The square brackets can be empty, as the array size would have been specified as part of the array definition.
PROGRAM
  A program to initialize a set of characters in an one dimensional character array and to display the contents of the given array.
    // example 5.10
   #include <iostream.h>
   void main (void)
   {
        int i;
        static char name [5] = { ‘r’,’a’,’v’,’i’,’c’};
        cout << “Contents of the array” << endl;
        for (i = 0; i <= 4; ++i) [
              cout << “name[ “ << i << “ ] = “ name [i] << endl;
              }
    }
Output of the above program
      Contents of the array
     name [ 0 ] = r
     name [ 1 ] = a       
     name [ 2 ] = v       
     name [ 3 ] = i       
     name [ 4 ] = c
PROGRAM
A program to initialize a set of character strings in an one dimensional character array and to display the contents of the array.
      // example 5.12
    #include <iostream.h>
    void main (void)
    {
        int i;
        static char name [] = “this is a test program”;
        cout << “Contents of the array” << endl;
        for (i = 0; name [i] != ‘\0’; ++i) {
              cout << “name[ “ << i<< “] =” << name[i] <<endl;
              }
      }
Output of the above program
       Contents of the array
      name [ 0]   = t
      name [ 1]   = h        
      name [ 2]   = i       
      name [ 3]   = s       
      name [ 4]   =         
      name [ 5]   = i       
      name [ 6]   = s       
      name [ 7]   =         

      name [ 8]   = a       
      name [ 9]   =         
      name [ 10] = t       
      name [ 11] = e       
      name [ 12] = s       
      name [ 13] = t       
      name [ 14] =         
      name [ 15] = p       
      name [ 16] = r       
      name [ 17] = o       
      name [ 18] = g       
      name [ 19] = r       
      name [ 20] = a       
      name [ 21] = m
PROGRAM
A program to read a line from the keybord and to display the contents of the array on the screen.
    // example 5.13
   // reading a line
  #define MAX 80
  #include <iostream.h>
  void main (void)
  {
     char line [MAX];
     cout << “Enter a line of text \n”;
     cin.get (line, MAX, ‘\n’);      //reading a line
     cout << “output from the array” << endl;
     cout << line;
Output of the above program
      Enter a line of text
     this is a test program
     output from the array
     this is a test program
PROGRAM 
A program to read a set of lines from the keyboard and to store it in an one dimensional array and to display the contents of the array on the screen.
       // example 5.14
     // reading a set of lines
     #define MAX 80
     #include <iostream.h>
     void main (void)
     {
         char line [MAX]
         cout << “Enter a set of lines and terminate with @ \n”;
         cin.get (line,MAX, ’@’);     reading a string
         cout << “output from the array” << endl;
         cout << line;
     }
Output of the above program
      Enter a set of lines and terminate with @
      This is a
      test program
      by ravic
      @
      Output from the array
      This is a
      test program
      by ravic
PROGRAM
A program to read a set of lines from the keyboard; store it in an one dimensional array;
Display the contents of the array and the number of the characters on the screen.
    //finding the number of characters
    #define MAX 200
    #include <iostream.h>
    void main (void)
    {
        char line [MAX];
        int nch;
        char ch;
        int number (char line [] ) ;
        cout << “Enter a set of lines and terminate with @”;
        cout << endl;
        cin.get (line,MAX,’@’);
        nch = number (line)-1;
        cout << output from the array” << endl;
        cout << line << endl;
        cout << endl;
        cout << “Number of characters =” << nch << endl;
    }
    int number (char a [])
    // function to find the number of characters
    {
        int i;
        i = 0;
        while (a [i] != ‘\0’)
              ++i;
        return (i);
     }
Output of the above program
   Enter a set of lines and terminate with @
   this is
   a test
   @
   output from the array
   this is
   a test
   Number of characters =15
PROGRAM 5.16
A program to read a set of lines from the keyboard; store it in an one dimensional array A; copy the contents of A to an array B and display the contents of arrays A and B separately.
    For example,
t
h
i
s
..

    
    A

t
h
i
s
..

    B          
       
    //string copy
    #define MAX 200
    #include <iostream.h>
    void main (void)
    {
        char a[MAX], b[MAX];
        void stringcopy (char b[], char a[]);
        cout << “Enter a set of lines and terminate with @”;
        cout << endl;
        cin.get (a,MAX,’@’);
        stringcopy (b,a);
        cout << “output from the A array” << endl;
        cout << b << endl;
        cout << “output from the B array” << endl;
        cout << b << endl;
     }
     void stringcopy (char b [], char a[])
     //copy the contents of A to B
     {
         int i;
         i = 0;
         while ( a[i] != ‘\0’) {
               b[i] = a[i];
               i++;
         }
         b[i++] = ‘\0’;
    }
Output of the above program
    Enter a set of lines and terminate with @
    this is
    by ravic
    @
    Output from the A array
    this is
    a test program
    by ravic
    output from the B array
    this is
    a test program
    by ravic
PROGRAM
A program to read a set of lines from the keyboard; store in the array A; again read a set of lines from the keyboard and store it in the array B; copy the contents of array A and array B into an array TOTAL and display the contents of arrays A and B and TOTAL separately.
    For example,
t
h
i
s
..

            
   A
                    
r
a
..



              
    B  

t
h
i
s
r
a
..


    TOTAL
  
   //string concatenate
   #define MAX 200
   #include <iostream.h>
   void main (void)
   {
       char a[MAX], b[MAX];
       void stringconcat (char total [], char b[]);
       cout << “Enter a set of lines and terminate with @”;
       cout << endl;
       cin.get (a,MAX,’@’)
       cout << “Enter again a set of lines and terminate with $“;
       cout << endl;
       cin.get (b,MAX,’$’);
       stringconcat (total,a,b);
       cout << endl;
       cout << “output from the A array” << endl;
       cout << a << endl;
       cout << ”output from the B array” << endl;
       cout << b << endl;
       cout << “output from the TOTAL array” <<endl;
       cout << total << endl;
  }
  void stringconcat (char total[], char source1 [],
                            char source2 []
  // copy the contents of source1 and source2 to the total
  {
      int i,j;
      i =0;
      while ( source1 [i] != ‘\0’) {
             total[i] = source1[i];
             i++;
             j++;
       }
       total [i++] = ‘\0’;
   }
Output of the above program
    Enter a set of lines and terminate with @
    this is
    a test program
    @
    Enter again a set of lines and terminate with $
    by ravic
    of iti palghat
    $
    Output from the A array
    this is
    a test program
    output from the B array
    by ravic
    of iti palghat
    output from the TOTAL array
    a test program
    by ravic
    of iti palghat         

Monday, January 23, 2012

Multidimensional Arrays

MULTIDIMENSIONAL ARRAYS
Multidimensional arrays are defined in the same manner as one dimensional arrays, except that a separate pair of square brackets are required for each subscript. Thus, a two dimensional array will require two pairs of square brackets; a three dimensional array will require three pairs of square brackets and so on.
The general format of the multidimensional array is,
                storage_class  data_type  arrayname [expression_1][expression_2]...
                                                                                                    [expression_n];
   where the storage_class refers to the scope of the array variable such as external static, automatic or register; the data_type refers to the nature of the data elements in the array such as character type, integer point or floating point etc; and the array name is the name of the multidimensional array. Expression_1, expression_2... expression_n refers to the maximum size of the each array locations.
     For example,
          float coordinate x[10] [10];
          int value [50] [10] [5];
          char line [10] [80];
          static double records [100] [100] [10];
  In the above illustration, the first line defines the coordinate as a floating point array having 10 rows, 10 columns with a total of 100 elements. The second one is a three dimensional integer array whose maximum size is 2500 elements.

Multidimensional array initialization Similar to one dimensional array, multidimensional arrays can also be initialized, if one intend assign some values to these elements. It should be noted that only external or static arrays can be initialized.
     For example, consider the following two dimensional array declaration:
     (1)
          int x[2][2] = {1,2,3};
  where x is a two dimensional array of integer numbers whosw maximum size is 4 and the assignments would be
         x[0] [0] = 1;
       x[0] [1] = 2;
       x[1] [0] = 3;
       x[1] [1] = 4;
   (2)      
   static float sum [3] [4 = { 0.1,0.2,0.3,0.4,0.6,0.7,0.8,0.9,1,1,1,1 };
  where sum is a static two dimensional array of floating point numbers and the maximum size of the array is 3 rows and 4 columns having a total of 12 elements.
      The assignments would be
          sum [0] [0] =0.1  sum [0] [1] =0.2  sum [0] [2] =0.3  sum [0] [3] =0.4
          sum [1] [0] =0.6  sum [1] [1] =0.7  sum [1] [2] =0.8  sum [1] [3] =0.9
          sum [2] [0] =1     sum [2] [1] =1     sum [2] [2] =1     sum [1] [3] =1
  The natural order in which the initial values are assigned can be altered by forcing groups of initial values enclosed within braces, i.e., {...}.
   For example, in the following a two dimensional array can be declared.
         int A[3] [3] =   {
                              { 1,2,3 },
                              { 4,5,6 },
                              { 7,8,9 }
                              };
  In the above array declaration, the three values in the first inner pair of braces are assigned to the array element in the first row; the values in the second pair of braces are assigned to the array element in the second row and so on.
      The elements in the above array A will be assigned as
       A[0] [0] = 1     A[0] [1] = 2     A[0] [2] = 3
      A[1] [0] = 4     A[1] [1] = 5     A[1] [2] = 6
      A[2] [0] = 6     A[2] [1] = 8     A[2] [2] = 9     
       Now consider the following two dimensional array definition,
        int matrixa [3] [3] = {
                                    { 1,2 },
                                    { 4,5 },
                                    { 7,8 }
                                    };
   This definition assigns values only to the first two elements in each row and hance, the array elements will have the following initial values,
   matrixa[0] [0] =1     matrixa[0] [1] =2     matrixa[0] [2] =0
   matrixa[1] [0] =4     matrixa[1] [1] =5     matrixa[1] [2] =0
   matrixa[2] [0] =7     matrixa[2] [1] =8     matrixa[2] [2] =0
   Note that the last element in each row is assigned a value zero.
    If we declared the two dimensional array as
    int matrix [3] [3] = { 1,2,3,4,5,6,7 }
   then following assignment will be carried out:
   matrixa[0] [0] =1     matrixa[0] [1] =2     matrixa[0] [2] =3
   matrixa[1] [0] =4     matrixa[1] [1] =5     matrixa[1] [2] =6
   matrixa[2] [0] =7     matrixa[2] [1] =0     matrixa[2] [2] =0
   Two of the array elements will be again assigned zero, though the order of the assignment will be different.
   If one declares the array definition as
  int a [3] [3] =      {
                            { 1,2,3,4 },
                            { 5,6,7,8 },  
                            { 9,10,11,12 }  
                            };
then, the maximum array allocation for the above two dimensional declaration will be 3x3=9 elements. But the initial definition of the array elements have exceeded the declared size and hence, normally, the compiler will produce the error massage. To avoid the above problem, the array may be declared and written as,
  int a [3] [3] =      {
                            { 1,2,3,4 },
                            { 5,6,7,8 },  
                            { 9,10,11,12 }  
                            };
PROGRAM
A program to initialized a set of numbers in a two dimensional array and to display the content of the array on the careen.
          //example 5.7
        #define N 3
        #define M 4
        #include <iostream.h>
        void main (void)
        {
             int I,j;
             float a[N] M] = {
                                    { 1,2,3,4 },
                                    { 5,6,7,8 },  
                                    { 9,10,11,12 }  
                                    };
              cout << “Contents of the array “ << endl;
              for (i = 0; i <=N-1; ++i) {
                   for (j = 0; j <= M-1; ++j)
                        cout << a[i] [j] << ‘\t’;
                        cout << endl;
                   }
              }
Output of the above program
     Contents of the array
    1      2      3      4
    5      6      7      8 
    9    10     11    12
PROGRAM
A program initialize only a few elements of a two dimensional array and to display the content of the array on the screen.
     //example 5.8
    #define N 3
    #define M 4
    #include <iostream.h>
    void main (void)
    {
        int i, j;
        float a [N] [M] = {
                                 { 1,2,3 },
                                 { 5,6,7 },
                                 { 9,10,11 }
                                  };
         Cout << “ Contents of the array “ << endl;
         for (i = 0; i <= N-1; ++i) {
              for (j = 0; j <= M-1; ++j)
                   cout << a[i] [j] << ‘\t’;
                   cout << endl;
              }
         }
Output of the above program
    Contents of the array
    1     2     3     0
    5     6     7     0
    9    10   11     0
PROGRAM
A program to read the elements of the given two matrices of order n x n and to perform the matrix multiplication.
     // example 5.9
    #define MAX 100
    #include <iostream.h>
    void main (void)
    {
         void output (float a [MAX] [MAX], int n);
         // function declaration
         void mul (float a[MAX] [MAX], float b [MAX] [MAX], int n);
         float a[MAX] [MAX], b[MAX] [MAX;
         int i,j,n;
         cout << “order of A matrix “ <<endl;
         cin >> n;
         cout << “Enter the elements of A Matrix “ << endl;
         for (i= 0; i <= n-1; ++i)  {
              for (j =0; j <= n-1; ++i)
                   cin >> a[i] [j];
             }
         cout << “ order of B matrix “ << endl;
         cin >> n;
         cout << “Enter the elements of B Matrix “ << endl;
         for (i = 0; i <= n-1; ++i) {
              for (j = 0; j <= n-1; ++j)
                   cin >> b[i] [i];
              }
         cout << “output A[i] [j] “ << endl;
         output (a,n);
         cout << endl;
         cout << “output B[i] [j] “ << endl;
         output (b,n);
         mul (a,b,c) ;
    }
    void mul (float a [MAX] [MAX], float b[MAX] [MAX], int n)
    {
         void mul (float c[MAX] [MAX], int n);
         // function declaration
         float c[MAX] [MAX];
         int i,j,k;
         for (i = 0; i <= n-1; ++i) {
              for (j = 0; j <= n-1; ++j) {
                   c[i] [j] = 0.0;
                   for (k =0; k <= n-1; ++k)
                         c[i] [j] = c[i] [j] + a[i] [k] * b[k] [j];
                   }
         }
         cout << endl;
         cout << “output of c[i] [j] matrix” << endl;
         output (c,n) ;
    }
    void output (float x[MAX] [MAX], int n)
    {
         int i,j;
         for (i = 0; i <= n-1; ++i) {
              for (j = 0; j <= n-1; ++j)
                   cout << x[i] [j] << ‘\t’;
                   cout << endl;
              }
    }

Output of the above matrix
     Order of A matrix
    3
    Enter the elements of A Matrix
    3     3     3
    3     3     3
    3     3     3
    Order of B matrix
    3
    Enter the elements of B Matrix
    3     3     3
    3     3     3
    3     3     3
    Output A[i] [j]
    3     3     3
    3     3     3
    3     3     3
    Output of c[i] [j] matrix
    27    27    27
    27    27    27
    27    27    27
  The following function performs the matrix subtraction of the two given matrices of order n*n.
       //function on matrix subtraction
     void sub (float a[MAX] [MAX], float b[MAX] [MAX], int n)
     {
         void output (float c[MAX][MAX]; int n);
         // function declaration
         float c [MAX [MAX];
         int i,j,k;
         for (i =0; i <= n-1; ++i) {
              for (j = 0; j <= n-1; ++j) {
                   c[i] [j] = a[i] [j] - b[i] [j];
              }
         }
    cout << endl;
    cout << “output of c[i] [j] matrix” << endl;
    output (c,n);
    }