Unit-III
ARRAYS
Introduction
✓ An array is a collection of homogeneous (similar) data items that are stored under
one common name.
✓ Individual data item (array elements) in an array is identified by index or subscript
enclosed in square brackets with array name.
✓ The elements in an array are stored in continuous memory location.
DECLARATION OF AN ARRAY
✓ Like other variable an array must be declared before they are used so that the
compiler can allocate space for them in memory.
✓ The syntax for array declaration is:
data type array_name [size];
✓ The data type specifies the array elements data type.
✓ Size indicates the maximum number of elements that can be stored in the array.
✓ For example
float height [10];
The above array declaration represents the array name is height, we can store a maximum
of 10 elements and the array elements are floating point data type.
ARRAY INITIALIZATION
The array elements can be initialized when they are declared like ordinary variables
otherwise they will take garbage values.
Syntax: data type array_name [size] = {value 0, value 1, . . . , value n-1)
The initialized values are specified within curly braces separated by commas.
Example: int Marks [3] = {70, 80, 90};
This statement declares the variable Marks as an array of 3 elements and will be assigned
the values specified in list as below.
70
Marks [0]
80
Marks [1]
90
Marks [2]
, Like ordinary variables, the values to the array can be initialized as follows.
int Marks[3];
Marks [0] = 70;
Marks [1] = 80;
Marks [2] = 90;
Character array can be initialized as follows:
char gender[2] = {'M','F'};
Classification of Array
✓ One Dimensional Array
✓ Two Dimensional Array
One dimensional array:
If the array has only one subscript then it is called one dimensional or single dimensional
array.
Syntax:
data type array_name [size];
Declaration Example:
int height[10], weight[10];
char name[20];
Initialization Example:
int marks[3] = {20,60,67};
Characteristics of One Dimensional Array
✓ Array size must be positive number.
✓ Array elements are counted from 0 to size-1.
✓ String arrays are terminated with null character ('\0').
Example Program 1:
// Program to print total marks of a student
# include <stdio.h>
void main()
{
int marks[10], i, n, Total = 0;
ARRAYS
Introduction
✓ An array is a collection of homogeneous (similar) data items that are stored under
one common name.
✓ Individual data item (array elements) in an array is identified by index or subscript
enclosed in square brackets with array name.
✓ The elements in an array are stored in continuous memory location.
DECLARATION OF AN ARRAY
✓ Like other variable an array must be declared before they are used so that the
compiler can allocate space for them in memory.
✓ The syntax for array declaration is:
data type array_name [size];
✓ The data type specifies the array elements data type.
✓ Size indicates the maximum number of elements that can be stored in the array.
✓ For example
float height [10];
The above array declaration represents the array name is height, we can store a maximum
of 10 elements and the array elements are floating point data type.
ARRAY INITIALIZATION
The array elements can be initialized when they are declared like ordinary variables
otherwise they will take garbage values.
Syntax: data type array_name [size] = {value 0, value 1, . . . , value n-1)
The initialized values are specified within curly braces separated by commas.
Example: int Marks [3] = {70, 80, 90};
This statement declares the variable Marks as an array of 3 elements and will be assigned
the values specified in list as below.
70
Marks [0]
80
Marks [1]
90
Marks [2]
, Like ordinary variables, the values to the array can be initialized as follows.
int Marks[3];
Marks [0] = 70;
Marks [1] = 80;
Marks [2] = 90;
Character array can be initialized as follows:
char gender[2] = {'M','F'};
Classification of Array
✓ One Dimensional Array
✓ Two Dimensional Array
One dimensional array:
If the array has only one subscript then it is called one dimensional or single dimensional
array.
Syntax:
data type array_name [size];
Declaration Example:
int height[10], weight[10];
char name[20];
Initialization Example:
int marks[3] = {20,60,67};
Characteristics of One Dimensional Array
✓ Array size must be positive number.
✓ Array elements are counted from 0 to size-1.
✓ String arrays are terminated with null character ('\0').
Example Program 1:
// Program to print total marks of a student
# include <stdio.h>
void main()
{
int marks[10], i, n, Total = 0;