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

Database Management System(DBMS)

Data and Information  Data is an unprocessed raw fact which does  not give complete meaning about any object,person, entity etc. But when various inter-related data arranged in a row, it gives a complete meaning which is referred to as information. For Example: ID Name Address Program 1101 Ram Shrestha Biratnagar BScIT         is information of a specific person. Database Database is a repository(storage) for the organized collection of related information  of data items. It Consist of meaningful arrangement of data, relationship, constraints, schema etc. In modern days we define database as a computerized record-keeping system used to store information and allows users to retrieve and update these information on their request. Some example of databases are telephone directory, data of SLC result, student and employee records etc. Database Management System(DBMS) Database is the softw...

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...