All about programming in GNU/LINUX

C Program to add elements of an array using Divide and Conquer approach

#include<stdio.h>
#include<stdio.h>
int add(int ,int ,int *);
int main()
{
    int *a,n,i,sum,mid;
    printf(“\NEnter the no.of.elements: “);
    scanf(“%d”,&n);
    a=(int *)malloc(n*sizeof(int));
    for(i=0;i<n;i++)
     scanf(“%d”,a+i);
    sum=add(0,n-1,a);
    printf(“\nSum=%d”,sum);
    return 0;

}

int add(int low,int high,int *a)
{
    int mid;
    if(high==low)
     return a[low];
    mid=(low+high)/2;
    return add(low,mid,a)+add(mid+1,high,a);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s