Array and its types and it's implemented programming Final.pdf

ajajkhan16 781 views 30 slides Jan 25, 2024
Slide 1
Slide 1 of 30
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30

About This Presentation

What is Array and it's types


Slide Content

Arrays:
Anarrayisacollectionofdatathatholds
fixednumberofvaluesofsametype.
Thesizeandtypeofarrayscannotbe
changedafteritsdeclaration.
ForExample:Ifyouwanttostoremarks
of100studentsyoucancreateanarrayforit.
floatmarks[100];

Someexampleswheretheconceptofan
arraycanbeused:
•Listoftemperaturesrecordedeveryhourinaday,ora
month
orayear.
•Listofemployeesinanorganization.
•Listofproductsandtheircostsoldbyastore.
•Testscoresofaclassofstudents.
•Listofcustomersandtheirtelephonenumbers.
•Tableofdailyrainfalldata.

HowtodeclareanarrayinC?
Syntax:
data_typearray_name[array_size];
Forexample:
floatmark[5];
Herewedeclaredanarray,mark,of
floating-pointtypeandsize5.Thatitholds5
floating-pointvalues.

ElementsofanarrayandHowtoaccessthem?
Youcanaccesselementsofanarraybyindices.
Supposeyoudeclaredanarraymarkasabove.Thefirst
elementismark[0],secondelementismark[1]andsoon.
FewKeypoints:
•Arrayshave0asthefirstindexnot1.Inthisexample,
mark[0].
•Ifthesizeofanarrayisn,toaccessthelastelement,(n-1)
indexisused.Inthisexample,mark[4].
•Supposethestartingaddressofmark[0]is2120d.Then,the
nextaddress,mark[1],willbe2124d,addressofmark[2]will
be2128dandsoon.Itsbecausethesizeofafloatis4bytes.

HowtoInitializeanarray?
Itspossibletoinitializeanarrayduringdeclaration.
Forexample:
intmark[5]={9,4,6,3,5};
Anothermethodofinitializearrayduringdeclaration
intmark[]={9,4,6,3,5};
Here,
mark[0]isequalto9
mark[1]isequalto4
mark[2]isequalto6
mark[3]isequalto3
mark[4]isequalto5

Importantthingtorememberwhenworkingwith
Carrays:
Supposeyoudeclaredanarrayof10elements.Letssay,
inttestArray[10];
YoucanusethearraymembersfromtestArray[0]to
testArray[9].
Ifyoutrytoaccessarrayelementsoutsideofitsbound,lets
saytestArray[12],thecompilermaynotshowanyerror.
However,thismaycauseunexpectedoutput(undefined
behavior).

Arrays are of three types:
1.One-dimensional arrays.
2.Two-dimensional arrays.
3.Multidimensional arrays.

One-dimensionalArray:
Alistofitemcanbegivenonevariablenameusing
onlyonesubscriptandsuchavariableiscalledasinglesubscripted
variableoraonedimensionalarray.
TheSyntaxforanarraydeclarationis:
typevariable-name[size];
Example:
floatheight[50];
intgroupt[10];
charname[10];

Thetypespecifiesthetypeoftheelementthatwillbe
containedinthearray,suchasint,float,orcharandthesize
indicatesthemaximumnumberofelementsthatcanbestored
insidethearray.
Nowaswedeclareaarray
intnumber[5];
Thenthecomputerreservesfivestoragelocationsas
thesizeothearrayis5asshowbelow.
ReservedSpace StoringvaluesafterInitialization
number[0] number[0]
number[1] number[1]
number[2] number[2]
number[3] number[3]
number[4] number[4]
35
20
40
57
19

Initializationofonedimensionalarray:
Afteranarrayisdeclared,itselementsmustbe
initialized.InCprogramminganarraycanbeinitializedat
eitherofthefollowingstages:
Atcompiletime
Atruntime

CompileTimeInitialization:
Thegeneralformofinitializationofarrayis:
typearray-name[size]={listofvalues};
Thevaluesinthelistareseparatedbycommas.
Forexample:
intnumber[3]={0,5,4};
willdeclarethevariable‘number’asanarrayof
size3andwillassignthevaluestoeachelements.
Ifthenumberofvaluesinthelistislessthanthe
numberofelements,thenonlythatmanyelementswillbe
initialized.
Theremainingelementswillbesettozero
automatically.
Remember,ifwehavemoreinitializersthanthe
declaredsize,thecompilerwillproduceanerror.

Runtimeinitialization:
Anarraycanalsobeexplicitlyinitializedatruntime.
Forexampleconsiderthefollowingsegmentofacprogram.
for(i=0;i<10;i++)
{
scanf(“%d”,&x[i]);
}
Intheruntimeinitializationofthearrayslooping
statementsarealmostcompulsory.
Loopingstatementsareusedtoinitializethevaluesofthe
arraysonebyoneusingassignmentoperatororthroughthe
keyboardbytheuser.

