Linear and Binary Summation Question

I am trying to write a program that will output the contents of an array A into a sum hence title.

Here is my code so far...


#include <iostream>
using namespace std;

int LinearSum(int A[], int);
int BinarySum(int A[], int i, int n);

int main()
{
int A[] = {20, 3, 6, 2, 5};
int B[] = {1, 2, 3, 4, 5, 6, 7, 8};


cout << "Linear sum of the array A is " << LinearSum(A,5) << endl << endl;

cout << "Binary sum of the array A is " << BinarySum(A,0,8) << endl;

cout << "Binary sum of the array B is " << BinarySum(A,0,8) << endl << endl;

system("pause");
return 0;
}

int LinearSum(int A[], int n)
{
if(n = 1)
{
return A[0];
}
else
{
return LinearSum(A, n-1) + A[n-1];
}
}

int BinarySum(int A[], int i, int n)
{
if(n = 1)
{
return A[i];
}
else
{
return BinarySum(A, i, ceil(n/2)) + BinarySum(A, i + ceil(n/2), floor(n/2));
}
}
Last edited on
I figured it out!

p.s. this place is dead
Last edited on
Topic archived. No new replies allowed.