Write a C Program to pass array to function to calculate sum

By | 18.03.2017

C Program to pass array to function


Write a C Program to pass array to function to calculate sum. Here’s a Simple Program which take input values into an array and pass array to  function to calculate sum of squares of elements of an array in C Programming Language.

Following C Program ask to the user to enter values that are going to be stored in array. Here we make an intialize an array of 6 elements to be stored in it i.e arr[6].

In this program , we use for loop to input values in the program to store to an array and a func( ) function for calculating sum of squares of elements of an array.


Below is the source code for C Program to pass array to function which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Program to pass array to function*/

#include<stdio.h>
void func(int val[]);
int main( )
{
        int i, arr[6];
        printf("Enter Contents of array : \n");
        for(i=0; i<6; i++)
        {
                scanf("%d", &arr[i]);
        }
        func(arr);

        printf("\nContents of array are now : ");
        for(i=0; i<6; i++)
                printf("%d ", arr[i]);
        printf("\n");

        return 0;
}

void func(int val[])
{
        int sum=0, i;
        for(i=0; i<6; i++)
        {
                val[i]=val[i]*val[i];
                sum+=val[i];
        }
        printf("\nThe sum of squares  =  %d\n", sum);
}

OUTPUT : :


Enter Contents of array :
3
5
8
1
2
5

The sum of squares  =  128

Contents of array are now : 9 25 64 1 4 25

Above is the source code for C Program to pass array to function which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval. Thanks for reading the post.


Feel Free to ASK !

4.5 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments