SlideShare a Scribd company logo
Array
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
15
1
Outline
 What is An Array?
 Why do we need Array?
 Declaring An One-Dimensional Array
 Accessing Array Elements
 One-Dimensional Array
 Two-Dimensional Array
What is An Array?
 Arrays a kind of data structure that can store a fixed-
size sequential collection of elements of the same
type.
 An array is used to store a collection of data, but it is
often more useful to think of an array as a collection
of variables of the same type.
Why do we need Array?
 We can use normal variables (v1, v2, v3, ..) when we
have small number of objects, but if we want to store
large number of instances, it becomes difficult to
manage them with normal variables.The idea of array
is to represent many instances in one variable.
Defining An One-Dimensional Array
 Arrays are defined in much the same manner as
ordinary variables, except that each array name must
be accompanied by a size specification (the number of
elements).
 For one-dimensional array, the size is specified by a
positive integer expression, enclosed in square
brackets.
 The expression is usually written as
storage-class data-type array[ expression ];
Declaring An One-Dimensional Array
 Each array element can be referred to by specifying
the array name followed by one or more subscripts,
with each subscript enclosed in square brackets.
 Each subscript must be expressed as a nonnegative
integer.
 In an n-element array, the array elements are a[0],
a[1], a[2],…, a[n-1].
Declaring An One-Dimensional Array
(cont..)
 The number of subscripts determines the
dimensionality of the array.
 Here, 3rd element is referred by a[2], nth element is
referred by a[n-1].
Declaring An One-Dimensional Array
(Example)
 Several typical one-dimensional array definitions are shown
below:
int x[100];
char text[80];
static char message[25];
float n[12];
Here, the first line states that x is a 100-element integer array,
second line defines text to be an 80-element character array and
so on…
Initializing An One-Dimensional Array
 Arrays can be initialized C either one by one or using
a single statement as follows −
1. double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
2. double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
 In example 1, the number of values between braces
{ } cannot be larger than the number of elements that
we declare for the array between square brackets [ ].
Accessing Array Elements
balance[4] = 50.0;
 The above statement assigns the 5th element in the array
with a value of 50.0.All arrays have 0 as the index of their
first element which is also called the base index and the
last index of an array will be total size of the array minus
1.
One-Dimensional Array Example1
#include <stdio.h>
int main () {
int n[ 10 ]; /* n is an array of 10 integers */
int i, j;
for ( i = 0; i < 10; i++ ) {/* initialize elements of array n to 0 */
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
for (j = 0; j < 10; j++ ) {/* output each array element's value */
printf("Element[%d] = %dn", j, n[j] );
}
return 0;
}
One-Dimensional Array Example1
(cont..)
Output:
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
One-Dimensional Array Example2
int digit[4] = {1, 2, 3, 4}
float x[6] = {0, 0.25, 0, -0.50, 0, 0}
char color[3] = { ‘R’ ,‘E’ ,‘D’}
13
digit[0] = 1
digit[1] = 2
digit[2] = 3
digit[3] = 4
x[0] = 0
x[1] = 0.25
x[2] = 0
x[3] = -0.50
x[4] = 0
x[5] = 0
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
One-Dimensional Array Example3
int digit[] = {1, 2, 3, 4}
float x[] = {0, 0.25, 0, -0.50, 0, 0}
char color[] = { ‘R’ ,‘E’ ,‘D’}
14
digit[0] = 1
digit[1] = 2
digit[2] = 3
digit[3] = 4
x[0] = 0
x[1] = 0.25
x[2] = 0
x[3] = -0.50
x[4] = 0
x[5] = 0
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
One-Dimensional Array Example3
int digit[6] = {1, 2, 3, 4}
float x[6] = {0, 0.25, 0, -0.50, 0, 0}
char color[3] = { ‘R’ ,‘E’ ,‘D’}
15
digit[0] = 1
digit[1] = 2
digit[2] = 3
digit[3] = 4
digit[4] = 0
digit[5] = 0
x[0] = 0
x[1] = 0.25
x[2] = 0
x[3] = -0.50
x[4] = 0
x[5] = 0
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
One-Dimensional Array Example4
1. char color[3] = { “RED” }
2. char color[] = { “RED” }
16
1.
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
2.
color[0] = ‘R’
color[1] = ‘E’
color[2] = ‘D’
color[2] = ‘0’
Two-dimensional Arrays
 The simplest form of multidimensional array is the two-
dimensional array.
 A two-dimensional array is, in essence, a list of one-
dimensional arrays.
 To declare a two-dimensional integer array of size [x]
[y], you would write something as follows −
type arrayName [ x ][ y ];
 Where type can be any valid C data type and arrayName will be a
valid C identifier.
Two-dimensional Arrays (cont..)
 A two-dimensional array can be considered as a table which
will have x number of rows and y number of columns.
 A two-dimensional array a, which contains three rows and four
columns can be shown as follows −
Initializing Two-Dimensional Arrays
 Multidimensional arrays may be initialized by specifying bracketed values
for each row. Following is an array with 3 rows and each row has 4
columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /*initializers for row indexed by 2 */
};
 The nested braces, which indicate the intended row, are optional.
