Functions 343
}
artyO
{
static int k;
int b[5Hl,2J,4r5l;
retum(bPc++]);
I
OUTPUT;
1 2 3 4 5
OUTPUT;
1 2 3 4 5
Explanation In the above program integer array b [ ] is declared and initialized in function
a rry (). The function a rry () is called five times. During each call it returns successive elements
of array b [] to function main (). Thus, all elements of array b[ ] are displayed,
f) Interchange of array elements
10.29 Write a program to interchange array elements of two arrays using a function.
# include <stdio.h>
# include <conio.h>
void mainO
{
int re ad ( ) ;
void c h a n g e (in t* ,in t* );
in t x , a [ 5 ] , b [5 ];
c l r s c r ( ) ;
printf ("Enter 10 Numbers :");
for (x=0;x<10;x++)
{
if (x<5)
a[x]=read();
else
b[x-5]=read();
>
344 Programming and Data Structures
printf ("n Array A & B");
for (x=0;x<5;x++)
{
printf (" %7d%8d", a[x],b[x]);
change(&a[x],&b(x]);
}
printf ("nNow A & B");
for (x=0;x<5,*x++)
{ printf ("n%7d%8d", a[x],b[x]); >
read 0
{
int x;
scanf ("%d",&x);
retu rn ( x ) ;
}
void change ( int *a, int *b)
{
int *k;
*a=*a+*b;
*b= *a-*b;
*a«=*a-*b;
}
OUTPUT: 1234567890
Array A & B
1 6
2 7
3 8
4 9
5 0
A &
B
6 1
7 2
8 3
9 4
0
5
Functions 345
Explanation Interchange of array elements can be done by call by reference. Here, we are passing
the address of array elements of arrays a [] and b [] to the function change (). The function change ()
interchanges the contents of elements.
g )
Global pointer
10.30 Write a program to read array elements declared in different functions using the
global pointer declaration.
# include <stdio.h>
# inclu de <conio.h>
in t *p/*q; vo id m ain()
{
In t aiB 0,call (in t ) ,k[5] ={3,8 ,5,2,5};
p=k;
dxscrO;
call(5);
while (m!=5)
{
printf ("%3d",*q);
m++;
q++;
}
call(int j)
{
static int m=0,u[5]={5,l,6,0,6};
q=u;
while (m!=j)
printf ("%3d",*p);
m++;
p++;
}
printf ("n");
retum(NULL);
}
Q U TPUI;
38525
51606
Explanation In the above program, 'p' and 'q' are declared as global pointers, that is, they are
defined outside the main () function. The pointer 'p' contains the base address of array k [ j and
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.85