Program to compute Sum & average of elements in array


    # include <iostream.h>
    # include<conio.h>
    int sum (int[ ], int);
    float avg (int[ ], int);
    void main( )
    {
        int a[ ]={11,33,55,77};
        int size = sizeof(a) /sizeof(int);
        cout<<"Sum (a, size) = "<<sum(a,size)<<endl;
        cout<<"Average (a, size) = "<<avg(a,size)<<endl;
    }
    int sum(int a[ ], int n)
    {
        int sum1 = 0;
        for(int i = 0; i < n; i++)
          sum1= sum1+a[i] ;
        return sum1;
    }
    float avg (int a[ ], int n)
    {
        float sum = 0.0;
        for(int i = 0; i < n; i++)
        sum= sum +a[i] ;
            return sum/n;
    }

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|