You are on page 1of 8

Passing arrays to functions

To pass an array to a function the array name must appear by itself, without brackets or subscripts, as
an actual argument within the function call.
When declaring a one-dimensional array as a formal argument, the array name is written with a pair
of empty square brackets.
The size of the array is not specified within the formal argument declaration.
Writing function prototypes.
An empty pair of square bracket must follow the name of each array argument, indicating that the
argument is an array.
If argument names are not included in a function declaration then an empty pair of square brackets
must follow the array argument data type.
When an array is passed to a function, however the values of the array elements are not passed to the
function, rather the array name is interpreted as the address of the first array element. This address is
assigned to the corresponding formal argument when the function is called, the formal arguments therefore
becomes a pointer to the first array element. Arguments that are passed in this manner are said to be passed
by reference rather than by value.
If array element is altered within the function the alteration will be recognized in the calling portion
of the program.
void modify(int a[]); printf("a[%d]=%d\n",count,a[count]);
void main() getch();
{ }
int count; void modify(int a[])
int a[3]; {
clrscr(); int count;
printf("\nFrom main, before calling the function \n"); printf("\nFrom function, After modifying the values ");
for(count=0;count<=2;++count) for(count=0;count<=2;++count)
{ {
a[count]=count+1; a[count]=6;
printf("a[%d]=%d\n",count,a[count]); printf("a[%d]=%d\n",count,a[count]);
} }
modify(a); return;
printf("\nFrom main, After calling the function \n"); }
for(count=0;count<=2;++count)
/* Addition of the two lists*/
void main() void add(int a[],int b[],int c[],int i)
{ {
void read(int[] ,int); for(i=0;i<5;i++)
void dis(int[] ,int); c[i]=a[i]+b[i];
void add(int[] ,int[] ,int[] ,int); }
int a[5],b[5],c[5],i; void read(int c[],int i)
printf("Enter the elements of first list \n"); {
read(a,5); /*read the first list*/ int j;
printf("The elements of first list are \n"); for(j=0;j<i;j++)
dis(a,5); /*Display the first list*/ scanf("%d",&c[j]);
printf("Enter the elements of second list \n"); }
read(b,5); /*read the second list*/ void dis(int d[],int i)
printf("The elements of second list are \n"); {
dis(b,5); /*Display the second list*/ int j;
add(a,b,c,i); for(j=0;j<i;j++)
printf("The resultant list is \n"); printf("%d ",d[j]);
dis(c,5); printf("\n");
} }
Operations on Arrays
There are many applications requiring a search for a particular element. Searching refers to finding out
whether a particular element is present in the list. The method that we use for this depends on how the
elements of the list are organized. If the list is an unordered list, then we use linear or sequential search,
whereas if the list is an ordered list, then we use binary search.

SEARCHING TECHNIQUES: LINEAR OR SEQUENTIAL SEARCH


The search proceeds by sequentially comparing the key with elements in the list, and continues until either
we find a match or the end of the list is encountered. If we find a match, the search terminates successfully
by returning the index of the element in the list which has matched. If the end of the list is encountered
without a match, the search terminates unsuccessfully.
LINER SEARCH
Algorithm
Here m represents the unordered array of elements Printf(search is successful)
N represents number of elements in the array Stop
El represents the value to be searched in the list Endif
Step 1 :[initialize] Step 4
K=0 If(flat=1)then
Flat=1 Print(search is unsuccessful)
Step 2 : repeat step 3 for k=0,1,2,..n-1 Endif
Step 3 if(m[k]=el) Step 5 stop
Then flag=0

