chaitra-1.pptx fake news detection using machine learning
Arrays.pptx
1. Arrays
• An array is defined as the collection of similar
type of data items stored at contiguous
memory locations.
• Arrays are the derived data type in C
programming language which can store the
primitive type of data such as int, char,
double, float, etc.
2. • C array is beneficial if you have to store similar
elements. For example, if we want to store the
marks of a student in 6 subjects, then we
don't need to define different variables for the
marks in the different subject.
• Instead of that, we can define an array which
can store the marks in each subject at the
contiguous memory locations.
3. Properties of Array
• Each element of an array is of same data type
and carries the same size, i.e., int = 4 bytes.
• Elements of the array are stored at contiguous
memory locations where the first element is
stored at the smallest memory location.
• Elements of the array can be randomly
accessed since we can calculate the address of
each element of the array with the given base
address and the size of the data element.
4. Advantage of C Array
• 1) Code Optimization: Less code to the access
the data.
• 2) Ease of traversing: By using the for loop, we
can retrieve the elements of an array easily.
• 3) Ease of sorting: To sort the elements of the
array, we need a few lines of code only.
• 4) Random Access: We can access any
element randomly using the array.
5. Disadvantage of C Array
• 1) Fixed Size: Whatever size, we define at the
time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size
dynamically
6. Declaration of C Array
• data_type array_name[array_size];
• Example:
• int marks[5];
7. Initialization of C Array
• marks[0]=80;//initialization of array
• marks[1]=60;
• marks[2]=70;
• marks[3]=85;
• marks[4]=75;
8.
9. Types of array
• One dimensional array: A one dimensional array
is a linear list consisting of related and similar
data items (or) only one subscripted array is
called as one or single dimensional arrays.
• Declaration and initialization of one dimensional
array:
• Array initialization in compile time
• int a[10]={10,20,30,40,50};
• one subscript
10. • Two Dimensional Arrays: Storing or
representing the data in two dimensional that
is in tabular form (or) two subscripted array is
called two dimensional array.
• It is also known as a matrix.
• Syntax: Data-type variable name [number of
rows][number of columns];
• Example: int a [3][4];
• Initialization:-
• int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
11. A 2D array’s elements are stored in continuous
memory locations. It can be represented in
memory using any of the following two ways:
1. Column-Major Order
2. Row-Major Order
1. Column-Major Order:
In this method the elements are stored column
wise, i.e. n elements of first column are stored in
first n locations, n elements of second column are
stored in next n locations and so on.
12. • 2. Row-Major Order:
In this method the elements are stored row wise, i.e.
m elements of first row are stored in first m
locations, m elements of second row are stored in
next m locations and so on.
• The array that we have in the example below is
having the dimensions 5 and 4.
• These dimensions are known as subscripts. So this
array has first subscript value as 5 and second
subscript value as 4.
• So the array abc[5][4] can have 5*4 = 20 elements.
13.
14. Multidimensional arrays
• A multi-dimensional array is an array that has
more than one dimension.
• It is an array of arrays; an array that has
multiple levels.
• The simplest multi-dimensional array is the 2D
array, or two-dimensional array.
15. • C allows arrays of three or more dimensions.
• The general form of a multi-dimensional array is
• Syntax:
• data_type
array_name[block_size][row_size][column_size];
• Example: int a[2][3][3];
•
16. int a[2][3][3];
//array of type integer
//number of blocks of 2D arrays:2 |rows:3 |columns:3
//number of elements:2*3*3=18 integer elements
can be stored.
block(1) 11 22 33 block(2) 12 13 14
44 55 66 21 31 41
77 88 99 12 13 14
3x3 3x3