WELCOME
PRESENTED BY
SHEYONA G
CHAPTER-06
Arrays
Arrays
Array is a fixed-size sequenced collection of related data items that share a common name.
It is simply a grouping of similar type of data
Array is a derived data type
There are three types of arrays. They are
One-dimensional arrays
Two-dimensional arrays
Multi-dimensional arrays
One-dimensional arrays
A list of items can be given one variable name using only one subscript and such a variable is called a
single subscripted variable or a one-dimensional array
Declaration of One-dimensional arrays:
data type variable-name[size];
Datatype can be int, float, char, etc
The size indicates the maximum number of elements that can be stored inside the array.
For example:
int number[5];
Here 0,1,2,3,4 are the subscripts of the array number.
Subscript is also known as index.
The array is indexed from zero to its maximum size minus one.
The values to the array elements can be assigned as follows:
number [0] = 35;
number [1] = 40;
number [2] = 20;
number [3] = 57;
number [4] = 19;
Example 2:
char name[10]; = “WELL DONE”
It declares the name as a character array(string) variable that can hold a maximum of 10 characters.
Initialization of One-dimensional arrays:
An array can be initialized at two stages:
At Compile Time
At Run Time
datatype array-name [size] = { list of values };
The values in the list are separated by commas.
Example 1: int number [3] = { 1,5,6 };
Example 2: float total [5] ={0.0, 15.75, -10}; // the remaining two elements will be considered as zero
Example 3: int counter [ ] = {1, 1, 1, 1};// here the counter array initialized with four elements(1’s)
Example 4: char name [ ] = { ‘J’ , ’O’ , ’H’ , ’N’ , ’\0’ };
Or char name [ ] = “JOHN”;
Example 5: char city [5] = { ‘B’ }; will initialize the first element to ‘B’ and the remaining four to NULL.
Run Time Initialization:
An array can be initialized at run time
for ( i = 0; i < 100; i++ )
{
if ( i < 50 )
sum [i] = 0.0;
else
}
sum [i] = 1.0;
Two-dimensional arrays
A Two-dimensional array is a collection of rows and columns.
Each element of the array can be identified using two subscripts.
Declaration of Two-dimensional arrays:
The general form of Two-dimensional array declaration:
datatype array-name [row-size] [column-size];
The datatype can be int, float, char, etc.
The row-size indicates the maximum number of rows and column size indicates the maximum number of
columns that can be stored in the array.
example:
int table[2][3];
Representation of a two-dimensional array in memory:
Consider int a[4][3];
Initializing Two-dimensional arrays:
int table[2][3] = { 0, 0, 0, 1, 1, 1 };
Initialize the elements of the first row to zero and the second row to one.
Different type of Two-dimensional array initializations
The initialization is done row by row. That is, int table[2][3] = { { 0, 0, 0 }, { 1, 1, 1 } }; Here the elements of
each row are separating by braces and each pair of braces should be separated by comma.
The initialization is done in the form of a matrix.
int table[2][3] = {
{ 0, 0, 0 },
{ 1, 1, 1 }
};
Multidimensional arrays
Declaration of multi-dimensional arrays:
The general form of a multi-dimensional array declaration is :
data_type array_name [s1][s2][s3]…..[sn];
Where si is the size of the ith dimension.
Examples :
int survey[3][5][12];
float table[5][4][5][3];
ASSIGNMENT
What is the size of the array int a[10]; 10 bytes 20 bytes 0 bytes 5 bytes
In C language, array is a ________ data type. fundamental primitive datatype user defined derived data
datatype datatype type
The_______is the syntax of declaring one dimensional array. datatype arrayname arrayname[si datatype [size];
arrayname[size]; datatype [size]; ze];
int a[10]; array can store maximum _____number of elements. 10 20 9 1
The process of arranging the array elements in ascending or Sorting searching finding deletion
descending order is known as
In C language, an array can be initialized at ________ both compile time compile time only run time only declaration
and run time time only
Which amongst the following process is used for allocating memory static memory dynamic memory stored function
at compile time in C language. allocation allocation procedures
In array we can access individual elements by using ________ scripts elements subscripts both a & c
In C language system, A string is a collection of ____________. characters integers floats symbols
In a programming language, array is a collection of elements of the values data type memory variable
same
What is the size of the array char a[10]; 10 bytes 20 bytes 0 bytes 5 bytes
What is the maximum number of elements can be stored in the array 10 20 5 30
int a[20];
What is the maximum number of elements can be stored in the array
char a[20]; 20 19 10 5
What is the size of the array int a[2][5]; 10 bytes 20 bytes 0 bytes 5 bytes
What is the size of the array char a[5][2]; 10 bytes 20 bytes 0 bytes 5 bytes
What is the maximum number of elements can be stored in the array
int a[2][2]; 8 4 16 30
Which among the following is a wrong statement? int a[2][2]; int a[2,2] int a[2][3][4]; int a[10];
In C language,when initializing 2-D array at compile time which size is row size column size row and column integer size
necessary? size
What is the maximum number of elements can be stored in the array 14 24 9 10
int a[2][3][4];
single
An array that uses more than two subscript is known as ____ multidimensional dimensional 1-D array 2-D array
array array
The process of finding the location of the specified element in a list is declaration compilation initialization searching
called _________
1) What is an array? What are the different types of arrays? Write the general form of declaration of different types
of arrays
2) Write one example for initializing one dimensional integer array at compile time
3) What is the syntax of declaring multi dimensional arrays?
4) Write one example for initializing two dimensional integer arrays at compile time
5) Give the number of elements and size in memory for the following
a)float areas[7][8];
6) Write a C program to create one dimensional integer array and store some elements at run time and display the
elements.
7) Write a C program to create a one dimensional integer array and store some elements and display only the even
numbers.