Like any other variables, we
can also pass entire array to a function. An array name can be named as an
argument for the prototype declaration and in function header. When we call the
function no need to subscript or square brackets. When we pass array that pass
as a call by reference because the array name is address for that array.
/* Program to
illustrate passing array to function */
#include<stdio.h>
void display(int) ; /* function prototype */
main( )
{
int num[5] = {100, 20, 40, 15, 33, i ;
clrscr( ) ;
printf (“\n The content of array is \n”) ;
for (i=0; i<5; i++)
display (num[i]) ; /*Pass array element fo fun */
getch{ } ;
}
void display(int n)
{
printf (“\t%d”, n ) ;
}
Output:
The content of array is
100 20 40
15 3
/* Program to
read 10 numbers from keyboard to store these num into array and then calculate
sum of these num using function */
#include<stdio.h>
#include<conio.h>
int sum (int a[]) ;
void output (int a[]) ;
main( )
{
int a[10], s, i ;
clrscr( ) ;
printf (“Enter 10 elements : \n”) ;
for (i=0 ; i<=9 ; i++) ;
scanf (“%d”, &a[i]) ;
output (a) ;
s = sum (a) ;
printf (“Sum of array element is :\n”, s) ;
getch( ) ;
}
int sum (int a[ ] )
for (i=0 ; i<=9 ; i++)
n = n+a[i] ;
return (n) ;
}
void output (int a[])
{
int i;
printf (“The elements of array are : \n”) ;
for (i=0 ; i<=9 ; i++) ;
printf (“%d\n”, a[i]) ;
}
Output:
Enter 10 elements:
5 12
15 8
10 20
2 30
8 40
The elements of array are:
5
15
10
2
8
12
8
20
30
40
Sum of array element is : 150
/* Passing two
dimensional array as an argument to function */
/* Program to
read two matrices A and B of order 2×2 and subtract B from A using function*/
#include<stdio.h>
#include<conio.h>
void input (int t[ ] [2]) ;
void output (int t1[ ] [2]) , int t2[ ] [2] ) ;
main( )
{
int a[2] [2], b[2] [2] ;
clrscr ( ) ;
printf (“Enter first array :\n” ) ;
input(a)
printf (“Enter second array :\n”) ;
output(a,b) ;
getch( ) ;
}
void input (int t[ ] [2])
{
int i,j ;
for (i=0 ; i<=1 ; i++)
for (j=0 ; j<=1 ; j++)
scanf (“%d”, &t [ i] [j]) ;
}
void
output (int t1[ ] [2], int t2[ ] [2])
{
int i,j ;
int s[2] [ 2] ;
for (i=0, i<=1 ; i++)
for (j=0 ; j<=1 ; j++)
s[i] [j] = t1[i] [j] – t2[i] [j] ;
for (i=0 ; i<=1 ; i++)
{
for ( ) = 0 ; j<=1 ; j++)
printf(“%d\t”, s[i] [j]) ;
printf(“\n”) ;
}
}
Output:
Enter first array:
4
3
2
4
Enter second array:
1
2
3
4
Subtraction is:
3 1
-1 2
/* Program to
illustrate string initialization */
#include<stdio.h>
#include<conio.h>
void main( )
{
char name[ ] = “Er.Ojha” ;
clrscr( ) ;
printf (“Your name is :%s\n”, name)
getch( ) ;
}
Output:
Your name is:
Er.Ojha
/* A program to
read a string and check for palindrome */
void main( )
{
char st[40] ;
int len i, pal = 1 ;
clrscr( ) ;
printf (“Enter string of our choice:”) ;
gets(st) ;
len = strlen(st) ;
for (i=0; i<(len 12); i++)
{
if (st[i] ! = st[len-i-1])
pal = 0 ;
}
if (pal = = 0)
printf (“\n The input string is not palindrome”) ;
else
printf (“\n the input string is palindrome”) ;
getch( ) ;
}
Comments
Post a Comment