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 3278592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:29:54+00:00 2026-05-17T19:29:54+00:00

I have following code to accomplish prefix sum task: #include <iostream> #include<math.h> using namespace

  • 0

I have following code to accomplish prefix sum task:

  #include <iostream>
  #include<math.h>
  using namespace std;

  int Log(int n){
      int count=1;
      while (n!=0){
          n>>=1;
          count++;

      }
      return count;
  }
  int main(){
    int x[16]={39,21,20,50,13,18,2,33,49,39,47,15,30,47,24,1};
    int n=sizeof(x)/sizeof(int );
    for (int i=0;i<=(Log(n)-1);i++){
          for (int j=0;j<=n-1;j++){
              if (j>=(std::powf(2,i))){
                  int t=powf(2,i);
                  x[j]=x[j]+x[j-t];

              }
          }
     }
     for (int i=0;i<n;i++)
          cout<<x[i]<< "  ";

     return 0;
  } 

From this wikipedia page
but i have got wrong result what is wrong? please help

  • 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-05-17T19:29:54+00:00Added an answer on May 17, 2026 at 7:29 pm

    The quickest way to get your algorithm working: Drop the outer for(i...) loop, instead setting i to 0, and use only the inner for (j...) loop.

    int main(){
        ...
        int i=0;
        for (int j=0;j<=n-1;j++){
            if (j>=(powf(2,i))){
                int t=powf(2,i);
                x[j]=x[j]+x[j-t];
            }
        }
        ...
    }
    

    Or equivalently:

    for (int j=0; j<=n-1; j++) {
        if (j>=1)
            x[j] = x[j] + x[j-1];
    }
    

    …which is the intuitive way to do a prefix sum, and also probably the fastest non-parallel algorithm.

    Wikipedia’s algorithm is designed to be run in parallel, such that all of the additions are completely independent of each other. It reads all the values in, adds to them, then writes them back into the array, all in parallel. In your version, when you execute x[j]=x[j]+x[j-t], you’re using the x[j-t] that you just added to, t iterations ago.

    If you really want to reproduce Wikipedia’s algorithm, here’s one way, but be warned it will be much slower than the intuitive way above, unless you are using a parallelizing compiler and a computer with a whole bunch of processors.

    int main() {
        int x[16]={39,21,20,50,13,18,2,33,49,39,47,15,30,47,24,1};
        int y[16];
        int n=sizeof(x)/sizeof(int);
    
        for (int i=0;i<=(Log(n)-1);i++){
            for (int j=0;j<=n-1;j++){
                y[j] = x[j];
                if (j>=(powf(2,i))){
                    int t=powf(2,i);
                    y[j] += x[j-t];
                }
            }
            for (int j=0;j<=n-1;j++){
                x[j] = y[j];
            }
        }
    
        for (int i=0;i<n;i++)
            cout<<x[i]<< "  ";
        cout<<endl;
    }
    

    Side notes: You can use 1<<i instead of powf(2,i), for speed. And as ergosys mentioned, your Log() function needs work; the values it returns are too high, which won’t affect the partial sum’s result in this case, but will make it take longer.

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

Sidebar

Related Questions

No related questions found

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.