Initializing Two-Dimensional Arrays
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /*initializers for row indexed by 2 */
};
We can also initialize the previous example as below:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing Two-Dimensional Array Elements
 An element in a two-dimensional array is accessed by using the
subscripts, i.e., row index and column index of the array. For example −
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /*initializers for row indexed by 2 */
};
 int val = a[2][3];
 The above statement will take the 4th element from the 3rd row of the
array.
Two-Dimensional Array Example1
#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
for ( i = 0; i < 5; i++ ) {
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %dn", i,j, a[i][j] );
}
}
return 0;
}
Two-Dimensional Array Example1
Output:
– a[0][0]: 0
– a[0][1]: 0
– a[1][0]: 1
– a[1][1]: 2
– a[2][0]: 2
– a[2][1]: 4
– a[3][0]: 3
– a[3][1]: 6
– a[4][0]: 4
– a[4][1]: 8
Lecture 15 - Array

More Related Content

PPT
Lecture 16 - Multi dimensional Array
PPT
PPT
Arrays
PDF
ARRAYS
PPT
Arrays in c
PPT
Lecture 17 - Strings
PPT
C programming , array 2020
PPTX
C++ lecture 04
Lecture 16 - Multi dimensional Array
Arrays
ARRAYS
Arrays in c
Lecture 17 - Strings
C programming , array 2020
C++ lecture 04

What's hot (20)

PPTX
C programming slide c05
PPSX
C Programming : Arrays
PPTX
2- Dimensional Arrays
DOC
Arrays and Strings
PDF
Arrays and library functions
PPTX
Array in C
PPTX
Arrays in C language
PPT
Two dimensional array
PPTX
concept of Array, 1D & 2D array
PPTX
Python programming workshop
PDF
Multidimensional arrays in C++
PPT
Java: Introduction to Arrays
PPT
Lecture 18 - Pointers
PPTX
Arrays basics
PPTX
Programming in c arrays
PPTX
Programming in c Arrays
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Array in c
PPTX
Arrays in c
C programming slide c05
C Programming : Arrays
2- Dimensional Arrays
Arrays and Strings
Arrays and library functions
Array in C
Arrays in C language
Two dimensional array
concept of Array, 1D & 2D array
Python programming workshop
Multidimensional arrays in C++
Java: Introduction to Arrays
Lecture 18 - Pointers
Arrays basics
Programming in c arrays
Programming in c Arrays
Array Introduction One-dimensional array Multidimensional array
Array in c
Arrays in c
Ad

Similar to Lecture 15 - Array (20)

PDF
Arrays-Computer programming
PDF
Array and its types and it's implemented programming Final.pdf
PDF
PDF
SlideSet_4_Arraysnew.pdf
PPTX
Array.pptx
PDF
Arrays In C- Logic Development Programming
PDF
Introduction to Arrays in C
PDF
Array&amp;string
PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
Chapter 13.pptx
PDF
Array
PPT
Array in c
PPTX
array-160309152651.pptx
PPT
uderstanding arrays and how to declare arrays
PDF
02 arrays
PDF
11 1. multi-dimensional array eng
PPTX
Basic array in c programming
Arrays-Computer programming
Array and its types and it's implemented programming Final.pdf
SlideSet_4_Arraysnew.pdf
Array.pptx
Arrays In C- Logic Development Programming
Introduction to Arrays in C
Array&amp;string
Arrays 1D and 2D , and multi dimensional
Chapter 13.pptx
Array
Array in c
array-160309152651.pptx
uderstanding arrays and how to declare arrays
02 arrays
11 1. multi-dimensional array eng
Basic array in c programming
Ad

