0% found this document useful (0 votes)
11 views13 pages

ARRAY

The document provides an overview of arrays in computer programming, defining them as fixed-size collections of elements of the same data type. It details the types of arrays (one-dimensional, two-dimensional, and multidimensional), their characteristics, and the syntax for declaration, initialization, and accessing elements. Examples illustrate how to declare and initialize both one-dimensional and two-dimensional arrays, including compile-time and run-time initialization methods.

Uploaded by

nitinchavda2615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

ARRAY

The document provides an overview of arrays in computer programming, defining them as fixed-size collections of elements of the same data type. It details the types of arrays (one-dimensional, two-dimensional, and multidimensional), their characteristics, and the syntax for declaration, initialization, and accessing elements. Examples illustrate how to declare and initialize both one-dimensional and two-dimensional arrays, including compile-time and run-time initialization methods.

Uploaded by

nitinchavda2615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Programming

Fundamentals
Arrays
Introduction to an Array
Define Array: An array is a fixed-size sequenced collection of elements of the same
data type.

For e.g. an array can be used to represent a list of numbers, or a list of names.

Types of Arrays:
1. One-dimensional arrays.
2. Two-dimensional arrays.
3. Multidimensional arrays.
Characteristics of an array
1. An array holds elements that have the same data type
2. Array elements are stored in subsequent memory locations
3. Two-dimensional array elements are stored row by row in subsequent memory
locations.
4. Array name represents the address of the starting element
5. Array size should be mentioned in the declaration.
One dimensional array: Declaration,
initialization and accessing
Declaration Syntax:
datatype array_name[size];
• data type: it defines the datatype, like int,float,char,double etc.
• array_name: it is the name of array.
• size: It represents the size of the array.
• E.g. int number[5];
• The computer reserves five storage location as follows:
number[0]
number[1]
number[2]
number[3]
number[4]
One dimensional array: Declaration,
initialization and accessing
Initialization Syntax:
1. Compile time:
datatype arrayname[size]={List of value};
e.g. int number[5]={10,20,30,40,50};
The computer reserves and stores five numbers as follows:
10 number[0]
20 number[1]
30 number[2]
40 number[3]
50 number[4]
One dimensional array: Declaration,
initialization and accessing
• float total [5]={4.5,2.5,0.2};
Above syntax will initialize first three elements to 4.5, 2.5 and 0.2 and remaining two
elements to zero.
• char name[]={‘J’, ’O’, ‘H’, ‘N’, ’\0’};
Above syntax will declare name to be an array of five characters, initialized with
“JOHN ” and ending with NULL character.
• char name[]=“JOHN”;
Above syntax will assign string literal directly.
• int number[3]={11,67,85,45,90};
Above syntax will produce compile time error as we have more initializers than
declared size.
One dimensional array: Declaration,
initialization and accessing
2. Run time initialization:
• To insert element at specific position
scanf("%d", &arr[3]); / / will insert element at index 3, i.e. 4th position

• To insert element at each index


for(i=0 ; i<5; i++)
{
scanf(“%d”,&a[i])
}
Example: Program to read 5 elements of array and print sum of it.
Two-dimensional array: Declaration, initialization and accessing
• Declaration of array:
Syntax: datatype arrayname [row size] [column size];
Example: int x[3][3] ; where, int is datatype, x is a arrayname , row size is 3 and column
size is 3.
• Representation in memory
Two-dimensional array: Declaration, initialization and accessing
• Initialization of array(compile time):
int table[2][3]={0,0,0,1,1,1};
or
int table[2][3]={{0,0,0},{1,1,1}};
In the above statements an array of two rows and three columns is declared and first row
is initialized with {0,0,0} and second row with {1,1,1}.

• int table[2][3]={{1,2},{3}}
In the above syntax first two elements of row1 is initialized to {1,2} and first
element of row2 is initialized with 3 and all other elements are initialized with 0.
• int table[ ][3]={{0,0,0},{1,1,1}};
When the array is fully initialized we can skip the size of first dimension.
Two-dimensional array: Declaration, initialization and accessing
• Initialization of array(run time):
1. To insert element at specific position
scanf("%d", &arr[0][0]); / / will insert element at row index 0 and column index 0

2. To insert element at each row and column indexes


int table[2][3];
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
scanf(“%d”,&table[i][j]);
}
}

You might also like