9.10 – Sending array as argument

by subbu on November 22, 2013

Sending an array as an argument:

Like a normal variable even an array can be send as an argument into the sub function.
Here the name of array is given as an actual argument with the calling statement.
An array is defined as a formal argument with the signature of function definition.
Elements of actual argument (original array) can be directly accessed by the formal argument from the sub function.
in case of single dimensional array, it is optional to specify the dimension (size) with the formal argument and prototype and mandatory in case of two or multidimensional arrays.
While sending an array as an argument, even the size n need to be send as an argument to control the loop in sub function.

Example:

#include<stdio.h>
void display(int[],int);
int main()
{
  int a[]={14,41,32,76,77,89};
  display(a,6);
  return 0;
}
void display(int p[],int n)
{
int i;
  printf("Elements of array:\n");
  for(i=0;i<n;i++)
    printf("\t%d",p[i]);
}

Output:
14      41      32      76      77      89

Example explained:
Sending an array as argumentIn this example the name of array a is given as actual argument, an array p[] is defined as a formal argument. Now p[] could access every element of array from the sub function display(). Here, the size of array n is also sent as an argument to control the loop, which helps to fetch the elements of array one by one.

Specification: Accept “n” elements into an array and print the sum of all elements using the function sum()

Program:

#include<stdio.h>
int sum(int[],int);
int main()
{
 int a[50],n,i,sm;
 printf("How many elements?");
 scanf("%d",&n);
 printf("Enter %d elements:\n",n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 sm=sum(a,n);
 printf("Sum of all the elements %d",sm);
 return 0;
}
int sum(int x[],int n)
{
 int s,i;
 for(s=0,i=0;i<n;i++)
    s=s+x[i];
 return s;
}

Execution:
How many elements? 5
Enter 5 elements:
12   4   5   20   12
Sum of all the elements 53

Specification: Accept a nxm matrix and print the norm of matrix using the function getnorm().

Logic: Norm is the square root of sum of squares of all the elements.

Program:

#include<stdio.h>
#include<math.h>
double getnorm(int[50][50],int,int);
int main()
{
 int a[50][50],n,m,i,j;
 printf("Enter the class of matrix:\n");
 scanf("%d%d",&n,&m);
 printf("Enter %dx%d matrix:\n",n,m);
 for(i=0;i<n;i++)
   for(j=0;j<m;j++)
    scanf("%d",&a[i][j]);
 printf("The norm of matrix %lf",getnorm(a,n,m));
 return 0;
}
double getnorm(int p[50][50],int n,int m)
{
 int i,j;
 long sum;
 for(sum=0,i=0;i<n;i++)
   for(j=0;j<m;j++)
     sum=sum+pow(p[i][j],2); /* sum of squares of all the elements */
 return sqrt(sum);
}

Execution:
Enter the class of matrix:
3    4
Enter 3×4 matrix:
1    2    3     4
4    3    2    1
1    2    4    3
The norm of matrix: 9.486833

Points to remember:

Point 1 : In case of primary type of arguments (normal variables), change in formal arguments result no change in actual arguments because it follows pass by value that is only values of actual arguments are assigned to the formal arguments.

Example:

#include<stdio.h>
void process(int,int);
int main()
{
 int x=10,y=20;
 process(x,y);
 printf("x=%d",x);
 printf("\ny=%d",y);
 return 0;
}
void process(int p,int q)
{
 p=p+10;
 q=q+10;
}

Output:
x=10
y=20

Example explained:

x, y are local variables of main(). p, q are local variables of process(). Only the values of x, y are assigned to p, q. The change in p, q doesn’t reflect any change in x, y.

Point 2: In case of derived type (array) change in formal argument reflects the change in actual argument because it follows pass by reference (We will discourse it in pointers) that is formal argument is just the reference to the original array but not a separate array.

Example:

#include<stdio.h>
void process(int[],int);
int main()
{
 int a[]={14,41,32,76,77,89};
 int i;
 process(a,6);
 printf("The resultant array:\n");
 for(i=0;i<6;i++)       /* printing array */
  printf("%5d",a[i]);
 return 0;
}
void process(int p[],int n)
{
 int i;
 for(i=0;i<n;i++)   /* changing array elements */
   p[i]=p[i]+10;
}

Output:
24    51     42    86    87    99

Example explained:
Changing array through formal argumentHere array a is send as an argument to the function process(), formal argument p[] could access the array from the sub function. Incrementing the values of elements through p[] resulted the change in the elements of (original array) actual argument a[].

Specification: Accept n elements into an array, print them in ascending order using the function sort()

Logic:

We read n elements into an array a, send as an argument to the sub function sort(). Sorting array p[] in sub function results sorting elements of original array a in the main() function.

Program:

#include<stdio.h>
void sort(int[],int);
int main()
{
 int a[50],n,i;
 printf("How many elements?");
 scanf("%d",&n);
 printf("Enter %d elements:\n",n);
 for(i=0;i<n;i++)
  scanf("%d",&a[i]);
 sort(a,n);      /* sending array as argument */
 /* printing resultant array*/
 printf("Elements in ascending order:\n");
 for(i=0;i<n;i++)
   printf("%5d",a[i]);
 return 0;
}
void sort(int p[],int n)
{
 int i,j,temp;
 /* sorting elements using bubble sort */
 for(j=n-2;j>=0;j--)
   for(i=0;i<=j;i++)
    if(p[i+1]<p[i])
    {
      temp=p[i];
      p[i]=p[i+1];
      p[i+1]=temp;
    }
}

Execution:
How many elements?5
Enter 5 elements:
56  34  12  44  20
Elements in ascending order:
12   20   34   44   56

Example explained
Change in p[] reflected the change in a[]

Previous post:

Next post: