l27 1d arrays
TRANSCRIPT
-
7/26/2019 l27 1D Arrays
1/37
A R R A Y S &S T R I N G S
-
7/26/2019 l27 1D Arrays
2/37
Arrays
Definition:
An array is a group of related data items that share
a common name.
The array elements are placed in a contiguous memory
locations.
A particular value in an array is indicated by writing an integer number called index number or subscript in a
square brackets
after the array name.
The least value that an index can take in array is0..
-
7/26/2019 l27 1D Arrays
3/37
Arrays
For example
We define an array named salary to represent a setof salaries of a group of employees.
Now the values or the salaries of employees
can be stored as follows.
salary[0]=10000, salary[1]=15000,
salary[2]=20000, salary[4]=10000,..etc.
wheresalary[0], salary[1], salary[2]
etc. respectively
represent the salaries of first, second, third employee.
7/13/2012 Department of CSE 3
-
7/26/2019 l27 1D Arrays
4/37
Arrays
If an array salary contains 5 integer values
(salaries) of employees then it is represented as
follows.
The elements are numbered from 0 to 4 since in
arrays the first index is always 0, independent of its
length.
7/13/2012 Department of CSE 4
-
7/26/2019 l27 1D Arrays
5/37
Arrays
ArrayDeclaration:
type name [size];
wheretypeis
avalid
data
type
(like
int,
float...)
nameisavalididentifier&
sizespecifieshowmanyelementsthearrayhastocontain.
sizefieldis
always
enclosed
in
square
brackets
[]and
takes
staticvalues.
Forexample
an
array
salarycontaining
5
elements
is
declared
as
follows
int salary [5];
7/13/2012 Department of CSE 5
-
7/26/2019 l27 1D Arrays
6/37
Arrays
One Dimensional Array
A linear list of fixed number of data items of same
type.
These items are accessed using the same name using
a single subscript. E.g. salary [1], salary [4]
A list of items can be givenone variable name
using only one subscriptand such a variable is
called a
single-subscripted variable
or a
one-
dimensional array.
7/13/2012 Department of CSE 6
-
7/26/2019 l27 1D Arrays
7/37
Arrays IDimensional
Rules for subscript:
A subscript can be integer constants, Integer variables like i,or expressions that yield integers.
Subscript of subscript is not allowed.
The Maximum subscript valueappearing in a program fora subscripted variable should not exceedthe declared one.
The subscript value ranges from 0 to one less than the
maximum size. For example, If the array size is 5 , then
the first subscript is 0, the second is 1 and so on the last
subscript is 4. In general ith element has subscript (i-1).
7/13/2012 Department of CSE 7
-
7/26/2019 l27 1D Arrays
8/37
Arrays1D
Totalsize:
TheTotalmemorythatcanbeallocatedto1Darrayis
computed
asTotal size =size *(sizeof(data_type));
where size numberofelementsin1Darray
data_type basicdatatype.
sizeof() is an unary operator which returns the size
of expression or data type in bytes.
Forexample,ifwewanttorepresentasetof5
numbersbyanarrayvariablen,thenwemay
declarethevariablenas int n[5];
7/13/2012 Department of CSE 8
-
7/26/2019 l27 1D Arrays
9/37
Arrays1D
7/13/2012 Department of CSE 9
If the values of array n
are 32, 27,64,18,95 then
these values are stored in n as follows.
n[0] n[1] n[2] n[3] n[4]
32 27 64 18 95
& n[0]
n
-
7/26/2019 l27 1D Arrays
10/37
Arrays1D
7/13/2012 Department of CSE 10
int n[5];
? ? ? ? ?
n[0] = 32; 32 ? ? ? ?
n[4] = 95; 32 ? ? ? 95
n[5] = 100; 32 ? ? ? 95100
Logically Not correct ! Out of range
-
7/26/2019 l27 1D Arrays
11/37
Initializingonedimensionalarray
At compile time
At run time[during program execution]
for (i=0; i
-
7/26/2019 l27 1D Arrays
12/37
Initializing one-dimensional array
(compile time)
type arrayname [size]={list of values}
type basic data type
arrayname name of the array.
sizemaximum number of elements and may be omitted.
List of values values separated by commas.
E.g.int number[3] ={ 0,0,0}or {0}
will declare the variable number as an array of size 3and will assign 0 to each element.
7/13/2012 Department of CSE 12
-
7/26/2019 l27 1D Arrays
13/37
Arrays1Dimensional
If the number of values in the list is less than
the number of elements, then only that many
elements will be initialized.
The statement
int age[ ] ={16, 25, 32, 48, 52, 65}; declares the age array to contain 6 elements
with initial values 16, 25, 32, 48,52, 65
respectively.
7/13/2012 Department of CSE 13
-
7/26/2019 l27 1D Arrays
14/37
Arrays1D
7/13/2012 Department of CSE 14
For example
int x[3] = {9,11,13};
cout
-
7/26/2019 l27 1D Arrays
15/37
Initializealltheelementsofan
integerarray
values
to
zero
7/13/2012 Department of CSE 15
int values[20];Begin for loop
Initialize counter
Set limit for counter
Increment counter
Initialize element in array
values
for ( )
values[i]=0;
int i=0; i
-
7/26/2019 l27 1D Arrays
16/37
Initializinganarraytothedaysof
eachmonthanddisplaysthosedays
7/13/2012 Department of CSE 16
const int NUM = 12;
for (int i=0; i < NUM; i++){
cout
-
7/26/2019 l27 1D Arrays
17/37
Programtoreadnelementsintoan
arrayand
print
it
7/13/2012 Department of CSE 17
cout>n;
couta[i];
cout
-
7/26/2019 l27 1D Arrays
18/37
Programtoaddtwoarrayelementsand
storethe
corresponding
elements
sum
in
anotherarray
7/13/2012 Department of CSE 18
cout>n; \\first array
for(i=0;i>a[i];
cout>m; \\second array
for(i=0;i>b[i];
if(m==n)
{
for(i=0;i
-
7/26/2019 l27 1D Arrays
19/37
Displayingelementsofanarrayin
reverseorder.
cout
-
7/26/2019 l27 1D Arrays
20/37
Writeaprogramto reverseanarray
usingonly
one
array
int a[20],i,j,n, temp;
cout>n;
couta[i];
7/13/2012 Department of CSE 20
Example:a[]={1,2,3,4,5}
Enter valuesn=5
12345
Reversedarray
5 4 3 2 1
Array
Reversedarray
a[0]=1 a[0]=5
a[1]=2 a[1]=4
a[2]=3 a[2]=3
a[3]=4 a[3]=2a[4]=5 a[4]=1
-
7/26/2019 l27 1D Arrays
21/37
Reversinganarray
j=n;
for(i=0;i
-
7/26/2019 l27 1D Arrays
22/37
cin>>n; // number of elements
couta[i];
cout>ele>>pos;
for(i=n;i>=pos;i--)
a[i]=a[i-1];
a[pos-1] = ele; //ele is inserted at the specified pos.
cout
-
7/26/2019 l27 1D Arrays
23/37
cout>n;
couta[i];
cout>pos;
for(i=pos; i
-
7/26/2019 l27 1D Arrays
24/37
Overview
1DArray:Syntax:type array_name[size];
MemoryRequirement:
Total size =size *(sizeof(data_type));
Initialization:
typearrayname[size]={list of values}
WriteandRead:
for(i=0;ia[i]; cout
-
7/26/2019 l27 1D Arrays
25/37
Searching&Sorting
SearchingTechniques LinearSearch
BinarySearch
SortingTechniques
BubbleSort
SelectionSort
7/13/2012 Department of CSE 25
-
7/26/2019 l27 1D Arrays
26/37
Linearsearch
7/13/2012 Department of CSE 26
-
7/26/2019 l27 1D Arrays
27/37
Linearsearch Example-2
7/13/2012 Department of CSE 27
-
7/26/2019 l27 1D Arrays
28/37
Linear search- Example-2
7/13/2012 Department of CSE 28
-
7/26/2019 l27 1D Arrays
29/37
int found=0;
cout>n;
for(i=0;ia[i];
}
cout>key;
WAPtosearchanelementinan
array
7/13/2012 Department of CSE 29
for(i=0; i
-
7/26/2019 l27 1D Arrays
30/37
BubbleSortAlgorithm
7/13/2012 Department of CSE 30
-
7/26/2019 l27 1D Arrays
31/37
BubbleSort Example
7/13/2012 Department of CSE 31
-
7/26/2019 l27 1D Arrays
32/37
BubbleSort Example
7/13/2012 Department of CSE 32
-
7/26/2019 l27 1D Arrays
33/37
for(i=0;i>a[i];
for(i=0;i
-
7/26/2019 l27 1D Arrays
34/37
Tobesolved
7/13/2012 Department of CSE 34
WAP to
Insert an element into a sorted array (new arrayshould be sorted one; find the pos in the sortedarray and insert there).
Delete an element from an array (elementshould be read; search it and delete it.)
-
7/26/2019 l27 1D Arrays
35/37
Insertanelementintoasorted
array
7/13/2012 Department of CSE 35
/ / f inding pos i t ion
for(i=0; i < n; i++)
if (ele>=a[i] elea[i])
pos = n;
/ / reposi t ion e lements a f ter pos
/ / inser t e le to pos
Example:insert3intoitsposition
a[]={1,2,4,5,6}
Newarrayafterinserting3:
a[]={1,2,3,4,5,6}
Readarrayelements&elementeletoinsert
-
7/26/2019 l27 1D Arrays
36/37
Tobesolved
7/13/2012 Department of CSE 36
WAP toFind the even & odd numbers stored in a given integer array
num. Display the original array num. Replace the even number
with its square and odd number with its cube in the same
array num. Display the new array num.[HINT:2 replaced with
4 & 3 replaced with 27]
Read two 1D arrays A & B. Merge A & B and store it in C. Array C
has to be sorted [use bubble sort] and displayed.
Now repeat the Merge process done for A & B (Q.2) by carrying
out the sort process (any sorting) while storing it in array A.[HINT: Read each element from array B and insert in
appropriate (sorted) position in array A]
-
7/26/2019 l27 1D Arrays
37/37
Summary
Arrays
1Dimensionalarrays(lists)
Problemson
1D
arrays
Searching
Sorting
7/13/2012 Department of CSE 37