SlideShare a Scribd company logo
Arrays
Programming
1
Array
An array is a collection of data that holds
fixed number of values of same type.
Arrays a kind of data structure that can
store a fixed-size sequential collection of
elements of the same type.
2
Array
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
For example: if you want to store marks of
100 students, you can create an array for
it.
float marks[100];
3
Array
All arrays consist of contiguous memory
locations. The lowest address corresponds
to the first element and the highest address
to the last element.
4
Array Applications
 Given a list of test scores, determine the
maximum and minimum scores.
 Read in a list of student names and rearrange
them in alphabetical order (sorting).
 Given the height measurements of students in
a class, output the names of those students
who are taller than average.
5
Array Declaration
 Syntax:
<type> <arrayName>[<array_size>]
Ex. int Ar[10];
 The array elements are all values of the type <type>.
 The size of the array is indicated by <array_size>, the
number of elements in the array.
 <array_size> must be an int constant or a constant
expression. Note that an array can have multiple
dimensions.
6
Array Declaration
// array of 10 uninitialized ints
int Ar[10];
-- -- ----Ar -- -- ---- -- --
4 5 630 2 8 971
0 1 2 3 4 5
7
Initializing Array
You can initialize an array in C either one
by one or using a single statement as
follows −
double balance[5] = {1000.0, 2.0, 3.4, 7.0,
50.0};
The number of values between braces { }
cannot be larger than the number of
elements that we declare for the array
between square brackets [ ].
8
Initializing Array
If you omit the size of the array, an array
just big enough to hold the initialization is
created. Therefore, if you write −
double balance[] = {1000.0, 2.0, 3.4, 7.0,
50.0};
9
Examples of array accessing
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %dn", j, n[j] );
}
return 0;
}
10
Multi-dimensional Arrays in C
11
C programming language allows multidimensional
arrays.
Here is the general form of a multidimensional
array declaration −
type name[size1][size2]...[sizeN];
For example, the following declaration creates a
three dimensional integer array −
int threedim[5][10][4];
Two-dimensional Arrays
12
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 ];
Two-dimensional Arrays
13
Where type can be any valid C data type
and arrayName will be a valid C identifier. 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
14
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
};
Accessing Two-dimensional Arrays
15
The above statement will take the 4th
element from the 3rd row of the array.
You can verify it in the above figure.
Let us check the following program where
we have used a nested loop to handle a
two-dimensional array −
Accessing Two-dimensional
Arrays(cont.)
16
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 val = a[2][3];
Accessing Two-dimensional
Arrays(cont.)
17
#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;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{ for ( j = 0; j < 2; j++ )
{ printf("a[%d][%d] = %dn", i,j, a[i][j] );
} }
return 0;
}

More Related Content

PPTX
Array in c programming
PPTX
Array in c programming
PPTX
Programming in c arrays
PPTX
arrays in c
PPTX
C++ programming (Array)
PPTX
Presentation on array
PPT
Array in c
PPTX
Array in c programming
Array in c programming
Array in c programming
Programming in c arrays
arrays in c
C++ programming (Array)
Presentation on array
Array in c
Array in c programming

What's hot (20)

PPT
Arrays in c
PPTX
PDF
Array
PPTX
PPTX
Array in (C) programing
PPTX
concept of Array, 1D & 2D array
PPT
Array
PPTX
Arrays in c language
PPTX
Basic array in c programming
PPTX
Arrays In C Language
PDF
ARRAYS
PPTX
Arrays in c
PPTX
Arrays in c
PPT
C programming , array 2020
PPTX
Programming in c Arrays
PPT
Numeric Arrays [6]
PPT
Arrays Basics
PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
Introduction to Array ppt
Arrays in c
Array
Array in (C) programing
concept of Array, 1D & 2D array
Array
Arrays in c language
Basic array in c programming
Arrays In C Language
ARRAYS
Arrays in c
Arrays in c
C programming , array 2020
Programming in c Arrays
Numeric Arrays [6]
Arrays Basics
Arrays 1D and 2D , and multi dimensional
Introduction to Array ppt
Ad

Similar to Array (20)

PDF
PDF
Array and its types and it's implemented programming Final.pdf
PDF
Arrays-Computer programming
PPTX
Chapter 13.pptx
PPTX
PDF
Programming Fundamentals Arrays and Strings
PPT
Lecture 15 - Array
PPTX
Array
PPTX
Arrays & Strings
PPTX
COM1407: Arrays
PPTX
Arrays basics
PPTX
Array,MULTI ARRAY, IN C
PPT
strings.ppt
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
PPT
Multidimensional array in C
PPTX
BHARGAVIARRAY.PPT.pptx
PPT
array2d.ppt
PDF
11 1. multi-dimensional array eng
Array and its types and it's implemented programming Final.pdf
Arrays-Computer programming
Chapter 13.pptx
Programming Fundamentals Arrays and Strings
Lecture 15 - Array
Array
Arrays & Strings
COM1407: Arrays
Arrays basics
Array,MULTI ARRAY, IN C
strings.ppt
C (PPS)Programming for problem solving.pptx
Unit4pptx__2024_11_ 11_10_16_09.pptx
Multidimensional array in C
BHARGAVIARRAY.PPT.pptx
array2d.ppt
11 1. multi-dimensional array eng
Ad

