Arrays
Arrays
MODULE 3
ARRAYS
Shashank N
CSE “I” Assistant Professor
Dept. of CSE
Our Vision: “VVCE shall be a leading Institution in engineering and management education enabling individuals for significant contribution to the society”
22/01/2025 1
Contents
• Introduction
• Declaration of arrays
• Accessing the elements of an array
• Storing values in arrays
• Operations on arrays- Searching and Sorting
• Two-dimensional arrays and operations
Introduction
• Consider a situation, there are 20 students in a class. We need to
write a program, that reads and prints the marks of all those 20
students
• However, imagine if it will be possible to follow this approach if we
had to read and print marks of the students
• In the entire course (100 students)
• In the entire college (500 students)
• In the entire university (10,000 students)
• The answer is no. To process the large amounts of data, we need a
data structure known as array
• An array is a collection of similar data elements. These data
elements have the same data type
• The elements of the array are stored in consecutive memory locations,
commonly known as index (also known as subscripts). Basically,
subscripts indicate an ordinal number of elements counted from the
beginning of the array
• Some popular examples where the concept of array can be included are
• List of temperature recorded on every day of a month
• List of employees in a company
• List of students in a class
• List of products sold
• List of customers
Declaration of arrays
• We know that, a variable must be declared at the beginning of the
program
• Similarly, an array can also be declared as a normal variable before
the beginning of the program
• Declaring the array means one must specify three things
• Data type- kind of values it can store- int, float, char, double
• Name- to identify the array
• Size- the maximum number of values that the array can hold
• The syntax to declare the array is
type name [size];
• Here the type is any valid data type
• The number in the brackets indicates the size of the array (the
maximum number of elements that an array can store the values)
• The procedure continues until all the 10 elements of the array are set
to -1.
Storing values in arrays
• When we declare and define an array, we just allocate the space for
elements, but no values are stored in the array
• To store the value in the array, there are three ways.
• First- to initialize the array elements
• Second- to input a value for every individual element
• Third- to assign the values to the elements
Initialization of arrays
• Elements in the array can also be initialized at the time of declaration
like any other variable
• When an array needs to be initialized, we need to provide the value
for every element in the array
• Arrays can be initialized by the syntax
type array_name [size]= {list of values};
• The values are written with curly braces and every value must be
comma separated
int marks[5]={90, 82, 78, 95, 88}
Declares an array marks which has enough space to store 5 elements
• While initializing the array at the declaration, the programmer may omit to
mention the size of the array
For example, int marks[]={98,97,90};
This statement is legal. Here in this case, the compiler will provide
enough space for all the initialized elements.
• If the number of values provided is less than the number of elements in the
array, the unassigned values are filled with zeros
• If we have more initializers than the declared size of the array, then the
compile time error will be generated.
For example, int marks[3]={98,97,90,100};
Inputting the values
• An array can be filled by inputting values from the standard input
device
• In this method, a do-while/while/for loop is executed to input the
value of each element of the array.
• Consider the code
//input the values for each element in the array
int i, marks[10];
for(i=0;i<10;i++)
scanf(“%d”, &marks[i]);
Assigning the values
• The third way to assign values to individual elements of array is using an
assignment operator
• Any value that evaluates to an appropriate data type as that of the array can be
assigned to the individual array element