Parallel prefix sum - fastest Implementation

后端 未结 3 353
死守一世寂寞
死守一世寂寞 2020-12-09 05:35

I want to implement the parallel prefix sum algorithm using C++. My program should take the input array x[1....N],and it should display the output in the array

3条回答
  •  我在风中等你
    2020-12-09 05:53

    Following piece of code will do the job

    void prfxSum()
    {
        int *x=0;
        int *y=0;
        int sum=0;
        int num=0;
        int i=0;
    
        cout << "Enter the no. of elements in input array : ";
        cin >> num;
    
        x = new int[num];
        y = new int[num];
    
        while(i < num)
        {
            cout << "Enter element " << i+1 << " : ";
            cin >> x[i++];
        }
    
        cout << "Output array :- " << endl;
        i = 0;
        while(i < num)
        {
            sum += x[i];
            y[i] = sum;
            cout << y[i++] << ", ";
        }
    
        delete [] x;
        delete [] y;
    }
    

    Following is the output on execution

    Enter the no. of elements in input array : 8
    Enter element 1 : 1
    Enter element 2 : 2
    Enter element 3 : 3
    Enter element 4 : 4
    Enter element 5 : 5
    Enter element 6 : 6
    Enter element 7 : 7
    Enter element 8 : 8
    Output array :- 
    1, 3, 6, 10, 15, 21, 28, 36
    

    You can avoid the user input of 1000 elements of array x[] by feeding it from file or so.

提交回复
热议问题