0% found this document useful (0 votes)
4 views6 pages

Array and Function

CS HSC Array and function 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Array and Function

CS HSC Array and function 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Arrays and Function

Friday, July 17, 2020 MITHIBAI COLLEGE 1


Introduction
• An array can be passed to function.
• Arrays are always passed as ref to the function.
• General form :
ret_type func_name (data_type arr_name [ ] , int SIZE)
{
// function body
}

Friday, July 17, 2020 KAUSHIK PANCHAL 2


Example
void input (int a[], int N) void display (int a[], int N)
{ {
for(int i = 0; i< N ; i++) for(int i = 0; i < N ; i++)
{ {
cin>>a[i]; cout<<a[i]<< “ ”;
} }
} }

Friday, July 17, 2020 KAUSHIK PANCHAL 3


Example (To input 3 arrays and print it )
#include <iostream.h> cout<<"Enter twenty integers : ";
#include <conio.h> input(a,20);
void input (int a[], int N) ;
void display (int a[], int N) ; cout<<“Elements of first Array are :";
display(a,5);
void main()
cout<<“Elements of Second Array are:";
{ display(b,10);
int a[5],b[10],c[20];
clrscr(); cout<<“Elements of Third Array are : ";
cout<<"Enter five integers : "; display(c,20);
input(a,5);
cout<<"Enter ten integers : "; getch();
input(b,10); }

Friday, July 17, 2020 KAUSHIK PANCHAL 4


Binary Search (USER DEFINED FUNCTION)
#include <iostream.h> if(x == a[m])
#include <conio.h> return m;
int Bsearch(int a[],int N,int x) else
{ return -1;
int l=0,u=N-1,m; }
m = ( l + u ) / 2 ;
while(l<u && a[m] != x)
{
if ( x > a[m] )
l = m + 1;
else
u = m – 1 ;
m = ( l + u ) / 2 ;
}

Friday, July 17, 2020 KAUSHIK PANCHAL 5


Binary Search (USER DEFINED FUNCTION)
void main() if(pos != -1)
{ cout<<“Search is successful and
int a[10]= {10,20,30,40,50,60,70, number found at position: “ << pos;
80,90,100}; else
int x,pos; cout<<“Search is unsuccessful";
clrscr(); getch();
cout<<“Elements of array are : "; }
display(a,10); }

cout<<“Enter number to be
searched : ";
cin >> x ;

pos = Bsearch(a,10,x);

Friday, July 17, 2020 KAUSHIK PANCHAL 6

You might also like