A R R AYS &
STRINGS
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 is 0..
Arrays
For example
We define an array named salary to represent a set
of 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.
where salary[0], salary[1], salary[2]… etc. respectively
represent the salaries of first, second, third employee.
10/2/2017 Department of CSE 3
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.
10/2/2017 Department of CSE 4
Arrays
Array Declaration:
type name [size];
where type is a valid data type (like int, float...)
name is a valid identifier &
size specifies how many elements the array has to contain.
size field is always enclosed in square brackets [ ] and takes
static values.
For example
an array salary containing 5 elements is declared as
follows
int salary [5];
10/2/2017 Department of CSE 5
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 given one variable name
using only one subscript and such a variable is
called a single-subscripted variable or a one-
dimensional array.
10/2/2017 Department of CSE 6
Arrays – I Dimensional
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 value appearing in a program for
a subscripted variable should not exceed the 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
ith element
subscript is 4. In generalDepartment
10/2/2017 of CSE has subscript (i-1). 7
Arrays - 1D
Total size:
The Total memory that can be allocated to 1Darray is
computed as
Total size =size *(sizeof(data_type));
where size number of elements in 1-D array
data_type basic data type.
sizeof() is an unary operator which returns the size
of expression or data type in bytes.
For example, if we want to represent a set of 5
numbers by an array variable n, then we may
declare the variable n as int n[5];
10/2/2017 Department of CSE 8
Arrays - 1D
If the values of array ‘n’ are 32, 27,64,18,95 then
these values are stored in ‘n’ as follows.
32 27 64 18 95
n[0] n[1] n[2] n[3] n[4]
& n[0]
n
10/2/2017 Department of CSE 9
Arrays - 1D
int n[5];
? ? ? ? ?
n[0] = 32; 32 ? ? ? ?
n[4] = 95; 32 ? ? ? 95
n[5] = 100; 32 ? ? ? 95
100
Logically Not correct ! Out of range
10/2/2017 Department of CSE 10
Initializing one-dimensional array
At compile time
At run time [during program execution]
for (i=0; i<100; i++)
{
if (i < 50)
n[i] = 0;
else
n[i] = 1;
}
10/2/2017 Department of CSE 11
Initializing one-dimensional array
(compile time)
type array-name [size]={list of values}
type basic data type
array-name name of the array.
size maximum 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 3
and will assign 0 to each element.
10/2/2017 Department of CSE 12
Arrays- 1-Dimensional
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.
10/2/2017 Department of CSE 13
Arrays - 1D
For example
int x[3] = {9,11,13};
printf(“%d\n”, x[0]);
printf(“%d\n”, x[1]); Output:
printf(“%d\n”, x[2]); 9
11
13
int x[3] = {9,11,13};
for (int i = 0; i<3; i++)
printf(“%d\n”, x[i]);
;
10/2/2017 Department of CSE 14
Initialize all the elements of an
integer array ‘values’ to zero
int values[20];
Begin for loop
Initialize counter
Set limit for counter for ( int i=0; i<20; i++ )
Increment counter
values[i]=0;
Initialize element in array
‘values’
10/2/2017 Department of CSE 15
Program to read n elements into an
array and print it
printf("enter no of numbers“);
scanf(“%d”,&n);
printf(" \nenter n numbers\n“);
Output:
for(i=0;i<n;i++) enter no of numbers
scanf(“%d”,&a[i]); 3
Numbers entered are:
printf(“\nNumbers entered are:\n”); 9
for(i=0;i<n;i++) 11
printf(“%d”, a[i]); 13
10/2/2017 Department of CSE 16
Program to add two array elements and
store the corresponding elements sum in
another array
printf("enter no for the first array\n“); if(m==n)
{
scanf(“%d”, &n); \\first array
for(i=0;i<m;i++)
for(i=0;i<n;i++)
scanf(“%d”, &a[i]); c[i]=a[i]+b[i];
printf(" enter no of numbers for the
second array\n“); for(i=0;i<n;i++)
scanf(“%d”, &m); \\second array printf(" %d”, c[i]);
}
for(i=0;i<m;i++) Else
scanf(“%d”, &b[i]); printf(" cannot add“);
}
10/2/2017 Department of CSE 17
Displaying elements of an array in
reverse order.
printf(" Enter values\n“);
for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
printf(" nReverse order printing of array\n”);
for(j=i-1;j>=0;--j)// reverse loop
printf(" %d”, a[j]);
10/2/2017 Department of CSE 18
Write a program to reverse an array
using only one array
Example : a[ ]={1, 2, 3, 4, 5}
int a[20],i,j,n, temp; Enter values
n=5
printf(" enter n \n“); 12345
scanf(“%d”, &n); Reversed array
5 4 3 2 1
printf(“ \n Enter values for an array“); Array Reversed array
a[0]=1 a[0]=5
for(i=0;i<n;i++) a[1]=2 a[1]=4
scanf(“%d”, &a[i]); a[2]=3
a[3]=4
a[2]=3
a[3]=2
a[4]=5 a[4]=1
10/2/2017 Department of CSE 19
Reversing an array
Example :
a[ ]={1, 2, 3, 4, 5}
j=n;
for(i=0;i<n/2; i++,j--){ a[i]=a[i]+a[n-1-i];
temp=a[i];
a[i]=a[j-1]; OR a[n-1-i]=a[i]-a[n-1-i];
a[j-1]=temp; a[i]=a[i]-a[n-1-i];
}
printf(" n Reversed array\n"; Output:
for(i=0;i<n;i++) Enter values for an array
n=5
printf(" \t%d”, a[i]); 12345
} Reversed array
5 4 3 2 1
10/2/2017 Department of CSE 20
WAP to insert an element to an array at given position
scanf(“%d”, &n); // number of elements
printf(" \nEnter the elements of array:“);
for(i=0;i<n;i++) Example : insert 9 at 2nd position
scanf(“%d”, &a[i]); a[ ]={1, 2, 3, 4, 5}
printf(“\nEnter the element and position of insertion:“);
scanf(“%d%d”, &ele, &pos);
New array after inserting 9 :
for(i=n;i>=pos;i--) a[ ]={1, 2, 9, 3, 4}
a[i+1]=a[i];
a[pos] = ele; //ele is inserted at the specified pos.
n++;
printf(“\nThe array after insertion is:“);
for(i=0;i<=n i++)
printff(“\t%d”, a[i]);
10/2/2017 Department of CSE 21
WAP to delete an element from an
array Example : delete ele at 2 position nd
printf(" enter no of numbers“); a[ ]={1, 2, 3, 4, 5}
scanf(“%d”, &n);
New array after deleting 3:
printf(" enter n numbers \n“); a[ ]={1, 2, 4, 5}
for(i=0;i<n;i++)
scanf(“%d”, & a[i]);
printf(" enter the position at which the ele to be deleted“);
scanf(“%d”, & pos);
for(i=pos; i<n; i++)
a[i] =a[i+1];
n--;
for(i=0;i<=n;i++)
printf(“%d”, a[i]);
10/2/2017 Department of CSE 22
Overview
1D Array:
Syntax: type array_name[size];
Memory Requirement:
Total size =size *(sizeof(data_type));
Initialization:
type array-name [size]={list of values}
Write and Read:
for(i=0;i<n;i++) for(i=0;i<n;i++)
scanf(“%d”, &a[i]); printf(“%d”, a[i]);
10/2/2017 Department of CSE 23
Searching & Sorting
Searching Techniques
Linear Search
Binary Search
Sorting Techniques
Bubble Sort
Selection Sort
10/2/2017 Department of CSE 24
Linear search
10/2/2017 Department of CSE 25
Linear search- Example-2
10/2/2017 Department of CSE 26
Linear search- Example-2
10/2/2017 Department of CSE 27
WAP to search an element in an
array
int found=0; for(i=0; i<n; i++){
printf("enter no of numbers“); if(a[i]==key)
scanf(“%d”, &n); {found=1;
for(i=0;i<n;i++) pos=i+1;
{ break;
printf(" enter number\n“); }
scanf(“%d”, &a[i]); }
} if(found==1)
printf(" enter the element to be printf(" ele found in %d
searched“); position“, pos );
scanf(“%d”, &key); else
printf(" no is not found“);
10/2/2017 Department of CSE 28
Bubble Sort Algorithm
10/2/2017 Department of CSE 29
Bubble Sort- Example
10/2/2017 Department of CSE 30
Bubble Sort- Example
10/2/2017 Department of CSE 31
Bubble Sort procedure
for(i=0;i<n;i++)
scanf(“%d”, &a[i]); Example :
a[ ]={16, 12, 11, 67}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++) Array after sorting (ascending)
a[ ]={11, 12, 16, 67}
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
10/2/2017 Department of CSE 32
To be solved …
WAP to
Insert an element into a sorted array (new array
should be sorted one; find the pos in the sorted
array and insert there).
Delete an element from an array (element
should be read; search it and delete it.)
10/2/2017 Department of CSE 33
Insert an element into a sorted
array
Read array elements & element ‘ele’ to insert
/ / f indin g p o s i t ion
for(i=0; i < n; i++)
if (ele>=a[i] && ele<=a[i+1]) Example : insert 3 into its position
a[ ]={1, 2, 4, 5,6}
pos = i+1;
else if ( ele>a[i] )
New array after inserting 3 :
pos = n; a[ ]={1, 2, 3, 4, 5,6}
/ / r eposi tion e l em ent s a f t er p o s &
/ / in s er t ‘ e l e’ t o p o s
10/2/2017 Department of CSE 34
To be solved …
WAP to
Find 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 1-D 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]
10/2/2017 Department of CSE 35
Summary
• Arrays
• 1 Dimensional arrays (lists)
• Problems on 1D arrays
• Searching
• Sorting
10/2/2017 Department of CSE 36