Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7872037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:05:26+00:00 2026-06-03T02:05:26+00:00

i am trying to get the sum of even numbers from a recursive array

  • 0

i am trying to get the sum of even numbers from a recursive array and the sum of an odd number and then add both sums together. i am trying to loop through the arrays so that to get the even numbers but it keep return the first index. please help and thanks in advance..

my aim is to take in array 2,1,5,9,8,4 take the even index and add it to the odd indexes. a[0]=2,a[1]=1,a[2]=5, a[3]=9, a[4]=8, a[5]=4. so it would take (2+5+8)-(1+9+4)=1

this is what i got so far i am not familiar with recursive so my code might be off

int calc(int *a, int size)
{
    if(size==1 || size==0)
        return a[0];
    for(int i=0; i<size; i++){
        if(i%2==0){
            int sum_i = a[i];
            int m=calc(a, size-1);
            if(m>a[size-1])
                return m;
        }
    }
    for(int j=0; j<size; j++){
        if(j%2!=0);
        int sum_j = a[j];
        return sum_j;
    }
    int sum = a[i] - a[j];
    int e = calc(a, size-1);
    if(e%2==0)
        return e=e+0; //return even

    return sum;
}

int main( )
{
    int a[6]={1,2,3,5,6,2};
    int size = 6;   
    cout<< calc(a, size)<<endl;

    system("pause");   
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:05:27+00:00Added an answer on June 3, 2026 at 2:05 am

    You don’t need recursion for this?

    // calc(): returns sum(a[0], a[2], a[4], ...) - sum(a[1], a[3], a[5], ...)
    int calc(int *a, int size)
    {
        int sum_even_pos = 0;
        int sum_odd_pos = 0;
        for (int i = 0; i < size; i++) 
        {
            sum_even_pos += a[i];
            if (++i < size) sum_odd_pos += a[i];
        }
        return sum_even_pos - sum_odd_pos;
    }
    

    http://codepad.org/DVx0sgg1

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the sum of all prime numbers from 0 to 2
I'm trying to sum two columns, and subtract the two sums to get a
Im trying to get the sum of a list from a web service. [WebMethod]
I'm trying to calculate the sum based off of sets of numbers received from
I'm trying to get a sum of a field cost from a page type
I am trying to get the sum hours for each month from a given
I have a problem with this query trying to get a sum and a
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
Trying to get a value out of 2d array inside of a hash and
I am trying to get the total count and total sum per salesperson but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.