Upload
sahilyadav
View
216
Download
0
Embed Size (px)
Citation preview
7/25/2019 1D_arrays
1/32
ARRAYS
7/25/2019 1D_arrays
2/32
Case
You are required to store the following data for theHealth Ministry Department of India:
Name of states and union territories along withcapitals
Population of eachiteracy rate of each
Compute and display the state with highest populationand literacy rate!
7/25/2019 1D_arrays
3/32
Notion of an array
"rray# Homogeneous collection of $aria%les of same
type!
# &roup of consecuti$e memory locations!# inear and inde'ed data structure!
(o refer to an element) specify
#"rray name# Position num%er * Inde' +
7/25/2019 1D_arrays
4/32
,ingle Dimension "rrays,ingle Dimension "rrays
7/25/2019 1D_arrays
5/32
"rray -ormat
.lements of an array can %e integers) floats) charactersetc!
"ll the elements share a common name with an inde'called su%script!
In an array of n elements:
[0] [1] [2] [3] [n-1]
7/25/2019 1D_arrays
6/32
Memory ayout
7/25/2019 1D_arrays
7/32
Declaration
/hen declaring arrays) specify
# Data typeof array * integers) floats ) characters0!!+
# Nameof the array!
# ,i1e: num%er of elements
array2type array2name3 si1e 4 5
# .'ample:
int student3 67 4 5
float my2array 3 877 4 5
7/25/2019 1D_arrays
8/32
.'ample
1004
1006
1008
1010
1012
1014
1016
1018
(x[0])
(x[1])
(x[2])
(x[3])
memory addresses
-or e'ample)
int ' 3 9 4 5
"n array of integers of 9elements!
Note that the starting memory
address is determined %y the
operating system*ust li;ethat of simple $aria%le+!
Contiguous memory locations
are allocated!
Square bracket
7/25/2019 1D_arrays
9/32
.'amples
Integer array of
7/25/2019 1D_arrays
10/32
si1eof*+
(he amount of storage required to hold an array is directlyrelated to its data type and the si1e!
.'ample
(otal si1e in %ytes for a 6 D array:
total %ytes > si1eof* data type+ ? si1e of array
int a 3 @ 4 5total2%ytes > < ? @
> 69 %ytes in memory
7/25/2019 1D_arrays
11/32
.'ercise
int main* $oid+ A
float f63674 5
char c63674 5
printf*Bd) si1eof*f6++5
printf*Bd) si1eof*c6++5
return 75
E
Futput:
97
67
7/25/2019 1D_arrays
12/32
.'ample 6
/rite a program to read 67 integers from the user
and display them!
int main* $oid+ A
int digit3 67 4 ) t 5
printf*B Gn .nter the $alue of 67 integers B + 5
? for reading 67 integers from the user using scanf ?
for* t > 7) t 67 5 t J J +
scanf* B d) Kdigit 3 t 4 +5? for displaying the integers using printf ?
for* t > 7) t 67 5 t J J +
printf* B d) digit 3 t 4 +5
return 75E
7/25/2019 1D_arrays
13/32
.'ample 8 digits 3 6 4 > 87 digits 3 < 4> 877
digits 3 8 4 > 7 digits 3 9 4> 7 digits3 = 4> 7
digits 3 @ 4 > 7 digits 3 R 4 >7 digits 3 S 4 >7
7/25/2019 1D_arrays
21/32
(he second way is toInitiali1e each array element separately
i+[0]13-!
i+[]3!
i+[3]3!
i+[-]!
i+[/]3!
Initiali1ing "rrays
7/25/2019 1D_arrays
22/32
Tasics of character array
If you are required to store a group of character li;eyour name) city ) or your college name) or any wordor te'tyou need to define a array of characters!
" char $aria%le can hold a ,IN&. character onlyli;e
char c > U" 5
char c6>UT5
/hat if you need to store B,achin (endul;ar orBMVMT"I a string!
7/25/2019 1D_arrays
23/32
Character "rrays
(o hold a single string you need to declare a single
dimension character array char str 3 66 4 5
/hen declaring a character array to hold astring* group of characters+) one need to declarethe array to %e one character longer than thelargest string that it will hold
.'ample a%o$e array str3 66 4 will hold 67characters and a NV character * UG7+ at the end
7/25/2019 1D_arrays
24/32
How (o .nter $alue O
? program to read users name and display it ?
int main* +
Achar str 367 4 5
printf* B .nter your name B+5
scanf*B s ) str+5
printf* B Your name is : s) str+5
return 75
E
o nee+ to p2t
7/25/2019 1D_arrays
25/32
In case of character arrays
Name of the array is the starting address of the
array
char str36745
Kstr 3 7 4 >Wstr
7/25/2019 1D_arrays
26/32
Disad$antage of s
/hile reading a string using sformat specifier) it does notscan the string after the space %ar!
-or e'ample
,chin (endul;ar
It will scan only B ,achin !
It will I&NFL. any space or ta% space
How to scan a complete te't of wordsO
7/25/2019 1D_arrays
27/32
gets* +
gets* argument string+ : Collects a string ofcharacters terminated %y new line character UGnfromthe standard input stream * stdin +!
It allows to input spaces) ta%s and copies all thecharacter till new line into the argument string andappend a NV character UG7at the end of it!
-or e'ample
char str 3
7/25/2019 1D_arrays
28/32
puts* +
puts* argument string +:
It displays the string of characters from argument
string to standard output till NV characterand
appends a new line character at the end!
-or e'ample
puts * str + 5
7/25/2019 1D_arrays
29/32
45678: Inp2t ten n2mbers into an array,
2sing val2es o% 0 to 99, an+ print o2t all n2mbers
ex$ept %or the largest n2mber..? to accept 67 $alues in the range 7 to SS ?
int si1e>675
int $alue3si1e4) i5for *i>75 i si1e5 JJi+
A scanf *Bd)K$alue3i4+5
if *$alue3i4 W SS XX $alue3i4 7+ A printf *B enter only $alues within 7 SS +5
i5 E
E
7/25/2019 1D_arrays
30/32
? to find the greatest among the list ?int ma'imum> 75
for *i>75 i si1e5 iJJ+
Aif *ma'imum $alue3i4 +
ma'imum > $alue3i45
E
7/25/2019 1D_arrays
31/32
? to print all the $alues other than the greatest ?
for *i>75 i si1e5 iJJ+
A
if *$alue3i4 > ma'imum +
printf *Bd) $alue3i45
E
7/25/2019 1D_arrays
32/32
Home "ssignment
/rite a program to read a te't from console and
display the num%er of words and lines in the te't!