Ans. : Array : Array is a derived data type. When it is necessary to store more than one value under a variable, user can make use of array. An array is a fixed-size sequence collection of elements of the same data type. It is simply a grouping of like-data type.
Different types of arrays : There are three types of arrays. They are,
Ü One dimensional array.
Ü Two dimensional array.
Ü Multidimensional array.
Qus. 02: Write down about the three types of arrays.
Ans. : One dimensional array : A list of items can be given one variable name using only one subscript and such a variable is called a one dimensional array.
Example : int number[5];
Here in the example, 5 value of the variable number can be kept under the single variable number.
Two dimensional array : When by using an array user can store two value, each for a row and a column under a variable, the array is then called a two dimensional array. Here, user can use infinite number of rows and columns.
Two dimensional arrays are declared as follows,
type array_name[row size][column size];
Multidimensional array : C allows arrays of three or more dimensions. The exact limit is determined by the compiler. The general form of a multidimensional array is,
type array_name[s1][s2]......[sn];
Where sn is the size of the dimension. For example,
int survey[3][5][12]
survey is a three dimensional array declared to contain 180 integer type elements.
Qus. 03: Write down about the declaration of one dimensional array.
Ans. : Declaration of one dimensional array : Like any other variable, arrays must be declared before they are used. The general form of array declaration is,
type variable_name[size];
The type specifies the type of elements that will be contained in the array, such as int, float etc. The size indicates the maximum number of element that can be stored inside the array. For example,
float height[50];
This declares the height to be an array containing 50 real numbers.
Qus. 04: Write down about the initialization of one dimensional array.
Or, Write down about the initialization of arrays.
Ans. : Initialization of one dimensional array : After an array is declared, its elements must be initialized. An array can be initialized either of the following stages,
§ At compile time.
§ At run time.
Compile time initialization : User can initialize the elements of an array in the same way as the ordinary variables when they are declared. This is compile time initialization. The general form is as follows,
type array_name[size]={list of values};
The values in the list are separated by commas. For example,
int number[3]={0,0,0};
This will declare the variable number as an array of size 3 and will assign 0 to each element.
Run time initialization : An array can be explicitly initialized at run time. This approach is usually applied for initializing large arrays. For example,
= = = = = = = = = =
for(i=0;i<100;i=i+1)
{
if (i<50)
sum[i]=0.0;
else
sum[i]=1.0;
}
= = = = = = = = = =
The first 50 elements of the array sum are initialized to zero while the remaining 50 elements are initialized to 1.0 at run time.
Qus. 05: Write down the initialization of two dimensional array. [not important]
Ans. : Initialization of two dimensional array : Like the one dimensional arrays, two dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. For example,
int table[2][3]={0,0,0,1,1,1};
This initializes the elements of the first row to 0 and the second row to 1. This statement can also be written as,
int table[2][3]={{0,0,0}, {1,1,1}};
This can also be written as,
int table[2][3]= {
{0,0,0},
{1,1,1}
};
Qus. 06: What are the static arrays and dynamic arrays ?
Ans. : Static arrays : The process of allocating memory at compile time is known as static memory allocation and the arrays that receives static memory allocation are called static arrays. This approach works fine as long as user know that what the data requirements are.
Dynamic arrays : The process of allocating memory at run time is known as dynamic memory allocation and the arrays that receives such kind of memory allocation are known as dynamic arrays.
Dynamic arrays are created using what are known as pointer variables and memory management functions malloc, calloc and realloc.ক্রেডিট : তন্ময় চক্রবর্তী
1 comments:
Well, you have mentioned the credit. Thats ok, but you could have told once that you are about to copy all my notes here as a book.
Its not a book, its my notes on C programming based on the book of C programming by E. Balaguruswamy.
Ok. Bye.
01. Overview of C
02. Constants, Variables & Data Types
03. Operators & Expressions
04. Managing I/O Operations
05. Decision Making & Branching
06. Decision Making & Looping
07. Arrays
08. User Defined Functions