SlideShare a Scribd company logo
(Managed By Shree Tapi Brahmcharyashram Sabha)
Shree Swami AtmanandVidhya Sankul, Kapodra,Varachha Road, Surat, Gujarat, India. 395006
Phone: 0261-2573552 Fax No.: 0261-2573554 Email: ssasit@yahoo.in Web: www.ssasit.ac.in
Shree Swami Atmanand Saraswati Institute of Technology,
Surat
Arrays1
Presented by:
Patel Raj G
Arrays2
Arrays
 Suppose, you need to store years of 100 cars. Will you define
100 variables?
int y1, y2,…, y100;
 An array is an indexed data structure to represent several
variables having the same data type: int y[100];
3
y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
One-Dimensional Arrays (cont’d)
 An element of an array is accessed using the array name
and an index or subscript, for example: y[5] which can be
used like a variable
 In C, the subscripts always start with 0 and increment by 1,
so y[5] is the sixth element
 The name of the array is the address of the first element
and the subscript is the offset
4
y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
Definition and Initialization
 An array is defined using a declaration statement.
data_type array_name[size];
allocates memory for size elements
subscript of first element is 0
subscript of last element is size-1
size must be a constant
5
Example
int list[5];
 allocates memory for 5 integer variables
 subscript of first element is 0
 subscript of last element is 4
 C does not check bounds on arrays
 list[6] =5; /* may give segmentation fault or overwrite
other memory locations*/
6
list[0]
list[1]
list[2]
list[3]
list[4]
Initializing Arrays
 Arrays can be initialized at the time they are declared.
 Examples:
double rate[3] ={0.15, 0.25, 0.3};
char list[5] = {‘h’, ’e’, ’l’, ’l’, ’o’};
double vector[100] = {0.0}; /* assigns
zero to all 100 elements */
int s[] = {5,0,-5}; /*the size of s is 3*/
7
Assigning values to an array8
For loops are often used to assign values to an array
Example:
int list[5], i;
for(i=0; i<5; i++){
list[i] = i;
}
list[0]
list[3]
list[4]
list[1]
list[2]
0
1
2
3
4
list[0]
list[1]
list[2]
list[3]
list[4]
OR
for(i=0; i<=4; i++){
list[i] = i;
}
Matrices (2D-array)
 A matrix is a set of numbers arranged in a grid with rows and
columns.
 A matrix is defined using a type declaration statement.
datatype array_name[row_size][column_size];
int matrix[3][4];
9
in memory
Column 0 Column 1 Column 2 Column 3
Row 0
Row 1
Row 2
4
1
0
2
-1
2
4
3
0
-1
3
1
4 1 0 2
-1 2 4 3
0 -1 3 1
Accessing Array Elements
int matrix[3][4];
 matrix has 12 integer elements
 matrix[0][0] element in first row, first column
 matrix[2][3] element in last row, last column
 matrix is the address of the first element
 matrix[1] is the address of the Row 1
 matrix[1] is a one dimensional array (Row 1)