/*LINER SEARCH*/ {
#define MAX 10 int i;
void lsearch(int list[],int n,int element) printf("Enter the elements\n");
{ for(i=0;i<n;i++)
int i, flag = 0; scanf("%d",&list[i]);
for(i=0;i<n;i++) }
if( list[i] == element)
{ void main()
printf("The element whose value is %d is {
present at position %d in list\n", int list[MAX], n, element;
element,i+1); printf("Enter the number of elements in the list
flag =1; max = 10\n");
break; scanf("%d",&n);
} readlist(list,n);
if( flag == 0) printf("\nEnter the element to be searched\n");
printf("The element whose value is %d is not scanf("%d",&element);
present in the list\n",element); lsearch(list,n,element);
} }
void readlist(int list[],int n)

void main() }
{ printf("enter any element for search");
int a[10],i,item,n; scanf("%d",&item);
printf("how many elements in a array"); //a[n]=item;
scanf("%d",&n); for(i=0;i<n;i++)
printf("\nenter the element of array\n"); {
for(i=0;i<n;i++) if(item==a[i])
{ {
scanf("%d",&a[i]); printf("search is successful.");
getch(); {
exit(); printf("search is unsucessful.");
} }
} getch();
if(a[i]==item) }

BINARY SEARCH
The prerequisite for using binary search is that the list must be a sorted one. An array based binary search
selects the middle element in the array and compares its value to that of the key value. Because the array is
sorted if the key value is less than the middle value then the key must be in the first half of the array.
Likewise if the value of the key item is greater than that of the middle value in the array, then it is known
that the key lies in the second half of the array. In either case, we can , in effect throw out one half of the
search space or array with only one comparison.
Algorithm
Step1: Declare an array k of size n I e k (n) is an if (key<k[mid])then
array high=mid-1
Step 2: I=0 else
Step3: low<-0, high<-n-1 low=mid+1
Step 4: while(low<=high)do endif
mid=(low+high)/2 endif
if(key=k[mid])then end while
write(record is at postion)mid +1 step 5 write value not found
else step 6 stop

/*BINARY SERCH*/
#define MAX 10 printf("The element whose value is %d is not
void bsearch(int list[],int n,int element) present in the list\n",
{ element);
int l,u,m, flag = 0; }
l = 0; void readlist(int list[],int n)
u = n-1; {
while(l <= u) int i;
{ printf("Enter the elements\n");
m = (l+u)/2; for(i=0;i<n;i++)
if( list[m] == element) scanf("%d",&list[i]);
{ }
printf(" The element whose value is %d void main()
is present at position %d in list\n",element,m+1); {
flag =1; int list[MAX], n, element;
break; printf("Enter the number of elements in the list
} max = 10\n");
else scanf("%d",&n);
if(list[m] < element) readlist(list,n);
l = m+1; printf("\nEnter the element to be searched\n");
else scanf("%d",&element);
u = m-1; bsearch(list,n,element);
} }
if( flag == 0)
/*Program to search an element in a array.*/
void main() {
{ lb=mid+1;
int a[10],n,i,lb,ub,mid,item; }
printf("enter how many elements"); else
scanf("%d",&n); {
printf("enter array elements"); ub=mid-1;
for(i=0;i<n;i++) }
{ mid=(lb+ub)/2;
scanf("%d",&a[i]); }
} if(a[mid]==item)
printf("enter item for search"); {
scanf("%d",&item); printf("Search is successful");
lb=1; }
ub=n; else
mid=(lb+ub)/2; /*find the median*/ {
while(lb<ub&&a[mid]!=item) printf("Search is unsuccessful");
{ }
if(a[mid]<item) }
/*In this program we deletion the any element in array elements.*/
void main() if(a[k]==ele)
{ loc=k;
int a[20],i,loc,k,n,ele; }
clrscr(); for(i=loc;i<=n-1;i++)
printf("Enter how many elements"); {
scanf("%d",&n); a[i]=a[i+1];
printf("Enter elements"); n=n-1;
for(i=0;i<n;i++) }
{ printf("\nArray after deletion\n");
scanf("%d",&a[i]); for(i=0;i<n;i++)
} {
printf("Enter the element is deletion="); printf("%d\t",a[i]);
scanf("%d",&ele); }
for(k=0;k<n;k++) getch();
{ }
/*This program is used to insertion a element in an appropriate location
in array. */
void main() scanf("%d",&item);
{ j=n;
int a[15],i,j,k,n,item; while(j>=k)
clrscr(); {
printf("Enter How many elements"); a[j+1]=a[j];
scanf("%d",&n); j=j-1;
printf("enter an elements"); }
for(i=1;i<=n;i++) a[k]=item;
{ n=n+1;
scanf("%d",&a[i]); printf("\nThe input of new array element is : ");
} for(i=1;i<=n;i++)
printf("\nenter location where the item is stored"); {
scanf("%d",&k); printf("\n%d\t",a[i]);
k=k+1; }
printf("enter the item"); }

You might also like