SlideShare uma empresa Scribd logo
1 de 39
Saranya K AP/CSE
SRIT
 An Array is a collection of similar data items,
that are stored under a common name.
 Array might belonging to any of the data types
 Array size must be a constant value.
 Always, Contiguous(adjacent) memory
locations are used to store array elements in
memory.
 Stores the elements of same data type.
 Used for maintaining multiple variable names
using a single name.
 Used for sorting elements.
 Matrix operations can be performed using
arrays.
 Arrays are also used in CPU scheduling.
 Types
› One-Dimensional array
› Two-Dimensional array
› Multi-Dimensional array
 Array declaration:
› Syntax:
data_type array_name[size];
 Example: int x[3];
X[0]
X[1]
X[2]
x
 The initializer for an array is a comma-separated list of
constant expressions enclosed in braces ({ }).
 The initializer is preceded by an equal sign (=).
 It is not necessary to initialize all elements in an array. If an
array is partially initialized, elements that are not initialized
receive the value of the appropriate type.
 Array initialization can be made either :
› At compile time or
› At run time
 Syntax:
data_type array_name[size]={variables};
Example: int x[3]={5,3,7};
5
3
7
X[0]
X[1]
X[2]
x
 Array can also be initialized at the run time.
Example:
while(i<10)
{
if(i<5)
sum[i]=0;
else
sum[i]=sum[i]+i;
}
Example:
scanf(“%d%d”,&a[0],&a[1]);
#include<stdio.h>
#include<conio.h>
void main()
{
int x[2],i;
printf("nEnter the inputs:");
for(i=0;i<2;i++)
scanf("%d",&x[i]);
for(i=0;i<2;i++)
printf("nThe value in x[%d] is %d",i,x[i]);
getch();
}
Enter the inputs:3
6
The value in x[0] is 3
The value in x[1] is 6
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char x[5]={'a','b','c','d','e'};
clrscr();
for(i=0;i<5;i++)
printf("nThe value in x[%d] is %c",i,x[i]);
getch();
}
The value in x[0] is a
The value in x[1] is b
The value in x[2] is c
The value in x[3] is d
The value in x[4] is e
 Array declaration
› Syntax:
data_type array_name[row_size]
[col_size];
 Example: int x[3][2];
X[0][0]
X[1][0]
X[2][0]
Col 0 Col 1
row 0
row 1
row 2
X[0][1]
X[1][1]
X[2][1]
 Syntax:
data_type array_name[row_size]
[col_size]={variables};
 Example: int x[2][2]={1,50,2,75};
 int x[2][2]={ {1,50},
{2,75}
};
(or)
 int x[ ][2]={ {1,50},
{2,75}
};
 The array elements will be stored in
contiguous memory locations, and it is
illustrated below:
1 50
2 75
row 0
row 1
Col 0 Col 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int x[2][2]={ {1,50},
{2,75}
};
clrscr();
for(i=0;i<2;i++)
for(j=0;j<2;j++)
printf("nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
}
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int x[][2]={ {1,50},{2,75},{3,65}};
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<2;j++)
printf("nThe value in x[%d][%d] is %d",i,j,x[i][j]);
getch();
}
The value in x[0][0] is 1
The value in x[0][1] is 50
The value in x[1][0] is 2
The value in x[1][1] is 75
The value in x[2][0] is 3
The value in x[2][1] is 65
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2;
int a[5][5],b[5][5],c[5][5];
clrscr();
step1:
printf("n Enter the size of matrix A:");
scanf("%d%d",&r1,&c1);
printf("n Enter the size of matrix B: ");
scanf("%d%d",&r2,&c2);
if((c1==c2)&&(r1==r2))
goto step2;
else
goto step1;
step2:
printf("n Enter the elements of matrix A n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n Enter the elements of matrix B n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("t%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+a[i][j]+b[i][j];
}
}
printf("n The resultant matrix after addition of A & B isn");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%dt",c[i][j]);
printf("n");
}
getch();
}
Enter the size of matrix A: 2
2
Enter the size of matrix B: 2
2
Enter the elements of matrix A
2
2
2
2
Enter the elements of matrix B
3
3
3
3
The resultant matrix after addition of A&B is
5 5
5 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r1,r2,c1,c2;
int a[5][5],b[5][5],c[5][5];
clrscr();
step1:
printf("n Enter the size of matrix A n");
scanf("%d%d",&r1,&c1);
printf("n Enter the size of matrix B n");
scanf("%d%d",&r2,&c2);
if(c1==r2)
goto step2;
else
goto step1;
step2:
printf("n Enter the elements of matrix A n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n Enter the elements of matrix B n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("t%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%dt",c[i][j]);
printf("n");
}
getch();
}
Enter the size of matrix A:2
2
Enter the size of matrix B:2
2
Enter the elements of matrix A
4
4
4
4
Enter the elements of matrix B
4
4
4
4
The resultant matrix is
32 32
32 32
Enter the size of matrix A:2
3
Enter the size of matrix B:3
2
Enter the elements of matrix A
1
2
3
4
5
6
Enter the elements of matrix B
2
4
6
8
2
4
The resultant matrix is
20 32
50 80
 An array can be passed as a parameter to a function by
specifying the array's name without an index.
 Here an array is transferred as parameter to a function.
void main() void fun(n,b[])
{ {
void fun(int,int); int x,b[5];
int a[5],n; …………..
…………… …………..
fun(n,a);
…………… }
}
#include<stdio.h>
#include<conio.h>
void add(int,int b[]);
void main()
{
int a[5],i,n;
clrscr();
printf("n Enter the Number: ");
scanf("%d",&n);
printf("n Enter the Values: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
add(n,a);
}
void add(int x,int b[])
{
int sum=0,i;
for(i=0;i<x;i++)
sum=sum+b[i];
printf("nThe sum is: %d",sum);
}
Enter the Number: 5
Enter the Values: 1
2
3
4
5
The sum is: 15
 In an array the characters are terminated by
the null (‘0’) character.
 Example: char a[]={a,b,c};
a b c 0
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
char a[]="abcd";
clrscr();
while(a[i]!='0')
{
printf("t%c",a[i]);
i++;
}
}
a b c d
 Arrays can have more than one dimension, these arrays-of-
arrays are called multidimensional arrays.
 Here is the general form of a multidimensional array
declaration:
datatype array_name [size1][size2]….[size n]
datatype - type of the data.
array_name -name of the array.
size -size of the array.
 They are very similar to standard arrays with
the exception that they have multiple sets of
square brackets after the array identifier.
 A two dimensional array can be thought of as a
grid of rows and columns.
 A two-dimensional array is an example in this
section, although the techniques can be
extended to three or more dimensions.
 The simplest form of the multidimensional array
is the two-dimensional array.
Example:
int a[3][3][3];
Col 0 Col 1 Col 2
row 0
row 1
row 2
X[0][0]
X[1][0]
X[2][0]
X[0][1]
X[1][1]
X[2][1]
X[0][2]
X[1][2]
X[2][2]

Mais conteúdo relacionado

Mais procurados

C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 

Mais procurados (20)

Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Advanced C - Part 3
Advanced C - Part 3Advanced C - Part 3
Advanced C - Part 3
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Functions in c
Functions in cFunctions in c
Functions in c
 

Semelhante a Arrays

2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 

Semelhante a Arrays (20)

Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
About Array
About ArrayAbout Array
About Array
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
02 arrays
02 arrays02 arrays
02 arrays
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Session 4
Session 4Session 4
Session 4
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 

Último

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

Arrays