Professional Documents
Culture Documents
Arrays and Operation On Arrays
Arrays and Operation On Arrays
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
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
NO J>=0
AND
Array[J]=value
YES
Set Array[J+1]=Array[J]
Decrement J (J==)
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
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;