More from Md. Imran Hossain Showrov (18)

PPT
Lecture 22 - Error Handling
PPT
Lecture 21 - Preprocessor and Header File
PPT
Lecture 20 - File Handling
PPT
Lecture 19 - Struct and Union
PPT
Lecture 14 - Scope Rules
PPT
Lecture 13 - Storage Classes
PPT
Lecture 12 - Recursion
PPT
Lecture 11 - Functions
PPT
Lecture 10 - Control Structures 2
PPT
Lecture 8- Data Input and Output
PPT
Lecture 9- Control Structures 1
PPT
Lecture 7- Operators and Expressions
PPT
Lecture 6- Intorduction to C Programming
PPT
Lecture 5 - Structured Programming Language
PPT
Lecture 4- Computer Software and Languages
PPT
Lecture 3 - Processors, Memory and I/O devices
PPT
Lecture 2 - Introductory Concepts
PPT
Lecture 1- History of C Programming
Lecture 22 - Error Handling
Lecture 21 - Preprocessor and Header File
Lecture 20 - File Handling
Lecture 19 - Struct and Union
Lecture 14 - Scope Rules
Lecture 13 - Storage Classes
Lecture 12 - Recursion
Lecture 11 - Functions
Lecture 10 - Control Structures 2
Lecture 8- Data Input and Output
Lecture 9- Control Structures 1
Lecture 7- Operators and Expressions
Lecture 6- Intorduction to C Programming
Lecture 5 - Structured Programming Language
Lecture 4- Computer Software and Languages
Lecture 3 - Processors, Memory and I/O devices
Lecture 2 - Introductory Concepts
Lecture 1- History of C Programming

Recently uploaded (20)

PDF
English Language Teaching from Post-.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Onica Farming 24rsclub profitable farm business
English Language Teaching from Post-.pdf
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Anesthesia in Laparoscopic Surgery in India
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Microbial disease of the cardiovascular and lymphatic systems
Pharma ospi slides which help in ospi learning
Cardiovascular Pharmacology for pharmacy students.pptx
human mycosis Human fungal infections are called human mycosis..pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Onica Farming 24rsclub profitable farm business

