Computer Concept & Programming In C - Unit IV - 1




Q.1 What is an array? How does manipulate array elements?
Ans. An array is a collection of variables that are of the same data type. Each item in an array is called an element. All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots.
Declaring Arrays: -
The general form to declare an array is as follows -
Data_type array_name [array_size];
Here data_type is specifier that indicates what data type the declared array will be.
Array_name is the name of the declared array.
Array_size defines how many elements the array contain.
Note that the brackets ([and]) are required in declaring an array. The brackets pair ([and]) is also called the array subscript operator. 
For example an array of integers is declared as 
int integers [32];
Where int specifies the data type of the array whose name is integer. The size of the array is 32, which means that the array can store (32 elements i.e. integers in this case).
In C, first an array has to be declared explicitly before it can be used.
Manipulating array elements: -
Indexing Arrays: - 
After you declare an array, you can access each element of the array separately.
For instance, the following declaration declares an array of characters.
char month [12];
You can access the elements in the array of month one after another.
The important thing to remember is that all arrays in C are indexed starting at 0. In other words, the index of the first element in an array is 0 not 1. Therefore, the first element in the array of month is month [0]. Because there are 12 elements in the month array, the last element is month [11] and not month [12]. 
The 12 elements of the array have the following expression month [0], month [2], ……..month [10], month [11].
Because these expressions reference the elements in the array, they are sometimes called array element references.
Initializing Arrays: -
With the help of the array element references, you can initialize each element in an array. For instance, you can initialize the first element in the array of month, which was declared in the last section like this:
month [0] = ‘J’;
Here, the numeric value of ‘J’ is assigned to the first element of month, month [0].
Likewise, the statement month [1] = ‘F’ assign ‘F’ to the second element, month [1], in the array.
The second way to initialize an array is to initialize all elements in the array together. For instance, the following statement initializes an integer array.  
int numbers [5] = {10, 20, 30, 40, 50};
Here, the integer inside the braces ({and}) are assigned to the corresponding elements of the array numbers i.e. 
10 is given to numbers [0]
20 is given to numbers [1]
30 is given to numbers [2]
………………………….
50 is given to numbers [4]
The program below shows manipulating an array.
# include <stdio.h>
void main ( )
{
int i;
int list [10];
for (i = 0; i<10; i++)
{
list [i] = i+1;
printf (“\n List [%d] is initialized with %d”, i, list[i]);
}
}
The output of the preceding program is 
List [0] is initialized with 1
List [1] is initialized with 2
List [2] is initialized with 3
………………………….
List [9] is initialized with 10

Q.2 Write short notes on the following:
(i) Multidimensional arrays (ii)  Unknown or varying size array.
Ans. (i) Multidimensional Arrays: -
In addition to one-dimensional arrays, the C language also supports multidimensional arrays.
You can declare arrays with as many dimensions as your compiler allows.
The general form of declaring a N-dimensional array is data_type, array_name [array size1][array size2]…….[array size N];
Where N can be any positive integer.
The following example declares a two dimensional array int number [2] [3];
Here there are two pairs of brackets that represent two dimensions with a size of 2 and 3 integer elements, respectively.
You can initialize the two dimensional array number in the following way:
number [0][0] = 1;
number [0][1] = 2;
number [0][2] = 3;
number [1][0] = 4;
number [1][1] = 5;
number [1][2] = 6;
It is equivalent to following statement.
int number [2][3] = {1, 2, 3, 4, 5, 6};
you can also initialize the array in the following way.
int number [2][3] = {{1, 2, 3}, {4,5, 6}};
Printing Out A Two-Dimensional Array: -
# include <stdio.h>
# include <conio.h>
void main ( )
{
int two_dim [3][5] = {1, 2, 3, 4, 5, 
            10, 20, 30, 40, 50,
    100, 200, 300, 400, 500};
int i, j;
for (i = 0; i < 3; i++)
{
printf (“\n”);
for (j = 0; j < 5; j++)
printf (“%d”, two_dim[i][j]);
}
getch ( );
}
The following output is obtained when running the executable file of above program.
 1  2  3  4  5
10 20 30 40 50
100 200 300 400 500
(ii) Arrays Of Unknown Or Varying Size: -
The size of a dimension is normally given during the declaration of an array. It means that you have to count each element in an array. It could be tedious to do so, though especially if there are many elements in an array. The positive point is that the compiler-compiler can actually calculate a dimension size of an array automatically if an array is declared as an unsized array. For example, when the compiler sees the following unsized array: int list_num [ ] = {10, 20, 30, 40, 50, 60, 70, 80, 90}; it will create an array big enough to hold  all elements. Likewise you can create a multidimensional unsized array. However, you have to specify all but the left most (that is, the first) dimension size. For instance, the compiler can reserve enough memory space to hold all elements in the following two dimensional unsized array:
char list_ch [ ][2] = {
‘a’, ‘A’,
‘b’, ‘B’,
‘c’, ‘C’,
‘d’, ‘D’,
‘e’, ‘E’,
‘f’, ‘F’,
‘g’, ‘G’};
Initializing Unsized arrays: -
#  include <stdio.h>
# include <conio.h>
void main ( )
{
char array_ch [ ] = { ‘c’, ‘ ’
   ‘i’, ‘s’, ‘ ’
   ‘p’, ‘o’, ‘w’, ‘e’, ‘r’
   ‘f’, ‘u’, ‘1’, ‘1’, ‘\0’};
int list_int [ ][3] = {
1, 1, 1
2, 4, 8
3, 9, 27
     };
printf (“\n The size of array_ch [] is %d bytes”, size of (array_ch));
printf (“\n The size of list_-int [][3] is %d bytes”, size of (list_int));
getch ( );
}
The following output is obtained by running the executable.
The size of array_ch is 15 bytes.
The size of list_int is 18 bytes.