Arrays in c unit iii chapter 1 mrs.sowmya jyothi

sowmyajyothi16 592 views 18 slides Jan 21, 2022
Slide 1
Slide 1 of 18
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

About This Presentation

ARRAYS IN C
C PROGRAMMING
MRS.SOWMYA JYOTHI


Slide Content

UNIT III
CHAPTER 1-ARRAYS
REFERENCE: PROGRAMMING IN C BY BALAGURUSWAMY
MRS. SOWMYA JYOTHI, SDMCBM, MANGALORE

Array:Arrayisaderiveddatatype.
•Whenitisnecessarytostoremorethanonevalue
underavariable,usercanmakeuseofarray.
•Anarrayisafixed-sizesequencecollectionof
elementsofthesamedatatype.
•Itissimplyagroupingoflike-datatype.

Different types of arrays :
There are three types of arrays. They are,
1.One dimensional array.
2.Two dimensional array.
3.Multidimensional array.

One dimensional array :
•Alistofitemscanbegivenonevariablenameusing
onlyonesubscriptandsuchavariableiscalled
aonedimensionalarray.
Example:intnumber[5];
• Hereintheexample,5valueofthevariable
numbercanbekeptunderthesinglevariable
number.

Declarationofonedimensionalarray:Likeanyother
variable,arraysmustbedeclaredbeforetheyareused.
•Thegeneralformofarraydeclarationis,
• typevariable_name[size];
Thetypespecifiesthetypeofelementsthatwillbecontained
inthearray,suchasint,floatetc.Thesizeindicatesthe
maximumnumberofelementthatcanbestoredinsidethe
array.
Forexample,
floatheight[50];
Thisdeclarestheheighttobeanarraycontaining50real
numbers.

Twodimensionalarray:
•Whenbyusinganarray,usercanstoretwovalue,
eachforarowandacolumnunderavariable,the
arrayisthencalledatwodimensionalarray.
Here,usercanuseinfinitenumberofrowsand
columns.
Twodimensionalarraysaredeclaredasfollows,
• typearray_name[rowsize][columnsize];
•Eg:inta[3][4];

Multidimensionalarray:
•Callowsarraysofthreeormoredimensions.Theexactlimit
isdeterminedbythecompiler.
•Thegeneralformofamultidimensionalarrayis,
• typearrayname[s1][s2]......[sn];
Where snis the size of the dimension.
For example,
• intsurvey[3][5][12]
surveyis a three dimensional array declared to contain 180
integer type elements.

Initialization of one dimensional array :
Afteranarrayisdeclared,itselementsmustbe
initialized.Anarraycanbeinitializedeitherofthe
followingstages,
1.At compile time.
2.At run time.

Compiletimeinitialization:
Usercaninitializetheelementsofanarrayinthesame
wayastheordinaryvariableswhentheyaredeclared.This
iscompiletimeinitialization.
Thegeneralformisasfollows,
• typearrayname[size]={listofvalues};
Thevaluesinthelistareseparatedbycommas.
Forexample,
• intnumber[3]={0,0,0};
Thiswilldeclarethevariablenumberasanarrayofsize3
andwillassign0toeachelement.

Run time initialization :
•An array can be explicitly initialized at run time. This approach is
usually applied for initializing large arrays. For example,
for(i=0;i<100;i=i+1)
{
if (i<50)
sum[i]=0.0;
else
sum[i]=1.0;
}
The first 50 elements of the array sum are initialized to zero while the
remaining 50 elements are initialized to 1.0 at run time.

Initialization of two dimensional array :
Liketheonedimensionalarrays,twodimensionalarraysmaybe
initializedbyfollowingtheirdeclarationwithalistofinitial
valuesenclosedinbraces.
Forexample,
inttable[2][3]={0,0,0,1,1,1};
Thisinitializestheelementsofthefirstrowto0andthesecond
rowto1.Thisstatementcanalsobewrittenas,
• inttable[2][3]={{0,0,0},{1,1,1}};
•This can also be written as,
inttable[2][3]={
{0,0,0},
{1,1,1}
};

marks [0][0]
35.5
Marks [0][1]
40.5
Marks [0][2]
45.5
marks [1][0]
50.5
Marks [1][1]
55.5
Marks [1][2]
60.5
marks [2][0] Marks [2][1] Marks [2][2]
marks [3][0] Marks [3][1] Marks [3][2]
Elements of multi dimension arrays:
A 2 dimensional array marks [4][3] is shown below figure.
The first element is given by marks [0][0] contains 35.5 &
second element is marks [0][1] and contains 40.5 and so on.

min=a[1] i.emin=36
i=2
a[2]< min 55<36 NO
i=3
A[3]< min 23<36 YES
min=23 SWAP
i=4
A[4]<min 12<23 YES
min=12 SWAP
i=5
A[5]<min 95<12 NO
12345

max=a[1] i.emax=36
i=2
a[2]> max 55>36 YES
max=a[2] max=55 SWAP
i=3
A[3]>max 23>55 NO
max=55 No change in max
i=4
A[4]>max 12>55 NO
i=5
A[5]>max 95>55 YES
Max=a[5] max=95 SWAP
i=6
A[6]>max 44>55 NO

•The Fibonacci Sequence is the series of numbers:
•0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
...
•Fibonacci Series is apattern of numbers where each number is
the result of addition of the previous two consecutive numbers.
•First 2 numbers start with 0 and 1.
•The third numbers in the sequence is 0+1=1. The 4th number is
the addition of 2nd and 3rd number i.e. 1+1=2 and so on.
The next number is found by adding up the two numbers before
it:
•the 2 is found by adding the two numbers before it (1+1),
•the 3 is found by adding the two numbers before it (1+2),
•the 5 is (2+3),
•and so on!

f1 f2 f3
f1 f2
f3
{
f3 = f1 + f2;
printf(" %d", f3);
f1 = f2;
f2 = f3;
}

How to reverse a number mathematically.
•Step 1 —Isolate the last digit(rem) in number.
rem= number % 10
The modulo operator (%) returns the remainder of a divison.
•Step 2 —Append lastDigit(rem) to reverse.
reverse= (reverse * 10) + rem
•Step 3-Remove last digit from number.
number = number / 10.
Tags