10
Initialization
int x[4][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
int x[][4] = { {2, 3, 7, 2},
{7, 4, 5, 9},
{5, 1, 6, -3},
{2, 5, -1, 3}};
11
Initialization
int i, j, matrix[3][4];
for (i=0; i<3; i++)
for (j=0; j<4; j++)
matrix[i][j] = i;
12
j
0 1 2 3
0
1
2
i
0 0 0 0
1 1 1 1
2 2 2 2
j
0 1 2 3
0
1
2
i
0 1 2 3
0 1 2 3
0 1 2 3
matrix[i][j] = j;
Multidimensional Arrays
 An array can have many dimensions – if it has more than
one dimension, it is called a multidimensional array
 Each dimension subdivides the previous one into the
specified number of elements
 Each dimension has its own length constant
 Because each dimension is an array of array references,
the arrays within one dimension can be of different lengths
these are sometimes called ragged arrays
13
Strings
 A string is an array of characters
char data[10] = “Hello”;
char data2[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘0’}
 Use printf to print strings
printf(“%s”,data);
 Can be accessed char by char
data[0] is first character
14
H e l l o 0
0 1 2 3 4 5 6 7 8 9
data
End of String
Symbol
Strings
 Each character has an integer representation
15
a b c d e z…………
97 98 99 100 101 ………………………112
A B C D E Z…………
65 66 67 68 69 ……………………… 90
0 1 2 3 4 98765
48 49 50 51 52 53 54 55 56 57
0
0
n
10
Strings
 Characters can be interpreted as integers
char c = ‘A’;
printf(“%c n”,c);
prints A
printf(“%d n”,c);
prints 65
Printf(“%c n”,65);
prints A
16
17

More Related Content

PPT
Lecture 16 - Multi dimensional Array
PPT
Multi dimensional arrays
PDF
11 1. multi-dimensional array eng
PPTX
PDF
C Language Lecture 10
PPTX
Array Data Structures
PPTX
Presentation on array
PDF
Day 2 repeats.pptx
Lecture 16 - Multi dimensional Array
Multi dimensional arrays
11 1. multi-dimensional array eng
C Language Lecture 10
Array Data Structures
Presentation on array
Day 2 repeats.pptx

What's hot (19)

PPTX
Multi-Dimensional Lists
PPTX
Basic data structures in python
PPTX
PPT
Algo>Arrays
PPTX
Fuzzy sets
PPTX
Chap1 array
PDF
List , tuples, dictionaries and regular expressions in python
PDF
Day 4a iteration and functions.pptx
PPTX
Array
PDF
Day 4b iteration and functions for-loops.pptx
PPTX
2D-Array
PDF
PPTX
Python Lecture 11
PPTX
Working with arrays in php
PDF
Basic R Data Manipulation
PPT
Trie tree
PDF
Coding test review
PPTX
Trie (1)
Multi-Dimensional Lists
Basic data structures in python
Algo>Arrays
Fuzzy sets
Chap1 array
List , tuples, dictionaries and regular expressions in python
Day 4a iteration and functions.pptx
Array
Day 4b iteration and functions for-loops.pptx
2D-Array
Python Lecture 11
Working with arrays in php
Basic R Data Manipulation
Trie tree
Coding test review
Trie (1)
Ad

Similar to Array (20)

PPTX
Arrays 1D and 2D , and multi dimensional
PPT
SP-First-Lecture.ppt
PPT
Lecture 15 - Array
PPTX
Array ppt you can learn in very few slides.
PDF
Introduction to Arrays in C
PPTX
Data structure array
PPTX
C (PPS)Programming for problem solving.pptx
PPT
Mesics lecture 8 arrays in 'c'
PPTX
Arrays in C language
PPTX
PPTX
Arrays in c
PPTX
Chapter 13.pptx
PPT
Comp102 lec 8
DOC
Arrays and Strings
PPTX
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
PDF
Array in C.pdf
PDF
Array.pdf
Arrays 1D and 2D , and multi dimensional
SP-First-Lecture.ppt
Lecture 15 - Array
Array ppt you can learn in very few slides.
Introduction to Arrays in C
Data structure array
C (PPS)Programming for problem solving.pptx
Mesics lecture 8 arrays in 'c'
Arrays in C language
Arrays in c
Chapter 13.pptx
Comp102 lec 8
Arrays and Strings
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
Array in C.pdf
Array.pdf
Ad

More from Patel Raj (6)

PPTX
disposal of solid waste
PPTX
Hydrography
PPTX
Inner Product Space
PPT
Communication Skill
PPTX
Water resources
PPTX
Nanorobotics13.ppt
disposal of solid waste
Hydrography
Inner Product Space
Communication Skill
Water resources
Nanorobotics13.ppt

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Well-logging-methods_new................
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Construction Project Organization Group 2.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Lecture Notes Electrical Wiring System Components
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
web development for engineering and engineering
DOCX
573137875-Attendance-Management-System-original
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
additive manufacturing of ss316l using mig welding
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Internet of Things (IOT) - A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
Well-logging-methods_new................
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Construction Project Organization Group 2.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Structs to JSON How Go Powers REST APIs.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Digital Logic Computer Design lecture notes
Lecture Notes Electrical Wiring System Components
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
web development for engineering and engineering
573137875-Attendance-Management-System-original
CH1 Production IntroductoryConcepts.pptx
additive manufacturing of ss316l using mig welding

Array

  • 1. (Managed By Shree Tapi Brahmcharyashram Sabha) Shree Swami AtmanandVidhya Sankul, Kapodra,Varachha Road, Surat, Gujarat, India. 395006 Phone: 0261-2573552 Fax No.: 0261-2573554 Email: ssasit@yahoo.in Web: www.ssasit.ac.in Shree Swami Atmanand Saraswati Institute of Technology, Surat Arrays1
  • 3. Arrays  Suppose, you need to store years of 100 cars. Will you define 100 variables? int y1, y2,…, y100;  An array is an indexed data structure to represent several variables having the same data type: int y[100]; 3 y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
  • 4. One-Dimensional Arrays (cont’d)  An element of an array is accessed using the array name and an index or subscript, for example: y[5] which can be used like a variable  In C, the subscripts always start with 0 and increment by 1, so y[5] is the sixth element  The name of the array is the address of the first element and the subscript is the offset 4 y[0] y[1] y[2] … y[k-1] y[k] y[k+1] … y[98] y[99]
  • 5. Definition and Initialization  An array is defined using a declaration statement. data_type array_name[size]; allocates memory for size elements subscript of first element is 0 subscript of last element is size-1 size must be a constant 5
  • 6. Example int list[5];  allocates memory for 5 integer variables  subscript of first element is 0  subscript of last element is 4  C does not check bounds on arrays  list[6] =5; /* may give segmentation fault or overwrite other memory locations*/ 6 list[0] list[1] list[2] list[3] list[4]
  • 7. Initializing Arrays  Arrays can be initialized at the time they are declared.  Examples: double rate[3] ={0.15, 0.25, 0.3}; char list[5] = {‘h’, ’e’, ’l’, ’l’, ’o’}; double vector[100] = {0.0}; /* assigns zero to all 100 elements */ int s[] = {5,0,-5}; /*the size of s is 3*/ 7
  • 8. Assigning values to an array8 For loops are often used to assign values to an array Example: int list[5], i; for(i=0; i<5; i++){ list[i] = i; } list[0] list[3] list[4] list[1] list[2] 0 1 2 3 4 list[0] list[1] list[2] list[3] list[4] OR for(i=0; i<=4; i++){ list[i] = i; }
  • 9. Matrices (2D-array)  A matrix is a set of numbers arranged in a grid with rows and columns.  A matrix is defined using a type declaration statement. datatype array_name[row_size][column_size]; int matrix[3][4]; 9 in memory Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2 4 1 0 2 -1 2 4 3 0 -1 3 1 4 1 0 2 -1 2 4 3 0 -1 3 1
  • 10. Accessing Array Elements int matrix[3][4];  matrix has 12 integer elements  matrix[0][0] element in first row, first column  matrix[2][3] element in last row, last column  matrix is the address of the first element  matrix[1] is the address of the Row 1  matrix[1] is a one dimensional array (Row 1) 10
  • 11. Initialization int x[4][4] = { {2, 3, 7, 2}, {7, 4, 5, 9}, {5, 1, 6, -3}, {2, 5, -1, 3}}; int x[][4] = { {2, 3, 7, 2}, {7, 4, 5, 9}, {5, 1, 6, -3}, {2, 5, -1, 3}}; 11
  • 12. Initialization int i, j, matrix[3][4]; for (i=0; i<3; i++) for (j=0; j<4; j++) matrix[i][j] = i; 12 j 0 1 2 3 0 1 2 i 0 0 0 0 1 1 1 1 2 2 2 2 j 0 1 2 3 0 1 2 i 0 1 2 3 0 1 2 3 0 1 2 3 matrix[i][j] = j;
  • 13. Multidimensional Arrays  An array can have many dimensions – if it has more than one dimension, it is called a multidimensional array  Each dimension subdivides the previous one into the specified number of elements  Each dimension has its own length constant  Because each dimension is an array of array references, the arrays within one dimension can be of different lengths these are sometimes called ragged arrays 13
  • 14. Strings  A string is an array of characters char data[10] = “Hello”; char data2[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘0’}  Use printf to print strings printf(“%s”,data);  Can be accessed char by char data[0] is first character 14 H e l l o 0 0 1 2 3 4 5 6 7 8 9 data End of String Symbol
  • 15. Strings  Each character has an integer representation 15 a b c d e z………… 97 98 99 100 101 ………………………112 A B C D E Z………… 65 66 67 68 69 ……………………… 90 0 1 2 3 4 98765 48 49 50 51 52 53 54 55 56 57 0 0 n 10
  • 16. Strings  Characters can be interpreted as integers char c = ‘A’; printf(“%c n”,c); prints A printf(“%d n”,c); prints 65 Printf(“%c n”,65); prints A 16
  • 17. 17