The document defines and describes different types of arrays in C programming. It states that arrays can hold multiple values of the same data type and are used to store data in linear or tabular form. The key types discussed are one-dimensional, two-dimensional, and multi-dimensional arrays. It provides examples and explains how to declare, initialize, search and sort each array type.
2. Definition :-Definition :-
• Arrays are complex variables that can hold multiple
values of the same data type.
• Array is a fixed type sequenced collection of elements of
the same data type.
• It is simply a grouping of like-type data.
• Some examples where a concept of arrays can be used :-
1) List of employees in an organization.
2) Exam scores of a class of students.
3) Table of daily rainfall data.
4. What is the Need of anWhat is the Need of an
Array ?Array ?
• To store large number of variables of same type
under a single variable.
• Easy understanding of the program.
• It provides a convenient structure for
representing data.
• We can use arrays to perform large amounts of
data in smaller values of commands and yet we
can have proper output.
5. Types of Arrays :-Types of Arrays :-
• There are basically three types of arrays used in a
C programming language :-
1) One-dimensional
2) Two-dimensional
3) Multi-dimensional
6. One-dimensional arrays :-One-dimensional arrays :-
• The array which is used to represent and store
data in a linear form is called as single or one
dimensional array.
• A structured collection of components all of the
same type, that is given a single name.
• Each component is accessed by an index that
indicates the component’s position within the
collection.
• Array position is always started at 0 and goes up
to one less then the size
7. Concept :-Concept :-
• Syntax :-
<data_type>
<array_name> [size];
• Total size (in bytes):-
total size = length of
array * size of data type
• The Memory allocation
of the one dimensional
array can be
understood from the
figure.
8. Declaration of One-Declaration of One-
dimensional arrays :-dimensional arrays :-
• Like any other variable, arrays must be declared before
they are used so that the compiler can allocate space for
them in memory. The general form of array declaration
is :-
type variable name[ size ];
• Here in above syntax ,
1) The type specifies the type of element that will be
contained in the array, such as int, float, or char.
2) The size indicates the maximum number of elements
that can be stored in array.
3) The Variable name indicates the name you want to
assign to the array.
9. Example :-Example :-
1. float height [50];
In above example, it declares the height to be an
array containing of 50 real elements. Any subjects
from 0 to 49 are valid.
2. int group[10];
In above example, it declares the group as an
array to contain a maximum of 10 integer constants.
10. Initialization of One-dimensionalInitialization of One-dimensional
Array :-Array :-
• After the array is declared, its elements must be
initialized.
• Otherwise, they will contain junk values.
• An array can be initialized at either of the
following stages :-
1) At compile time
2) At run time
11. Compile time initialization :-Compile time initialization :-
• Whenever we declare an array, we can initialize
array directly at compile time.
• Initialization of an array is known as compiler
time initialization if and only if we assign certain
set of values to array element before executing
program .i.e. at compilation time.
12. Explanation :-Explanation :-
• Compiler counts the number of elements written
inside pair of brackets and determines the size of
arrays.
• After counting the number of elements inside the
brackets, the size of an array is declared during
complete execution.
• This type of initialization is called as “Compile
time initialization”.
13. Example :-Example :-
1. float total [5] = {0.0,15.75,-10}
Here in above example, we initialize the first
three elements to 0.0, 15.75 and -10 and the
remaining two values to zero.
2. char name [ ] = {‘j’, ‘o’, ‘h’, ‘n’, ‘o’}
Here in above example, declares the name to be
an array of five characters, initialized with a string
john ending with the null character.
14. Run time initialization :-Run time initialization :-
• An array can be explicitly initialized at run time.
• This approach is usually applied for initializing
large arrays.
• Compile-time initialization can only be done
using constant expressions, but run-time
initialization can be done using any expression at
all.
15. Example :-Example :-
for (i=0; i<100, i=i+1)
{
if i<50
sum [i] = 0.0;
else
sum [i] = 1.0;
}
• Here, the first 50 elements of the array “sum” are
initialized to zero while the remaining 50 elements are
initialized to 1.0 at run time.
16. Searching and Sorting :-Searching and Sorting :-
• Searching and sorting are two most frequent
operations performed on arrays.
• Computer scientists have devised several data
structures and searching and sorting techniques
that facilitate rapid access to data stored in lists.
• More information on these two concepts are
provided further…..
17. Searching :-Searching :-
• Searching is a process of finding the location of
the specified element in a list.
• The specified element is often called the search
key.
• If the process of searching finds a match for the
search key with a list element value, the search is
said to be successful, Otherwise it is unsuccessful.
• Types of searching :
1) sequential search
2) binary search
18. Sorting :-Sorting :-
• Sorting is a process of arranging elements in the
list according to their values, in ascending or
descending order.
• A sorted list is called a ordered list.
• Many sorting techniques are available, but the
most commonly used are 3 techniques :-
1) bubble sort
2) selection sort
3) insertion sort
19. Two-dimensional arrays :-Two-dimensional arrays :-
• The array which is used to represent and store
data in a tabular form is called as two
dimensional array.
• Such type of array specially used to represent data
in a matrix form.
• Examples:
1) Lab book of multiple readings over
several days
2) Periodic table
3) Movie ratings by multiple reviewers.
- Each row is a different reviewer
- Each column is a different movie
20. Concept :-Concept :-
• Syntax :-
<data-type>
<array_nm>
[row_subscript]
[column_subscript]
• Memory allocation of
these type of arrays
can be understood
from the figure.
21. Declaration of Two-DimensionalDeclaration of Two-Dimensional
Arrays :-Arrays :-
• Like any other variable, arrays must be declared before
they are used so that the compiler can allocate space for
them in memory. The general form of array declaration is
type array_name [row_size] [column_size]
• Here in above syntax ,
1) The type specifies the type of element that will be
contained in the array, such as int, float, or char.
2) The size indicates the maximum number of elements that
can be stored columns and rows in array.
3) The Variable_name indicates the name you want to assign
to the array.
23. Initialization of Two-DimensionalInitialization of Two-Dimensional
Arrays :-Arrays :-
• Like one dimensional arrays, two-dimensional
arrays can be initialized by following their
declaration with a list of initial values enclosed in
brackets.
• Two-dimensional arrays may be initialized by
specifying bracketed values for each row and
column.
25. Multi-Dimensional Arrays :-Multi-Dimensional Arrays :-
• C allows arrays of three or more dimensions.
• The exact limit is determined by the compiler.
• The general syntax of a multi-dimensional array is
given below :-
type array_name [s1] [s2] [s3]…….[sn];
where ‘s’ is the size of the dimension
• For example,
int survey [3] [5] [12];
float table [5] [6] [7] [8];