Friday 26 November 2010

ARRAY | Fundamentals of Programming (FOP)


ARRAY :
Array is a collection or group of similar data type elements stored in contiguous memory. The individual data items can be characters, integers, floating points numbers and so on . Here contiguous memory allocation means array occupies contiguous bytes as needed in the memory. 

DEFINING AN ARRAY :
Arrays are defined in much the same manner as ordinary variables. Except that each array name must be accompanied by a size specification number of elements for a one-dimensional array. The size specified by positive integer expression enclosed in square brackets. The expression is usually written as a positive integer constants. For example if we want to declare an array of 10 integer values then we can  define it as following. The general form is Storage class data type array[expression] = {vlaue1, value2,…valuen}

Single Dimension Array 
 Int a[10];

a[0]         a[1]         a[2]           a[3]        a[4]             a[5]        a[6]           a[7]         a[8]             a[9]
2 bytes.

2
bytes.

2
 bytes.

2
bytes.

2
bytes.

2
bytes.

2
bytes.

2
 bytes.

2
bytes.

2 bytes.

2001        2003       2005         2007         2009         2011       2013         2015        2017          2019        

Each array elements is referred to by specifying the array name followed by one or more  subscripts. With each subscript enclosed in square brackets. Each subscript must be expressed as a non-negative integer : thus in the n- element array x, the array elements arr[0], arr[1], arr[2]..arr[n-1]. The value of each subscript can be expressed as an integer constant, an integer variable or a more complex integer expression.

The number of subscripts determines the dimensionality of the array. For example, x[I]  refers to an element in the one dimensional array x. Similarly y[I][j] refers to an element in the two dimensional array..

MULTIDIMENTIONAL ARRAY 

Multidimensional, array is defined in much the same manner as one-dimensional arrays, except  that a separate pair of square brackets is required for each subscript. Thus a two dimensional array will require two pairs of square brackets, three dimensional array will require three pairs of square brackets and so on.

In general terms, a multidimensional array definition can be written as
Storage_class data_type array[exp1][exp2]…[expn];



Where storage class refers to the storage class of the array, data type is its data type. Array is the array name and exp1, exp2,.. expn are positive valued expressions that indicate the number of array element associated with each subscript. The storage class is optional, the default values are automatic for arrays
 that are defined inside of a function, and external for  arrays defined outside of a function. 


Two Dimension Array

Int a[5][5];

    0                        1                        2                             3                              4
