Module 3 (Arrays 1) - 16857900840503 PDF
Module 3 (Arrays 1) - 16857900840503 PDF
(18CPS13)
MODULE 3:
Arrays
What is Array? Types of Arrays
1. One-dimensional arrays
•An array is a fixed-size sequential collection of elements of 2. Two-dimensional arrays
same data types that share a common name. 3. Multidimensional arrays
•It is simply a group of data types.
•An array is a derived data type. One-dimensional Arrays
•An array is used to represent a list of numbers , or a list of • It is a linear list consisting of related data items.
names.
• In memory, all the data items are stored in
For example,an array can be used contiguous memory location i.e. one after the
•List of employees in an organization. other.
•Test scores of a class of students. • Pictorial representation of single dimensional
array is given below.
• List of customers and their telephone numbers.
int a[6];
•List of students in the college.
For Example, Consider an array of marks of 5 students as
shown below
For example
int marks[5];
Array Size=5*2=10bytes
n items
2n if size of integer is 2
4n if size of integer is 4
Initialization of single dimensional arrays Initializing all specified memory locations
Datatype Array_Name[size]={list of values}; int a[5]={10,15,20,25,30}; a[0] a[1] a[2] a[3] a[4]
10 15 20 25 30
Where
• Data type can be int, float, char etc char b[6]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟};
•Array_name is the name of the array
•Size can be an expression should be evaluated to a positive Partial array Initialization
integer only or can be integer constant.
•List of values are array elements enclosed within ‘{‘ and ‘}’ int a[5]={10,15}; a[0] a[1] a[2] a[3] a[4]
separated by commas. 10 15 0 0 0
char b[7]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟};
The various ways of initializing arrays are:
•Initializing all specified memory locations Initialization without array size
•Partial array initialization
•Initialization without size int a[]={10,15,20,25,30};
•String initialization char b[]={„C‟,‟O‟,‟M‟,‟P‟,‟U‟,‟T‟,‟E‟};