Lecture 15 - Array

  • 2. Outline  What is An Array?  Why do we need Array?  Declaring An One-Dimensional Array  Accessing Array Elements  One-Dimensional Array  Two-Dimensional Array
  • 3. What is An Array?  Arrays a kind of data structure that can store a fixed- size sequential collection of elements of the same type.  An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
  • 4. Why do we need Array?  We can use normal variables (v1, v2, v3, ..) when we have small number of objects, but if we want to store large number of instances, it becomes difficult to manage them with normal variables.The idea of array is to represent many instances in one variable.
  • 5. Defining An One-Dimensional Array  Arrays are defined in much the same manner as ordinary variables, except that each array name must be accompanied by a size specification (the number of elements).  For one-dimensional array, the size is specified by a positive integer expression, enclosed in square brackets.  The expression is usually written as storage-class data-type array[ expression ];
  • 6. Declaring An One-Dimensional Array  Each array element can be referred to by specifying the array name followed by one or more subscripts, with each subscript enclosed in square brackets.  Each subscript must be expressed as a nonnegative integer.  In an n-element array, the array elements are a[0], a[1], a[2],…, a[n-1].
  • 7. Declaring An One-Dimensional Array (cont..)  The number of subscripts determines the dimensionality of the array.  Here, 3rd element is referred by a[2], nth element is referred by a[n-1].
  • 8. Declaring An One-Dimensional Array (Example)  Several typical one-dimensional array definitions are shown below: int x[100]; char text[80]; static char message[25]; float n[12]; Here, the first line states that x is a 100-element integer array, second line defines text to be an 80-element character array and so on…
  • 9. Initializing An One-Dimensional Array  Arrays can be initialized C either one by one or using a single statement as follows − 1. double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; 2. double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};  In example 1, the number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
  • 10. Accessing Array Elements balance[4] = 50.0;  The above statement assigns the 5th element in the array with a value of 50.0.All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1.
  • 11. One-Dimensional Array Example1 #include <stdio.h> int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i, j; for ( i = 0; i < 10; i++ ) {/* initialize elements of array n to 0 */ n[ i ] = i + 100; /* set element at location i to i + 100 */ } for (j = 0; j < 10; j++ ) {/* output each array element's value */ printf("Element[%d] = %dn", j, n[j] ); } return 0; }
  • 12. One-Dimensional Array Example1 (cont..) Output: Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
  • 13. One-Dimensional Array Example2 int digit[4] = {1, 2, 3, 4} float x[6] = {0, 0.25, 0, -0.50, 0, 0} char color[3] = { ‘R’ ,‘E’ ,‘D’} 13 digit[0] = 1 digit[1] = 2 digit[2] = 3 digit[3] = 4 x[0] = 0 x[1] = 0.25 x[2] = 0 x[3] = -0.50 x[4] = 0 x[5] = 0 color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’
  • 14. One-Dimensional Array Example3 int digit[] = {1, 2, 3, 4} float x[] = {0, 0.25, 0, -0.50, 0, 0} char color[] = { ‘R’ ,‘E’ ,‘D’} 14 digit[0] = 1 digit[1] = 2 digit[2] = 3 digit[3] = 4 x[0] = 0 x[1] = 0.25 x[2] = 0 x[3] = -0.50 x[4] = 0 x[5] = 0 color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’
  • 15. One-Dimensional Array Example3 int digit[6] = {1, 2, 3, 4} float x[6] = {0, 0.25, 0, -0.50, 0, 0} char color[3] = { ‘R’ ,‘E’ ,‘D’} 15 digit[0] = 1 digit[1] = 2 digit[2] = 3 digit[3] = 4 digit[4] = 0 digit[5] = 0 x[0] = 0 x[1] = 0.25 x[2] = 0 x[3] = -0.50 x[4] = 0 x[5] = 0 color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’
  • 16. One-Dimensional Array Example4 1. char color[3] = { “RED” } 2. char color[] = { “RED” } 16 1. color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’ 2. color[0] = ‘R’ color[1] = ‘E’ color[2] = ‘D’ color[2] = ‘0’
  • 17. Two-dimensional Arrays  The simplest form of multidimensional array is the two- dimensional array.  A two-dimensional array is, in essence, a list of one- dimensional arrays.  To declare a two-dimensional integer array of size [x] [y], you would write something as follows − type arrayName [ x ][ y ];  Where type can be any valid C data type and arrayName will be a valid C identifier.
  • 18. Two-dimensional Arrays (cont..)  A two-dimensional array can be considered as a table which will have x number of rows and y number of columns.  A two-dimensional array a, which contains three rows and four columns can be shown as follows −
  • 19. Initializing Two-Dimensional Arrays  Multidimensional arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row has 4 columns. int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /*initializers for row indexed by 2 */ };  The nested braces, which indicate the intended row, are optional.
  • 20. Initializing Two-Dimensional Arrays int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /*initializers for row indexed by 2 */ }; We can also initialize the previous example as below: int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
  • 21. Accessing Two-Dimensional Array Elements  An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example − int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /*initializers for row indexed by 2 */ };  int val = a[2][3];  The above statement will take the 4th element from the 3rd row of the array.
  • 22. Two-Dimensional Array Example1 #include <stdio.h> int main () { /* an array with 5 rows and 2 columns*/ int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; int i, j; for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i,j, a[i][j] ); } } return 0; }
  • 23. Two-Dimensional Array Example1 Output: – a[0][0]: 0 – a[0][1]: 0 – a[1][0]: 1 – a[1][1]: 2 – a[2][0]: 2 – a[2][1]: 4 – a[3][0]: 3 – a[3][1]: 6 – a[4][0]: 4 – a[4][1]: 8