Professional Documents
Culture Documents
Array
Array
Array:
An array is a fixed-sized sequenced collection of elements of the same data type. It is simply a
grouping of like-type data. Arrays are defined in much the same manner as ordinary variables,
except that each array must be accompanied by a size specification (i.e., number of elements).
Why use?
In many applications, we need to handle large volume of data in terms of reading, processing and
printing. Array can provide the facility of efficient storing, accessing and manipulation of such
large amounts of data items.
The main facility of using array is that it has the ability to use a single name to represent a
collection of items and to refer to an item by specifying the item number which enables us to
develop concise and efficient program.
2. How can we declare and initialize of one dimensional array and two dimensional array?
Ans:
type array-name[size];
The type specifies the type of element that will be contained in the array, such as int, float, or
char and the size indicates the maximum number of elements that can be stored inside the array.
For example,
char name[30];
will declare the variable number as an array of size 3 and will assign zero to each element.
Dr. Abu Nowshed Chy 1
Lecturer, Dept. of CSE, CU
Two dimensional array:
The general form of two-dimensional array declaration is:
type array-name[row_size][column_size];
The type specifies the type of element that will be contained in the array, such as int, float, or
char.
For example,
int grades[NUM_STUDENTS][NUM_TESTS];
initializes the elements of the first row to zero and the second row to one. The initialization is
done row by row.
Also initializes the two-dimensional array in the form of a matrix as shown below:
int table[2][3]={
{0,0,0},
{1,1,1}
};
But consider a situation where we want to use an array that can vary greatly in size. We must
guess what will be the largest size ever needed and create the array accordingly. A difficult task
in fact!
Other limitation:
o Because names of arrays represent just a pointer to the beginning of the array, we have some
limitations or problems.
o Another limitation comes with arrays being passed into functions. Take for example:
Both are the same declaration. As only the pointer to the array is passed in, not the whole
array. So what if you mistakenly did a sizeof(ia) inside func? Instead of returning the
sizeof the whole array, it would return the size of a pointer which corresponds to the word
size of the computer.
Ans:
The limitation of scanf ( ) function in case of taking string data is that %s specifier cannot be
used to read strings with blank spaces i.e.; it can read only the word before first space. But this
problem can be solved with the help of %[ ] specification. Blank spaces may be included within
the brackets, thus enabling the scanf ( ) to read strings with blank spaces.
6. Briefly define the memory representation of one and two dimensional array.
Ans:
Using the the base address of LA (denoted by Base(LA)), computer calculates the address of
elements of LA by following formula:
where w is the number of words per memory cell for the array LA.