Friday, January 06, 2012

ARRAY AND FUNCTION

Introduction

Like other variables, values of an array can also be passed to a function as parameter.

Call by Value

We can pass a individual array element or total numbers of elements to a function.
For example :

#include "stdio.h"
void func(int);
main ()
{
    int arr[5] = { 1, 2, 3, 4, 5 } ;

    printf("Passing the Third element to the Function !  \n" );

    func( arr[2] );
}

void func( int a )
{
    printf("Inside Function ! \nThird element of the Array is %d  \n", a );
}

Display values of an array from another function :

#include "stdio.h"
void show(int);
main ()
{
    int arr1[5], i ;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number for the Array : ", i+1);
   
            scanf("%d", &arr1[i] );
        }

    printf("\nPassing all the elements to the Function ! \n\n");

        for( i = 0 ; i < 5 ; i++ )

            show( arr1[i] ); // When i 's value is 0 first element is passed and so on.

    printf("\n");
}

void show(int a)   //  Each numbers are received at integer a.
{
    printf("Values of the Array are : %d \n",a );
}

Addition of 5 Number :

#include "stdio.h"
int sum(int);
main ()
{
    int arr1[5], i,c ;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number : ", i+1);

            scanf("%d", &arr1[i] );
        }

        for( i = 0 ; i < 5 ; i++ )

            c = sum( arr1[i] );

    printf("\nAddition of 5 Numbers : %d \n\n", c);
}

int sum(int a)
{
    static int sum = 0 ;   //  A static variable is initialized once.

    sum = sum + a;

    return sum;
}

Call by Reference

This is a smarter way to manipulate a array element from another function. Just know the Dimension and send the Base Address of the Array to the desired function.
For example :

#include "stdio.h"
void func( int * );
main ()
{
    int arr[5] = { 1, 2, 3, 4, 5 } ;

    printf("Passing the Address of Third element to the Function !  \n" );

    func( &arr[2] );
}

void func( int *a )
{
    printf("Inside Function ! \nThird element of the Array is %d  \n", *a );
}

Display values of an array from another function :

#include "stdio.h"
void show(int *, int);
main ()
{
    int arr1[5], i ;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number for the Array : ", i+1);

            scanf("%d", &arr1[i] );
        }

    printf("\nPassing the Base Address and Dimension to the Function ! \n\n");

            show( &arr1[0], 5 );

    printf("\n\n");
}

void show(int *a, int b)
{
    for ( int j=0 ; j < b ; j++ )  //  b is the dimension of the array.
    {
        printf("Values of the Array are : %d \n",*a);

        a++;  //  Switching over to the next memory location.
    }
}

Addition of 5 Number :

#include "stdio.h"
int sum(int *);
main ()
{
    int arr1[5],i,c;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number : ", i+1);

            scanf("%d", &arr1[i] );
        }

    c = sum(&arr1[0]);  //  First or Base Address is passed.

    printf("\nAddition of 5 Numbers : %d \n\n", c);
}

int sum(int *a)
{
    int sum=0, j ;

    for( j = 0 ; j < 5 ; j++ )
    {
        sum = sum + *a;
        a++;
    }
    return sum;
}

Or it can be done in the following way too :

#include "stdio.h"
void sum(int *, int *);
main ()
{
    int arr1[5], i, sm = 0;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number : ", i+1);
   
            scanf("%d", &arr1[i] );
        }

    sum(&arr1[0], &sm);

    printf("\nAddition of 5 Numbers : %d \n\n", sm);
}

void sum(int *arr, int *s)
{
    for( int j = 0 ; j < 5 ; j++ )
    {
        *s = *s + *arr;
        arr++;
    }
}

Copy of Array :

#include "stdio.h"
void copy(int *);
main ()
{
    int arr1[5], i ;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number for First Array : ", i+1);

            scanf("%d", &arr1[i] );
        }

    printf("\nCopy the Numbers ! \n\n");

    copy(&arr1[0]);
   
    printf("\n");
}

void copy(int *ranty)
{
    int arr2[5], j ;

    for( j = 0 ; j < 5 ; j++ )
    {
        arr2[j] = *ranty ;

        ranty++;
    }

    for( j = 0 ; j < 5 ; j++ )

        printf("Value of Second Array : %d \n", arr2[j]);
}

Followers