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

Object Oriented Programming

Object Oriented Programming Programming paradigm that represents the concept of "objects" that have data fields (attributes that describe the object) and associated procedures known as methods Programming methodology based on objects, instead of just functions and procedures Focuses on data rather than process As individual objects can be modified without affecting other aspects of the program, it is easier for programmers to structure and organize software programs Easier to update and change programs written in object-oriented languages Simula was the first object oriented programming language Eg: C++, Java, etc. Features of OOPS Objects Referred as instance of class Basic run-time entities in an object-oriented system a person, a place, a bank account, a table of data, etc can be an object They occupy space in memory that keeps its state  Each object contains data and code to manipulate the data  Classes Blue print or prototype  which defi

2. Star Network topology

In star topology all devices or computers are connected to one centralized server. They are connected with server by hub.  Star topology is used in that network where end points are directly reachable from central location. Because each nodes link to the server by different cables. There is no risk of data collisions. Each computer connected to the hub by its own cable segments only a single computer can transmit at a time and every computer on the network wait for message. Thus it is best on broadcasting architecture. a.      Advantages i.                     Devices are attached to server by its own cable, thus break down of cable affects only to that nodes. ii.                   Failure of single node will not affect the rest of the network. iii.                 Central hub can be represent with other having more ports to plugs the nodes. b.      Disadvantages i.                     Failure of central hub will affect whole network. ii.                   More expens