Showing posts with label TO CALCULATE THE SUM OF THE ARRAY ELEMENT USING POINTERS AND FUNTIONS. Show all posts
Showing posts with label TO CALCULATE THE SUM OF THE ARRAY ELEMENT USING POINTERS AND FUNTIONS. Show all posts

Thursday, February 5, 2015

TO CALCULATE THE SUM OF THE ARRAY ELEMENT USING POINTERS AND FUNTIONS

AIM
             To write a C program to illustrate parameter passed to the function.

ALGORITHM
Step-1: Start the program
Step-2: Enter the size of the array
Step-3: Enter the elements of the array
Step-4: Print the array elements
Step-5: Call the function with base address of the array passed to it
Step-6: In the calling function gets the base address in the pointer variable
Step-7: Add the array elements
Step-8: Return the value from the calling function to the variable in the called function
Step-9: Print the sum in the called function
Step-10: End the program in the main function

PROGRAM
#include<stdio.h>
main()
{
            int a[10],i,no,sum=0;
            printf(“Enter the size of array...”);
            scanf(“%d”,&no);
            printf(“Enter the elements of the array...”);
            for(i=0;i<no;i++)
                        scanf(“%d”,&a[i]);
            for(i=0;i<no;i++)
                        printf(“\n%d”,a[i]);
            sum=add(&a[0],no);
            printf(“\nThe sum of %d numbers is...%d”,no,sum);
}
add(int *pt,int n)
{
            int i,a=0;
            for(i=0;i<n;i++)
            {
                        a=a+*pt;
                        pt++;
            }
return(a);
}