0% found this document useful (0 votes)
6 views

Arrays

Uploaded by

tunknown359
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Arrays

Uploaded by

tunknown359
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Vidyavardhaka College of Engineering, Mysuru

Autonomous Institute, Affiliated to VTU


Accredited by NBA | NAAC with ‘A’ Grade

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)

• Consider an example, int marks [10];


Declares a variable marks to be an array containing 10 elements
• In C, array index starts from 0, meaning that array marks will contain 10
elements. The first element is stored in marks[0], second in marks[1]
and last element stored in marks[9]
Accessing the elements of an array
• For accessing the element in an array, the array subscript must be
used.
• For example, to access the 4th element of the array, we need to
write it as arr[3]. The subscript must be an integral value.
• To access all the elements of an array, one must use the loop.
int i, marks[10];
for(i=0;i<10;i++)
marks[i]=-1;
• The following code accesses every individual element of the array and
sets its value to -1.
• In the for loop, the first value marks[0] is set to -1 , then the value
of the index (i) is incremented and the next value is set to -1.

• 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

Consider the following example


marks[3]=100;
Here 100, is assigned to the fourth element of the array which is
specified as marks[3]
• We cannot assign one array to another array, even if the two arrays are of same
type and size
Operations on arrays
• There are many operations that can be performed on arrays. They include
• Traversal- Accessing each and every element of an array for a specific
purpose
• Insertion- Adding a new element to the existing array
• Deletion- Removing an element in the existing array
• Search-Find a particular value is present in the array or not
• Sort-Arranging the elements of the array in some relevant order either
ascending or descending order
• Merge- Merging two arrays into a third array means first copying the
contents of the first array into third array and then copying the
contents of the second array into the third array
Searching
• Find a particular value is present in the array or not
• If the value is present in the array, then searching is said to be
successful and it gives the location of that value in the array otherwise
search displays an unsuccessful search message
• There are two popular methods for searching the array elements
• Linear search
• Binary search
Sorting
• Arranging the elements of the array in some relevant order either
ascending or descending order
• There are various sorting methods
• Bubble sort
• Selection sort
• Merge sort
• Quick sort
• Insertion sort
• Radix/Bucket sort
• Shell sort
Two-dimensional arrays

You might also like