You are on page 1of 22

ARRAYS AND

OPERATION ON
ARRAYS
Group Members

Sehar Arshad(20070)
Zainab Dar(20071)
Tayyaba Amjad(20072)
ARRAYS
INTRODUCTION:
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.
“An array is used to store a collection of data, but it is often more useful to think of an array as a collection of
variables of the same type”
Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required
by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimension array.
Initializing Arrays:
You can initialize C++ array elements either one by one or using a single statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } can not be larger than the number of elements that we declare for the
array between square brackets [ ].
Important Concepts of C++ Arrays

Concept Description
Multi-dimensional arrays C++ supports multidimensional arrays. The simplest form of the
multidimensional array is the two dimensional array.
Pointer to an array You can generate a pointer to the first element of an array by simply specifying
the array name, without any index.
Passing arrays to functions You can pass to the function a pointer to an array by specifying the array's name
without an index.
Return array from functions C++ allows a function to return an array.
OPERATION ON ARRAYS
Following are the basic operations supported by an array.
Traverse − print all the array elements one by one.
Insertion − Adds an element at the given index.
Deletion − Deletes an element at the given index.
Search − Searches an element using the given index or by the value.
TRAVERSAL
Traversal is an operation in which each element of a list, stored in an array, is visited.
The travel proceeds from the zero element to the last element of the list.
Algorithm for Traversing an Array
LB = lower bound, UB = upper bound
Start
[Initialize counter variable ] Set i = LB.
Repeat for i = LB to UB
Apply process to arr[i]
[End of loop]
Stop
START
ALGO
Declare variables: i , size

Declare an array arr and initializes its values

Size <---- sizeof(arr)/sizeof(arr[0])

Display: “The array


elements are”

i=0
NO
i<size
YES
Process
i= i+1

STOP
PROGRAM
#include<iostream> PROGRAM FOR AVERAGE OF ARRAY
using namespace std; #include<iostream>
int main() using namespace std;
{ double average(int array[], int size)
int arr[5]={10,12,15,30,40}; {
for(int i=0; i<5; i++) double sum = 0.0;
{ for ( int i=0; i<size; i++ )
cout<<arr[i]<<"\n"; {
} sum +=array[i];
} }
OUTPUT: return(sum/size)
}
int main()
{
int points[]={18, 20, 5, 17, 22};
cout<<average(points, 5)<<endl;
}
OUTPUT:
16.4
INSERTION
Insertion is the operation that inserts an element at a given location of the list.
To insert an element at I location of the list, then all elements from the right of i+1 location have to be shifted one
step towards right .
Algorithm for insertion
Set N = Length of Array
Set I = 1
Set Value = Array[I]
Set J = I – 1
J >= 0 AND Array[J] > Value
Set Array [J + 1] = Array [J]
Decrement J
J—
Array[J + 1] = Value
Increment I (I++)
I<N
The array is sorted
INSERTION SORT

ALGO Set N=Length of Array


Set i=1
Set value= Array[i]
Set J= i-1

NO J>=0
AND
Array[J]=value
YES

Set Array[J+1]=Array[J]
Decrement J (J==)

Array [J+1] = value