OnedimensionalArrayProgram:
#include<stdio.h>
voidmain()
{
intarray[5];
printf(“Enter5numberstostoretheminarray\n”);
for(i=0;i<5;i++)
{
Scanf(“%d”,&array[i]);
}
printf(“Elementinthearrayare:\n”);
For(i=0;i<5;i++)
{
printf(“Elementstoredata[%d]=%d\n”,i,array[i]);
}
getch();
}

Input:
Enter 5 elements in the array: 23 45 32 25 45
Output:
Element in the array are:
Element stored at a[0]:23
Element stored at a[0]:45
Element stored at a[0]:32
Element stored at a[0]:25
Element stored at a[0]:45

Two-dimensionalArrays:
Thesimplestformofmultidimensionalarrayis
thetwo-dimensionalarray.Atwo-dimensionalarrayis,in
essence,alistofone-dimensionalarrays.
Todeclareatwo-dimensionalintegerarrayof
size[x][y],youwouldwritesomethingasfollows:
typearrayName[x][y];
WheretypecanbeanyvalidCdatatypeand
arrayNamewillbeavalidCidentifier.

Atwo-dimensionalarraycanbeconsideredas
atablewhichwillhavexnumberorowsandynumbero
columns.
Atwo-dimensionalarraya,whichcontains
threerowsandfourcolumnscanbeshownasfollows.
Column0Column1Column2Column3
Row0
Row1
Row2
a[0][0] a[0][1]
a[0][2] a[0][3]
a[0][2] a[0][2] a[0][2] a[0][2]
a[0][2] a[0][2] a[0][2] a[0][2]

Thus,everyelementinthearrayaisidentified
byanelementnameoftheforma[i][j].
where‘a’isthenameofthearray,and‘i’and‘j’
arethesubscriptsthatuniquelyidentifyeachelementin‘a’.
InitializingTwo-DimensionalArrays:
Multidimensionalarraysmaybeinitializedby
specifyingbracketedvaluesforeachrow.

Followingisanarraywith3rowsandeachrow
has4columns.
inta[3][4]={
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
Thenestedbraces,whichindicatetheintended
row,areoptional.Thefollowinginitializationisequivalent
tothepreviousexample
inta[3][4]={0,1,2,3,4,5,6,7,8,9,10,11};

AccessingTwo-DimensionalArrayElements:
Anelementinatwo-dimensionalarrayis
accessedbyusingthesubscripts.i.e.,rowindexand
columnindexofthearray.
ForExample:
intval=a[2][3];
Theabovestatementwilltakethe4
th
element
fromthe3
rd
rowofthearray.

Two-Dimensional Arrays program:
#include<stdio.h>
int main()
{
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] = %d\n”,i,j,a[i][j]);
}
}
return 0;
}

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

Multi-DimensionalArrays:
C programming languagesupports
multidimensionalArrays.
•Multidimensionalarrayshavemorethanone
subscriptvariables.
•Multidimensionalarrayisalsocalledasmatrix.
•Multidimensionalarraysarearrayoarrays.

DeclarationofMultidimensionalArray
Amultidimensionalarrayisdeclaredusingthe
followingsyntax
Syntax:
data_typearray_name[d1][d2][d3][d4]....[dn];
AbovestatementwilldeclareanarrayonN
dimensionsofnamearray_name,whereeachelementof
arrayisoftypedata_type.
Themaximumnumberofelementsthatcanbe
storedinamultidimensionalarrayarray_nameissize1X
size2Xsize3....sizeN.
Forexample:
charcube[50][60][30];

Multidimensional Array program:
#include<stdio.h>
int main()
{
int r,c,a[50][50],b[50][50],sum[50][50],i,j;
printf("\t\n Multi-dimensional array");
printf("\n Enter the row matrix:");
scanf("%d",&r);
printf("\n Enter the col matrix:");
scanf("%d",&c);
printf("\n Element of A matrix");
for(i=0;i<r;++i)
for(j=0;j<c;++j)

{
printf("\n a[%d][%d]:",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\n Element of B matrix");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("\n b[%d][%d]:",i+1,j+1);
scanf("%d",&b[i][j]);
}
printf("\n Addition of matrix");
for(i=0;i<r; i++)

{
for(j=0;j<c;j++)
sum[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("\t%d",sum[i][j]);
}
printf("\n\t");
}

if(j==c-1)
{
printf("\n");
}
return 0;
}

Output
Multidimensional array
Enter the row matrix:2
Enter the col matrix:2
Element of A matrix
a[1][1]:1
a[1][2]:4
a[2][1]:6
a[2][2]:3

Element of B matrix
b[1][1]:1
b[1][2]:7
b[2][1]:3
b[2][2]:9
Addition of a matrix is
2 11
9 12
Tags