SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
ARRAYS
MODULE 3
1
• An array is a collection of similar data elements.
• These data elements have the same data type.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a
numerical index, which is used to identify the element.
• The elements of the array are stored in consecutive
memory locations and are referenced by an index
(subscript).
2
DEFINITION
ARRAY REPRESENTATION
Array Name Elements
int marks[10] = { 56, 59, 69, 78, 96, 43, 90, 88, 94, 70}
Data type Array Size
Index starts with 0
Mark1 mark2 mark3 mark4 mark5 mark6 mark7 mark8 mark9 mark10
index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
3
56 59 69 78 96 43 90 88 94 70Elements
Element name
LIMITATIONS OF ARRAY
• Arrays are generally used when we want to store large
amount of similar type of data.
• But they have the following limitations:
• Arrays are of fixed size.
• Data elements are stored in contiguous memory
locations which may not be always available.
• Insertion and deletion of elements can be problematic
because of shifting of elements from their positions.
4
EXAMPLE CODE 5
With Array
#include <stdio.h>
void main ()
{
int marks[6] = {56,78,88,76,56,89);
int i,tot=0;
float avg;
for (i=0; i<6; i++ )
{
tot=tot+ marks[i];
}
avg=tot/6;
printf(avg);
}
Without Array
#include <stdio.h>
void main ()
{
int marks_1 = 56, marks_2 = 78, marks_3 = 88,
marks_4 = 76, marks_5 = 56, marks_6 = 89;
float avg = (marks_1 + marks_2 + marks_3 +
marks_4 + marks_5 +marks_6) / 6 ;
printf(avg);
}
TYPES OF ARRAYS
• One Dimensional Arrays - Linear List
• Two Dimensional Arrays – Table, Matrix
• Three Dimensional Arrays – Multi Dimensional
6
TWO DIMENSIONAL ARRAYS
7
// Different ways to initialize two-
dimensional array
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
//Declaration of 2D Array
float x[3][4];
C1 C2 C3 C4
R1 X[0][0] X[0][1] X[0][2] X[0][3]
R2 X[1][0] X[1][1] X[1][2] X[1][3]
R3 X[2][0] X[2][1] X[2][2] X[2][3]
THREE DIMENSIONAL ARRAY 8
// Initialization of 3D Array with Input Values
int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11},
{23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3,
1, 4, 9}}};
// Declaration
float y[2][4][3];
OPERATIONS
• Traversing: It prints all the array elements one after another.
• Inserting: It adds an element at given index.
• Deleting: It is used to delete an element at given index.
• Searching: It searches for an element(s) using given index or by value.
• Updating: It is used to update an element at given index.
9
TRAVERSAL 10
// Access the first element of the
array
printf("%d", mark[0]);
// Access the third element of the
array printf("%d", mark[2]);
// Access ith element of the array
printf("%d", mark[i-1]);
#include <stdio.h>
void main()
{
int array[10];
int i;
printf("enter the elements n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
printf(“Elements of the array n");
for (i = 0; i < 10; i =i++)
printf( "%dn", array[i]) ;
}
INSERTION
• First get the element to be inserted, say x
• Then get the position at which this element is to be inserted, say pos
• Then shift the array elements from this position to one position
forward, and do this for all the other elements next to pos.
• Insert the element x now at the position pos, as this is now empty.
11
12 87 14 68 73 90 12 3
12 87 14 68 73 50 90 12 3
X= 50
Pos = 5
12CODE
// element to be inserted
x = 50;
// position at which elements
to be inserted
pos = 5;
// increase the size by 1
n++;
// shift elements forward
for (i = n; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("n");
DELETION 13
Step by step descriptive logic to remove element from array.
• Move to the specified location which you want to remove in given
array.
• Copy the next element to the current element of array. Which is you
need to perform array[i] = array[i + 1].
• Repeat above steps till last element of array.
• Finally decrement the size of array by one.
14
12 87 14 68 73 50 90
12 14 68 73 50 90 90
Element to be deleted
12 14 68 73 50 90 90
15
printf("Enter the element position to delete :
");
scanf("%d", &pos);
/* Invalid delete position */
if(pos < 0 || pos > size)
{
printf("Invalid position! Please enter position
between 1 to %d", size);
}
else
{
/* Copy next element value to current
element */
for(i=pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}
/* Decrement array size by 1 */
size--;
}
/* Print array after deletion */
printf("nElements of array after delete are :
");
for(i=0; i<size; i++)
{
printf("%dt", arr[i]);
}
SEARCHING
• Input size and elements in array from user.
• Store it in some variable say size and arr.
• Input number to search from user in some variable say toSearch.
• Define a flag variable as found = 0.
• Run loop from 0 to size.
• Inside loop check if current array element is equal to searched
number or not.
• Which is if(arr[i] == toSearch) then set found = 1 flag and
terminate from loop.
• Since element is found no need to continue further.
• Outside loop if(found == 1) then element is found otherwise not.
16
UPDATION
• Consider A is a linear array with N elements
• K is a positive integer such that K<=N.
• Following is the algorithm to update an element available at the
Kth position of A.
17
printf("The original array elements are :n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d n", i, LA[i]);
}
A[k-1] = item;
printf("The array elements after updation :n");
for(i = 0; i<n; i++)
{
printf("A[%d] = %d n", i, LA[i]);
} }
18

More Related Content

PPTX
C function presentation
PPTX
Functions in C.pptx
PPTX
Control statements in c
PPTX
Variables in C and C++ Language
PPT
358 33 powerpoint-slides_6-strings_chapter-6
PPTX
Recursive Function
PPT
Two dimensional array
PPTX
Static Data Members and Member Functions
C function presentation
Functions in C.pptx
Control statements in c
Variables in C and C++ Language
358 33 powerpoint-slides_6-strings_chapter-6
Recursive Function
Two dimensional array
Static Data Members and Member Functions

What's hot (20)

PPTX
C Programming: Control Structure
PPTX
data types in C programming
PPTX
Variables, Data Types, Operator & Expression in c in detail
PPTX
Operators and expressions in c language
PPT
Multidimensional array in C
PPTX
Input output statement in C
PPTX
Arrays in c
PPSX
Break and continue
PPT
Introduction to Basic C programming 01
PPTX
Exception handling c++
PPT
Array in c
PPTX
Unit 2. Elements of C
PPTX
PPTX
Function in C program
PPTX
File handling in C
PPT
Introduction to c programming
PPTX
Scope rules : local and global variables
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
PPT
RECURSION IN C
PPTX
Programming in c Arrays
C Programming: Control Structure
data types in C programming
Variables, Data Types, Operator & Expression in c in detail
Operators and expressions in c language
Multidimensional array in C
Input output statement in C
Arrays in c
Break and continue
Introduction to Basic C programming 01
Exception handling c++
Array in c
Unit 2. Elements of C
Function in C program
File handling in C
Introduction to c programming
Scope rules : local and global variables
358 33 powerpoint-slides_5-arrays_chapter-5
RECURSION IN C
Programming in c Arrays
Ad

Similar to Arrays (20)

PDF
SlideSet_4_Arraysnew.pdf
PPTX
Module_3_Arrays - Updated.pptx............
PDF
PPTX
PPTX
DS Unit 1.pptx
PDF
SPL 10 | One Dimensional Array in C
PPSX
C Programming : Arrays
PPTX
Yash Bhargava Array In programming in C
PPTX
arrays in c programming - example programs
PDF
CP PPT_Unit IV computer programming in c.pdf
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
Basic of array and data structure, data structure basics, array, address calc...
PPT
Chapter 10.ppt
PPTX
Unit 6. Arrays
PPTX
Arrays 1D and 2D , and multi dimensional
PDF
CSEG1001Unit 3 Arrays and Strings
PPTX
The document discusses arrays in data structures using Cpp programming langua...
PPTX
Data structure and algorithm list structures
PPT
Basics of Data structure using C describing basics concepts
PDF
Array (data structure using c++).PPT presentation
SlideSet_4_Arraysnew.pdf
Module_3_Arrays - Updated.pptx............
DS Unit 1.pptx
SPL 10 | One Dimensional Array in C
C Programming : Arrays
Yash Bhargava Array In programming in C
arrays in c programming - example programs
CP PPT_Unit IV computer programming in c.pdf
C (PPS)Programming for problem solving.pptx
Basic of array and data structure, data structure basics, array, address calc...
Chapter 10.ppt
Unit 6. Arrays
Arrays 1D and 2D , and multi dimensional
CSEG1001Unit 3 Arrays and Strings
The document discusses arrays in data structures using Cpp programming langua...
Data structure and algorithm list structures
Basics of Data structure using C describing basics concepts
Array (data structure using c++).PPT presentation
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
additive manufacturing of ss316l using mig welding
DOCX
573137875-Attendance-Management-System-original
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Geodesy 1.pptx...............................................
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
composite construction of structures.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Lecture Notes Electrical Wiring System Components
Internet of Things (IOT) - A guide to understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Construction Project Organization Group 2.pptx
Digital Logic Computer Design lecture notes
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
additive manufacturing of ss316l using mig welding
573137875-Attendance-Management-System-original
ETO & MEO Certificate of Competency Questions and Answers
OOP with Java - Java Introduction (Basics)
Geodesy 1.pptx...............................................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
composite construction of structures.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Model Code of Practice - Construction Work - 21102022 .pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

Arrays

  • 2. • An array is a collection of similar data elements. • These data elements have the same data type. • Element − Each item stored in an array is called an element. • Index − Each location of an element in an array has a numerical index, which is used to identify the element. • The elements of the array are stored in consecutive memory locations and are referenced by an index (subscript). 2 DEFINITION
  • 3. ARRAY REPRESENTATION Array Name Elements int marks[10] = { 56, 59, 69, 78, 96, 43, 90, 88, 94, 70} Data type Array Size Index starts with 0 Mark1 mark2 mark3 mark4 mark5 mark6 mark7 mark8 mark9 mark10 index [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 3 56 59 69 78 96 43 90 88 94 70Elements Element name
  • 4. LIMITATIONS OF ARRAY • Arrays are generally used when we want to store large amount of similar type of data. • But they have the following limitations: • Arrays are of fixed size. • Data elements are stored in contiguous memory locations which may not be always available. • Insertion and deletion of elements can be problematic because of shifting of elements from their positions. 4
  • 5. EXAMPLE CODE 5 With Array #include <stdio.h> void main () { int marks[6] = {56,78,88,76,56,89); int i,tot=0; float avg; for (i=0; i<6; i++ ) { tot=tot+ marks[i]; } avg=tot/6; printf(avg); } Without Array #include <stdio.h> void main () { int marks_1 = 56, marks_2 = 78, marks_3 = 88, marks_4 = 76, marks_5 = 56, marks_6 = 89; float avg = (marks_1 + marks_2 + marks_3 + marks_4 + marks_5 +marks_6) / 6 ; printf(avg); }
  • 6. TYPES OF ARRAYS • One Dimensional Arrays - Linear List • Two Dimensional Arrays – Table, Matrix • Three Dimensional Arrays – Multi Dimensional 6
  • 7. TWO DIMENSIONAL ARRAYS 7 // Different ways to initialize two- dimensional array int c[2][3] = {{1, 3, 0}, {-1, 5, 9}}; int c[][3] = {{1, 3, 0}, {-1, 5, 9}}; int c[2][3] = {1, 3, 0, -1, 5, 9}; //Declaration of 2D Array float x[3][4]; C1 C2 C3 C4 R1 X[0][0] X[0][1] X[0][2] X[0][3] R2 X[1][0] X[1][1] X[1][2] X[1][3] R3 X[2][0] X[2][1] X[2][2] X[2][3]
  • 8. THREE DIMENSIONAL ARRAY 8 // Initialization of 3D Array with Input Values int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}}; // Declaration float y[2][4][3];
  • 9. OPERATIONS • Traversing: It prints all the array elements one after another. • Inserting: It adds an element at given index. • Deleting: It is used to delete an element at given index. • Searching: It searches for an element(s) using given index or by value. • Updating: It is used to update an element at given index. 9
  • 10. TRAVERSAL 10 // Access the first element of the array printf("%d", mark[0]); // Access the third element of the array printf("%d", mark[2]); // Access ith element of the array printf("%d", mark[i-1]); #include <stdio.h> void main() { int array[10]; int i; printf("enter the elements n"); for (i = 0; i < 10; i++) scanf("%d", &array[i]); printf(“Elements of the array n"); for (i = 0; i < 10; i =i++) printf( "%dn", array[i]) ; }
  • 11. INSERTION • First get the element to be inserted, say x • Then get the position at which this element is to be inserted, say pos • Then shift the array elements from this position to one position forward, and do this for all the other elements next to pos. • Insert the element x now at the position pos, as this is now empty. 11 12 87 14 68 73 90 12 3 12 87 14 68 73 50 90 12 3 X= 50 Pos = 5
  • 12. 12CODE // element to be inserted x = 50; // position at which elements to be inserted pos = 5; // increase the size by 1 n++; // shift elements forward for (i = n; i >= pos; i--) arr[i] = arr[i - 1]; // insert x at pos arr[pos - 1] = x; // print the updated array for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("n");
  • 13. DELETION 13 Step by step descriptive logic to remove element from array. • Move to the specified location which you want to remove in given array. • Copy the next element to the current element of array. Which is you need to perform array[i] = array[i + 1]. • Repeat above steps till last element of array. • Finally decrement the size of array by one.
  • 14. 14 12 87 14 68 73 50 90 12 14 68 73 50 90 90 Element to be deleted 12 14 68 73 50 90 90
  • 15. 15 printf("Enter the element position to delete : "); scanf("%d", &pos); /* Invalid delete position */ if(pos < 0 || pos > size) { printf("Invalid position! Please enter position between 1 to %d", size); } else { /* Copy next element value to current element */ for(i=pos-1; i<size-1; i++) { arr[i] = arr[i + 1]; } /* Decrement array size by 1 */ size--; } /* Print array after deletion */ printf("nElements of array after delete are : "); for(i=0; i<size; i++) { printf("%dt", arr[i]); }
  • 16. SEARCHING • Input size and elements in array from user. • Store it in some variable say size and arr. • Input number to search from user in some variable say toSearch. • Define a flag variable as found = 0. • Run loop from 0 to size. • Inside loop check if current array element is equal to searched number or not. • Which is if(arr[i] == toSearch) then set found = 1 flag and terminate from loop. • Since element is found no need to continue further. • Outside loop if(found == 1) then element is found otherwise not. 16
  • 17. UPDATION • Consider A is a linear array with N elements • K is a positive integer such that K<=N. • Following is the algorithm to update an element available at the Kth position of A. 17 printf("The original array elements are :n"); for(i = 0; i<n; i++) { printf("A[%d] = %d n", i, LA[i]); } A[k-1] = item; printf("The array elements after updation :n"); for(i = 0; i<n; i++) { printf("A[%d] = %d n", i, LA[i]); } }
  • 18. 18