Increment i= i++
YES NO
i<N SORTED ARRAY
PROGRAM
#include <iostream> cout<<"Resultant array is\n"<<endl;
using namespace std; for (c = 0; c <= n; c++)
int main() {
{ cout<<array[c];
int array[100], position, c, n, value; }
cout<<"Enter number of elements in array\ return 0;
n"<<endl; }
cin>>n; OUTPUT:
cout<<"Enter elements\n"<<endl;
for (c = 0; c < n; c++)
{
cin>>array[c];
}
cout<<"Enter the location where you wish to
insert an element\n"<<endl;
cin>>position;
cout<<"Enter the value to insert\n"<<endl;
cin>>value;
for (c = n - 1; c >= position - 1; c--)
{
array[c+1] = array[c];
}
array[position-1] = value;
DELETION

Deletion is the operation that removes an element from a given location of the list.
To delete an element from the ith location of the list, then all elements from the right of i+ 1th location have to be
shifted one step towards left to preserve contiguous locations in the array.
 
Algorithm to Delete an element from an Array
Start
[Initialize counter variable ] Set i = pos - 1
Repeat Step 04 and 05 for i = pos - 1 to i < size
[Move i element backward (left) ] set a[i] = a[i+1]
Increase counter. Set i = i + 1
End of step 03 loop
[Reset size of the array. set size = size - 1
Stop
Visual Representation

2 4 6 8 12

Initial Array

Move each element backward by one place whose position is greater than the element you wish to delete.
2 4 8 12

Array with 6 deleted from 3rd position


PROGRAM
#include <iostream> for(i=pos-1;i<size;i++)
using namespace std; a[i]=a[i+1];
int main() size = size - 1;
{ for(i=0;i<size;i++)
int i, size, x, pos; cout << "\na[" << i << "] = " << a[i];
int a[]={1, 87, 68, 10, 8}; return 0;
size=sizeof(a)/sizeof(a[0]); }
cout << "The array elements before OUTPUT:
deletion operation:\n";
for(i=0;i<size;i++)
cout << "a[" << i << "] = " << a[i] <<
"\n";
cout << "Enter the position from
where you wish to delete the element: ";
cin >> pos;
cout << "The array elements after
deletion operation: ";
DELETION LAST ELEMENT OF ARRAY
#include<iostream> {
#include<conio.h> cout<<"\n\nThis value is not in the array: ";
using namespace std; }
int main() else
{ {
int j; --position;
int array[5]; for(j=position;j<=4;j++)
int no; {
int position; array[j]=array[j+1];
cout<<"Enter data in array: "<<endl; }
for(j=0;j<5;j++) cout<<"\n\nNew data after deletion in array is: ";
{
cin>>array[j]; for(j=0;j<4;j++)
} {
cout<<"\n\nData shown in array: "; cout<<array[j];
for(j=0;j<5;j++) }
{ }
cout<<array[j]; getch();
} return 0;
cout<<"\n\nEnter position of element to delete: ";
cin>>position;
}
if(position>5)
OUTPUT
SEARCHING
Searching is the process of finding a given value position in a list of values.
 It decides whether a search key is present in the data or not.
 It is the algorithmic process of finding a particular item in a collection of items.
 It can be done on internal data structure or on external data structure.
Searching Techniques
To search an element in a given array, it can be done in following ways:
 
1. Sequential Search
2. Binary Search
 
Sequential Search:
• Sequential search is also called as Linear Search.
• Sequential search starts at the beginning of the list and checks every element of the list.
• It is a basic and simple search algorithm.
• Sequential search compares the element with all the other elements given in the list. If the element is matched, it
returns the value index, else it returns -1.
The above figure shows how sequential search works. It searches an element or value from an
array till the desired element or value is not found. If we search the element 25, it will go step by
step in a sequence order. It searches in a sequence order. Sequential search is applied on the
unsorted or unordered list when there are fewer elements in a list.
 Function searchValue(value, target)
{
For (var I = 0; I < value.length; i++)
{
If (value[i] == target)
{
Return I;
}
}
Return -1;
}
PROGRAM
#include <stdio.h> }
Int main() }
{ If (cnt == num)
Int arr[50], search, cnt, num; Printf(“%d is not present in array.\n”, search);
   
Printf(“Enter the number of elements in array\n”); Return 0;
Scanf(“%d”,&num); }
  OUTPUT:
Printf(“Enter %d integer(s)\n”, num); M
 
For (cnt = 0; cnt < num; cnt++)
Scanf(“%d”, &arr[cnt]);
 
Printf(“Enter the number to search\n”);
Scanf(“%d”, &search);
For (cnt = 0; cnt < num; cnt++)
{If (arr[cnt] == search) /* if required element found */
{
Printf(“%d is present at location %d.\n”, search,
cnt+1);
Break;
BINARY SEARCH

Binary Search is used for searching an element in a sorted array.


 It is a fast search algorithm with run-time complexity of O(log n).
 Binary search works on the principle of divide and conquer.
 This searching technique looks for a particular element by comparing the middle most element of the
collection.
 It is useful when there are large number of elements in an array.

The above array is sorted in ascending order. As we know binary search is applied on sorted lists only for
fast searching.
For example: if searching an element 25 in the 7-element array, following figure shows how
binary search works:
Binary searching starts with middle element. If the element is equal to the element that we are
searching then return true. If the element is less than then move to the right of the list or if the element
is greater than then move to the left of the list. Repeat this, till you find an element.
 
PROGRAM
#include<stdio.h> While (f <= l) {
#include<conio.h> If (list[m] < sElement)
  F = m + 1;
Void main() Else if (list[m] == sElement) {
{ Printf(“Element found at index %d.\n”,m);
Int f, l, m, size, I, sElement, list[50]; //int f, l ,m : First, Last, Break;
Middle }
Clrscr(); Else
  L = m – 1;
Printf(“Enter the size of the list: “); M = (f + l)/2;
Scanf(“%d”,&size); }
  If (f > l)
Printf(“Enter %d integer values : \n”, size); Printf(“Element Not found in the list.”);
  Getch();
For (I = 0; I < size; i++) }
Scanf(“%d”,&list[i]); OUTPUT:
 
Printf(“Enter value to be search: “);
Scanf(“%d”, &sElement);
 
F = 0;
L = size – 1;
M = (f+l)/2;

You might also like