More from Rokonuzzaman Rony (20)

PPTX
Course outline for c programming
PPTX
PPTX
Operator Overloading & Type Conversions
PPTX
Constructors & Destructors
PPTX
Classes and objects in c++
PPTX
Functions in c++
PPTX
Object Oriented Programming with C++
PPTX
Humanitarian task and its importance
PPTX
PPTX
PPTX
Introduction to C programming
PPTX
Constants, Variables, and Data Types
PPTX
C Programming language
PPTX
User defined functions
PPTX
Numerical Method 2
PPT
Numerical Method
PPTX
Data structures
PPT
Data structures
PPTX
Data structures
Course outline for c programming
Operator Overloading & Type Conversions
Constructors & Destructors
Classes and objects in c++
Functions in c++
Object Oriented Programming with C++
Humanitarian task and its importance
Introduction to C programming
Constants, Variables, and Data Types
C Programming language
User defined functions
Numerical Method 2
Numerical Method
Data structures
Data structures
Data structures

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Basic Mud Logging Guide for educational purpose
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
GDM (1) (1).pptx small presentation for students
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Basic Mud Logging Guide for educational purpose
Supply Chain Operations Speaking Notes -ICLT Program
Sports Quiz easy sports quiz sports quiz
PPH.pptx obstetrics and gynecology in nursing
102 student loan defaulters named and shamed – Is someone you know on the list?
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Cell Types and Its function , kingdom of life
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
GDM (1) (1).pptx small presentation for students
VCE English Exam - Section C Student Revision Booklet
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf

Array

  • 2. Array An array is a collection of data that holds fixed number of values of same type. Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. 2
  • 3. Array 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 For example: if you want to store marks of 100 students, you can create an array for it. float marks[100]; 3
  • 4. Array All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. 4
  • 5. Array Applications  Given a list of test scores, determine the maximum and minimum scores.  Read in a list of student names and rearrange them in alphabetical order (sorting).  Given the height measurements of students in a class, output the names of those students who are taller than average. 5
  • 6. Array Declaration  Syntax: <type> <arrayName>[<array_size>] Ex. int Ar[10];  The array elements are all values of the type <type>.  The size of the array is indicated by <array_size>, the number of elements in the array.  <array_size> must be an int constant or a constant expression. Note that an array can have multiple dimensions. 6
  • 7. Array Declaration // array of 10 uninitialized ints int Ar[10]; -- -- ----Ar -- -- ---- -- -- 4 5 630 2 8 971 0 1 2 3 4 5 7
  • 8. Initializing Array You can initialize an array in C either one by one or using a single statement as follows − double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ]. 8
  • 9. Initializing Array If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write − double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; 9
  • 10. Examples of array accessing #include <stdio.h> int main () { int n[ 10 ]; /* n is an array of 10 integers */ int i,j; /* initialize elements of array n to 0 */ for ( i = 0; i < 10; i++ ) { n[ i ] = i + 100; /* set element at location i to i + 100 */ } /* output each array element's value */ for (j = 0; j < 10; j++ ) { printf("Element[%d] = %dn", j, n[j] ); } return 0; } 10
  • 11. Multi-dimensional Arrays in C 11 C programming language allows multidimensional arrays. Here is the general form of a multidimensional array declaration − type name[size1][size2]...[sizeN]; For example, the following declaration creates a three dimensional integer array − int threedim[5][10][4];
  • 12. Two-dimensional Arrays 12 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 ];
  • 13. Two-dimensional Arrays 13 Where type can be any valid C data type and arrayName will be a valid C identifier. 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 −
  • 14. Initializing Two-dimensional Arrays 14 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 };
  • 15. Accessing Two-dimensional Arrays 15 The above statement will take the 4th element from the 3rd row of the array. You can verify it in the above figure. Let us check the following program where we have used a nested loop to handle a two-dimensional array −
  • 16. Accessing Two-dimensional Arrays(cont.) 16 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 val = a[2][3];
  • 17. Accessing Two-dimensional Arrays(cont.) 17 #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; /* output each array element's value */ for ( i = 0; i < 5; i++ ) { for ( j = 0; j < 2; j++ ) { printf("a[%d][%d] = %dn", i,j, a[i][j] ); } } return 0; }