A[0][0]
A[0][1]
A[0][2]
A[0][3]
A[0][4]
A[1][0]
A[1][1]
A[1][2]
A[1][3]
A[1][4]
A[2][0]
A[2][1]
A[2][2]
A[2][3]
A[2[4]
A[3][0]
A[3][1]
A[3][2]
A[3][3]
A[3][4]
A[4][0]
A[4][1]
A[4][2]
A[4][3]
A[4][4]

The first line defines table as a floating point array having 50 rows and 50 columns (hence 50 x
50 = 2500 elements) and the second line establishes page as a character array with 24 rows and  80
columns (24 x 80 =1920 elements), the third array can be thought of as a set of double  precision tables, each having 66 lines and 266 columns (hence 100 x 66 x 255 = 1,683,000 elements).

The last definition is similar to the preceding definition except that the symbolic constant L,M,N defines the array size. Thus the values assigned to these symbolic constant will determine the actual size of  the array.

If a multidimensional array definition includes the assignment of initial values, then care must  be given to the order in which the initial values are assigned to the array elements(remember only external and static arrays can be initialized). The rule is that the last (right most) subscript increases most rapidly and the first(left most) subscript increases least rapidly. Thus the elements of a two-dimensional array will be

assigned by rows, that is the element of the first row will be assigned, then the element of the second row and so on.
 For example, consider following two-dimensional array definition : 
Int values[3][4] = {1,2,3,4,5,6,7,8,9,10,11.13};  

Note that values can be thought of a table having three  rows and for columns (four element per
row.) Since the initial values are assigned by rows (i.e. last subscript including most rapidly,
the results this initial assignment are as follows: 


            Remembers that the first subscript ranges from 0 to 2 and the second ranges from 0 to 3. This example can be written as :

           
           
            The natural order in which the initial values are assigned can be altered by forming groups of
 initial values enclosed in braces. The values within each innermost pair of braces will be assigned to those array element whose last subscript changes most rapidly. In a two dimensional array, for example, the
 value within the inner pair of braces will be assigned to the element of row, since the second subscript increase most rapidly. If there array two few values within a pair of braces, the remaining elements of that row will be assigned zeros. On the other hand, the number of values within each pair of braces cannot be exceed the defined row size.

            Multidimensional arrays are processed in the same manners as one-dimensional arrays, an element-by-element basis. However, some care is required when passing multidimensional arrays to a function. In

particular, the formal argument declarations within a function definition must include explicit size specifications in all of the subscript positions except the first. These size specification must be consistent with the corresponding size specifications in the calling program. The first subscript position may be written as an empty pair of square brackets as with a one-dimensional array.

            At individual array element that are not assigned explicit values will automatically to set to zero. This includes the remaining elements of an array in which certain elements have been assigned non zero values. The array size need not be specified explicitly when initial values are included as a part of an
array definition, with a numerical array, the size will automatically be set equal to the number of initial values included within the definition.

            ARRAY AND STRINGS
            The gets and puts functions:

            The gets and puts functions facilitate the transfer of strings between the computer and the standard input/output devices. Each of these functions accepts a single argument. The argument must be a data item that represents a string(e.g. character array). The string may  include white space characters. In the case of gets, the string will enter from the keyboard and will terminate with a new line character(i.e. the string will end when the user presses the RETURN key). The gets and puts functions offer simple alternatives to the use of scan f and printf for reading and displaying strings, as illustrated in the following example. Here is a program that reads a line of text into the computer and then writes it back out in its original form :





This program utilizes gets and puts to transfer the line of text into and out of the computer.  Most C compilers include library functions that allow strings to be compared. Copied or concatenated. Other functions permit operations on individual character within strings. For example, they allow individual characters to be found within strings and so on. The following example illustrate the use of some of these library functions :

Strlen()
This function counts a number of characters present in a string while giving a call to the function we are to pass the base address of the string. Total numbers of characters of the stings are counted without counting null character, returning length of the string.

           
Static char msg[] = “Lord Krishna”;
Int n;
N=strlen(msg);
Printf(“length of string =%d”, n);


            Strcpy()
This function copies the contents of one string to another. The base address of source and  target strings
are supplied to the function.

Static char source[] = “Lord Krishna”;
Static char target[15];
Strcpy(target, source);
Printf(“Source string is : %s”, source);
Printf(“target string is   :%s”,target);

           







Strcat()
This function concatenates the source string at the end of target sting.

Static char s[] = “Lord”;
Static char t[] = “Krishna”;
Strcat(t,s);
Printf(“source string is %s\n”, s);
Printf(“target stirng is %s\n”, t);

This function compare two strings to find out whether they are same of different. The process of character wise comparison continues till a mismatch is attained or- end of string. If two strings are identical, it returns a values zero, if they are not, it returns numeric difference between ASCII values of non-matching characters.

Strcmp() :
 This function compares two strings and returns some integer value. If both the strings are equal then it returns 0 otherwise it returns some other integer value.

Main()
{
  static char s1[]=”Jerry”;
  static char s2[]=”Ferry”;
  static char s3[]=”jerry boy”
 int I,j,k;
  I = strcmp(s1, “jerry”);
  J= strcmp(s1, s2);
 J=strcmp(s1, s3);
  Printf(“%d %d %d”, I,j,k);
}

PROCESSING AN ARRAY :
Single operations involving entire arrays are not permitted in C. thus if a and b are similar
arrays, assignment operations. Comparison operations, and so on must be carried out on an
element-by-element basis. This is usually accomplished within a loop where each pass of loop
will therefore equal the number of array elements to be passed.
For example,

#include<stdoio.h>
#define EOL ‘\n’;
main()
{
 char letters[80];
  int tag, count=0;
  while(letter[count] = getchar() != EOL)
  count ++;
  tag = count;
  count=0;
 while(count<tag)
 {
 putchar(toupper(letter[count]));
  ++count;
  }
}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms