Skip to main content

Passing arrays to functions in C programming

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

Popular posts from this blog

What is Java Applets?

An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. Furthermore, an applet is downloaded on demand, without further interaction with the user. If the user clicks a link that contains an applet, the applet will be automatically downloaded and run in the browser. Applets are intended to be small programs. They are typically used to display data provided by the server, handle user input, or provide simple functions, such as a loan calculator, that execute locally, rather than on the server. In essence, the applet allows some functionality to be moved from the server to the client.

What is manipulator in C++?

Answer: Manipulators are special functions that can be included in the I/O statement to alter the format parameters of a stream. Table-2 shows some important manipulator functions that are frequently used. To access these manipulators, the file < iomanip.h > should be included in program.                               Table-2               Manipulators                                                    Equivalent ios function               set...

Network Topology

A network topology is the arrangement of the computers cable and other components on a network. The major goal of network topology is to find out the most economical and efficient way to connect all the users to the network resources while providing adequate capacity to handle users demand, maintain system reliability and decrease the delay.    Topology is the map of physical network. The type of topology we use defect the type and capabilities network hardware’s its management and possibilities for future expansion. There are many types of network topology, some of these are:   Bus Topology Star Topology Mesh Topology   Ring Topology Tree Topology