Function Programming : Array programming in C

Write  a program using c-function to read from an array and do the sum of all elements from that array.

Source Code in C:

#include<stdio.h>
int arr(int a[], int len);
int main()
{
     int len, ret, i;
     printf ("\nEnter your array size: ");
     scanf ("%d", &len);
     int a[len];
     printf("\nEnter your values: ");
     ret=arr(a, len);
     printf("\nThe sum is: %d", ret);
 }

int arr(int b[ ], int n)
{
     int i, result =0;
         for(i=0; i<n; i++)
         {
              scanf ("%d", &b[i]);
              result += b[i];
         }
         for(i=0; i<n; i++)
               result +=  b[i];
          
         return (result);
}

21 comments:

  1. #include
    int arr(int a[],int len);
    main()
    {
    int len,ret,i;
    printf("enter your array size:");
    scanf("%d",&len);
    int a[len];
    printf("enter your values:\n");
    ret=arr(a,len);
    printf("\nthe sum is:%d\n", ret);
    for(i=0;i<len;i++)
    printf("%d\n",a[i]);
    }
    int arr(int b[],int n)
    {
    int i,result=0;
    for(i=0;i<n;i++)
    {
    scanf("%d",&b[i]);
    result+=b[i];
    }
    return(result);
    }

    ReplyDelete
  2. #include
    main()
    {
    int n,j,sum=0;
    printf("Array size is : ");
    scanf("%d",&n);
    int a[n];
    array(a,n);
    printf("\nYour values are : ");
    for(j=0;j<n;j++)
    printf("\n%d",a[j]);
    }

    int array(int b[],int n)
    {
    int i,sum=0;
    printf("\nEnter your values : \n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&b[i]);
    sum+=b[i];
    }
    printf("\nSum of all elements : %d\n",sum);

    ReplyDelete
  3. #include
    int arr(int a[],int len);
    main()

    {
    int len,j,sum=0,s=0;
    printf("Enter the array size: ");
    scanf("%d",&len);
    int a[len];
    arr(a,len);
    printf("\nthe values are: ");
    for(j=0;j<len;j++)
    printf("\n%d",a[j]);
    }

    int arr(int b[],int n)

    {
    int i,s=0;
    printf("Enter the values: \n");
    for(i=0;i<n;i++)

    {
    scanf("%d",&b[i]);
    s+=b[i];
    }

    printf("\nthe sum is: %d ",s);

    }

    ReplyDelete
  4. #include
    #include
    int read_sum(int s[],int q);
    int main()
    {
    int j,a,result;
    printf("\n Enter size");
    scanf("%d",&a);
    int n[a];
    result=read_sum(n,a);
    j=0;
    do
    {
    printf("\n%d",n[j]);
    j++;
    }while(j<a);

    getch();
    return 0;

    }

    int read_sum(int s[],int q)
    {
    int i,sum=0;
    printf("\n Enter your values");
    i=0;
    do
    {
    scanf("%d",&s[i]);
    sum+=s[i];
    i++;
    }while(i<q);
    printf("\n The sum is %d",sum);
    printf("\n Your values are");

    }

    ReplyDelete
  5. #include
    #include
    int shohan(int p[],int m);
    int main()
    {
    int k,rem,n;
    printf("\n Enter array size: ");
    scanf("%d",&n);
    int a[n];
    rem=shohan(a,n);
    for(k=0;k<n;k++)
    printf("\n%d",a[k]);
    getch();
    return 0;

    }

    int shohan(int p[],int m)
    {
    int i,sum=0;
    printf("\n Enter array values");
    for(i=0;i<m;i++)
    {
    scanf("\n%d",&p[i]);
    sum+=p[i];
    }
    printf("\nThe sum is %d",sum);
    printf("\n values are");
    }

    ReplyDelete
  6. #include
    #include
    int read_sum(int b[],int j);
    int main()
    {
    int n,shohan,h;
    printf("\n Enter size");
    scanf("%d",&n);
    int a[n];
    shohan=read_sum(a,n);
    printf("\n your value are");
    h=0;
    while(h<n)
    {
    printf("\n %d",a[h]);
    h++;

    }

    getch();
    return 0;

    }

    int read_sum(int b[],int j)
    {
    int i,sum=0;
    printf("\n Enter values");
    i=0;
    while(i<j)
    {
    scanf("%d",&b[i]);
    sum+=b[i];
    i++;
    }

    printf("\n The sum is %d",sum);

    }

    ReplyDelete
  7. #include
    #include
    int arr(int a[],int len);
    main()
    {
    int len,j,sum=0,s=0;
    printf("Enter the array size: ");
    scanf("%d",&len);
    int a[len];
    arr(a,len);
    printf("\nthe values are: ");
    for(j=0;j<len;j++)
    printf("\n%d",a[j]);
    }

    int arr(int z[],int n)

    {
    int l,s=0;
    printf("Enter the values: \n");
    for(l=0;l<n;l++)

    {
    scanf("%d",&z[l]);
    s+=z[l];
    }

    printf("\nthe sum is: %d ",s);

    }

    ReplyDelete
  8. #include
    #include
    int funcx(int m[],int n);
    int main()
    {
    int n,result,k;
    printf("\n Enter size:");
    scanf("\n%d",&n);
    int m[n];
    result=funcx(m,n);
    for(k=0;k<n;k++)
    {
    printf("\n%d",m[k]);
    }
    getch();
    return 0;
    }

    int funcx(int m[],int n)
    {
    int i,sum=0;
    printf("\n Enter values:");
    for(i=0;i<n;i++)
    {
    scanf("\n%d",&m[i]);
    sum+=m[i];
    }
    printf("\n The sum is %d:",sum);
    printf("\n Entered values are:");
    }

    ReplyDelete
  9. #include
    main()
    {
    int p;
    printf("Enter the number");
    scanf("%d",&p);
    sum(p);
    }
    int sum (int Q)
    {
    int i,R=1,sum=0;
    for(i=1;i<=5;i++)
    {
    R=Q*i;
    sum+=R;
    }
    printf("%d",sum);
    }

    ReplyDelete
  10. #include

    void arraySum(int z[],int n)
    {
    int l,s=0;
    printf("Enter the values: \n");
    for(l=0; l<n; l++)
    {
    scanf("%d",&z[l]);
    s+=z[l];
    }
    printf("\nthe sum is: %d ",s);
    }

    void main()
    {
    int len,j,sum=0,s=0;
    printf("Enter the array size: ");
    scanf("%d",&len);
    int a[len];
    arraySum(a,len);
    printf("\nthe values are: ");
    for(j=0; j<len; j++)
    {
    printf("\n%d",a[j]);
    }
    }

    ReplyDelete
    Replies
    1. #include
      int arr(int a[],int len);
      main()
      {
      int len,ret,i;
      printf("enter your array size:");
      scanf("%d",&len);
      int a[len];
      printf("enter your values:\n");
      ret=arr(a,len);
      printf("\nthe sum is:%d\n", ret);
      for(i=0;i<len;i++)
      printf("%d\n",a[i]);
      }
      int arr(int b[],int n)
      {
      int i,result=0;
      for(i=0;i<n;i++)
      {
      scanf("%d",&b[i]);
      result+=b[i];
      }
      return(result);
      }

      Delete
  11. #include
    using namespace std;
    class Array

    {
    public:
    int n;
    Array ();
    void output();
    ~Array();
    };
    Array::Array()
    {
    cout<<"Enter how many to hold in Array:"<>n;
    }
    void Array::output()
    {
    int arr[n],i,j;
    cout<<"Enter"<>arr[i];
    cout<<"The Array is:";
    for(j=0;j<n;j++)
    cout<<arr[j]<<endl;
    }
    Array::~Array()
    {

    }
    int main()
    {
    Array obj;
    obj.output();
    return 0;
    }

    ReplyDelete
  12. #include
    using namespace std;
    class Array
    {
    public:
    int n;
    Array();
    void output();
    ~Array();
    };
    Array::Array()
    {
    cout<<"Enter how many numbers to hold in array: ";
    cin>>n;
    cout<>arr[i];
    sum=sum+arr[i];
    }
    cout<<endl<<"The sum is: "<<sum<<endl<<endl;
    cout<<"The array is: "<<endl;
    for(j=0; j<n; j++)
    cout<<arr[j]<<endl;

    }
    Array::~Array()
    {

    }
    int main()
    {
    Array obj;
    obj.output();
    return 0;
    }

    ReplyDelete
  13. #include
    using namespace std;

    class Array
    {
    public:
    int n,sum=0;
    Array();
    void process();
    void show();
    ~Array();
    };
    Array::Array()
    {

    cout<<"Enter how many number do u want: ";
    cin>>n;
    }
    void Array::process()
    {

    int i,arr[n];
    cout<<"Enter values in the array: ";
    for(i=0; i>arr[i];
    sum=sum+arr[i];
    }
    cout<<"Printing the array: ";
    for(i=0; i<n; i++)
    cout<<endl<<arr[i];

    }
    void Array::show()
    {
    cout<<endl<<"Summation is: "<<sum;
    }
    Array::~Array()
    {
    cout<<endl<<"Destructing....";
    }
    main()
    {
    Array ob;
    ob.process();
    ob.show();
    }

    ReplyDelete
  14. #include
    using namespace std;
    class Array
    {
    public:
    int n;
    Array();
    void output();
    ~Array();
    };
    Array::Array()
    {
    cout<<"Enter how many numbers to hold in array: ";
    cin>>n;
    cout<>arr[i];
    sum=sum+arr[i];
    }
    cout<<endl<<"The sum is: "<<sum<<endl<<endl;
    cout<<"The array is: "<<endl;
    for(j=0; j<n; j++)
    cout<<arr[j]<<endl;

    }
    Array::~Array()
    {

    }
    int main()
    {
    Array obj;
    obj.output();
    return 0;
    }


    ReplyDelete
  15. #include
    using namespace std;
    class Array

    {
    public:
    int n;
    Array ();
    void output();
    ~Array();
    };
    Array::Array()
    {
    cout<<"Enter how many to hold in Array:"<>n;
    }
    void Array::output()
    {
    int arr[n],i,j,sum=0;
    cout<<"Enter"<>arr[i];
    sum=sum+arr[i];}
    cout<<"The sun is:"<<sum<<endl;
    cout<<"Array is:"<<endl;
    for(j=0;j<n;j++)
    cout<<arr[j]<<endl;


    }
    Array::~Array()
    {

    }
    int main()
    {
    Array obj;
    obj.output();
    return 0;
    }


    ReplyDelete
  16. #include
    using namespace std;
    class arr
    {
    public:
    int n,summ=0;
    arr();
    int show();
    void sum();
    ~arr()
    {

    }
    };
    arr::arr()
    {
    cout<<"Input array size: ";
    cin>>n;
    }
    int arr::show()
    {
    int a[n];
    for(int i=0;i>a[i];
    cout<<"Given array value:";
    for(int i=0;i<n;i++)
    {
    cout<<a[i]<<endl;
    summ=summ+a[i];
    }
    return summ;
    }
    void arr::sum()
    {
    cout<<"Array sum is:"<<summ<<endl;
    }
    int main()
    {
    arr appel;
    appel.show();
    appel.sum();
    }

    ReplyDelete
  17. using C++

    #include
    using namespace std;

    class cons
    {
    public:
    int num;
    cons(int i);
    ~cons();

    void result();
    };

    cons::cons(int i)
    {
    num = i;
    }

    void cons::result()
    {
    int arr[num];
    int i, total = 0;
    cout << "Enter your array element --: ";
    for (i = 0; i < num; i++)
    cin >> arr[i];

    for (i = 0; i < num; i++)
    total += arr[i];

    cout << "\nThe total of the array --: " << total << "\n";
    }

    cons::~cons()
    {
    cout << "[+]MEMORY RELEASED[+]";
    }

    int main()
    {
    int i;
    cout << "Enter how many element in your array --: ";
    cin >> i;

    cons obj(i);
    obj.result();

    return 0;
    }

    ReplyDelete
  18. #include
    using namespace std;
    class arr
    {
    public:
    int num,i,sum=0;
    arr();
    ~arr()
    {
    cout<>a[i];
    cout<<"Your array numbers are: "<>num;
    }
    int main()
    {
    arr obj;
    obj.func();
    return 0;
    }

    id201510660

    ReplyDelete
  19. #include
    using namespace std;
    class arr
    {
    public:
    int num,i,sum=0;
    arr();
    ~arr()
    {
    cout<>a[i];
    cout<<"Your array numbers are: "<>num;
    }
    int setu()
    {
    arr obj;
    obj.func();
    return 0;
    }

    id201510992

    ReplyDelete
  20. using c++

    #include
    using namespace std;

    class cons
    {
    public:
    int num;
    cons(int i);
    ~cons();

    void result();
    };

    cons::cons(int i)
    {
    num = i;
    }

    void cons::result()
    {
    int arr[num];
    int i, total = 0;
    cout << "Enter your array element --: ";
    for (i = 0; i < num; i++)
    cin >> arr[i];

    for (i = 0; i < num; i++)
    total += arr[i];

    cout << "\nThe total of the array --: " << total << "\n";
    }

    cons::~cons()
    {
    cout << "[+]MEMORY RELEASED[+]";
    }

    int main()
    {
    int i;
    cout << "Enter how many element in your array --: ";
    cin >> i;

    cons obj(i);
    obj.result();

    return 0;
    }

    ReplyDelete