CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    440

    Partial Summation Table

    The assignment is:
    -----------------------------------------
    This program should print out a table with k rows and m columns. Declare the variables “k” and “m” as constants. Take k=4 and m=5. Each row of the table contains partial sums of jth power of first n integers (1^j+2^j+3^j……..n^j); 1<=n<=m. Proceed this way to the other rows 1<=j<=k. The table should be computing in the following way:

    1^1+2^1+...+m^1 | 1^1+2^1+...+(m-1)^1 | 1^1+2^1+...+(m-2)^1 | ...... | 1^1
    1^2+2^2+...+m^2 | 1^2+2^2+...+(m-1)^2 | 1^2+2^2+...+(m-2)^2 | ...... | 1^2
    1^3+2^3+...+m^3 | 1^3+2^3+...+(m-1)^3 | 1^3+2^3+...+(m-2)^3 | ...... | 1^3
    .....
    .....
    1^k+2^k+...+m^k | 1^k+2^k+...+(m-1)^k | 1^k+2^k+...+(m-2)^k | ...... | 1^k

    The Output should look like:

    15 10 6 3 1
    55 30 14 5 1
    225 100 36 9 1
    979 354 98 17 1
    -----------------------------------------------

    My code so far:
    Code:
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        const double k = 4, m = 5;
        double j = 1;
       
       
          
        while (j <= k)
        {
            double n;
             
            for (n = 0; n < m ; n++)
            {
            double x = pow(m-n, j);
            cout << x << " ";
    	   
            }
                            
       
         
        cout << endl << endl;   
        j++;
    }
    
        return 0;
    }
    Correction: It appears I can use either while loops or for loop. I am not sure where to go from there. I need to get the first number to be: 1^1 + 2^1...m^1 where the final m will be 5. Then the next number in the row needs to drop down to where m = 5 - 1. Thanks for any help.
    Last edited by Alterah; September 8th, 2009 at 09:04 AM. Reason: updated code

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Partial Summation Table

    Clearly it'll be easier to work from right to left in each row, since then you can just define
    table[j-1][m-1] = 1
    table[j-1][n] = table[j-1][n+1] + pow(n,j)
    There may be an off-by-one error in there, but you get the idea.

  3. #3
    Join Date
    Aug 2009
    Posts
    440

    Re: Partial Summation Table

    I haven't learned about tables yet, but I sort of see where you are going with that.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Partial Summation Table

    The point is to use the previous value to compute the next one. You don't need to actually use an array to store the values, that just makes it easier. And you don't need to do it in that order---you can start by computing the first value in a row, and just subtract off a power for each subsequent number instead if you want to compute them left to right (easier if you're outputting as soon as the value is computed).

  5. #5
    Join Date
    Aug 2009
    Posts
    440

    Re: Partial Summation Table

    Ok, I don't think I can use the previous value the way you describe, however, I was able to get my code to output something close, but I can't really seem to figure out why I can't display it the correct way.

    Code:
    /*
     *  table2_type.cpp
     *
     *
     *  Created by Greg Matthies on 09/07/2009.
     *  E-mail: gregmatthies@hotmail.com
     *  Description: Creates a table based off of partial sums.
     *  Updated on: 09/08/2009
     */
    
    #include <cmath>
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main()
    {
        const double k = 4, m = 5;
        double j;
      
      
         
        for (j = 1; j <= k; j++)
        {
            double n;
            
            for (n = m ; n >= 0 ; n--)
            {
                double sum = 0, l = 0;         
                double x = pow(m-n, j);       
                for (n = m - 1  ; n >= 0 ; n--)
                {
                    sum = sum + pow(m-n,j);
                    cout << sum << " ";
                }
            cout << " ";
        }
                            
            cout << endl << endl;  
       
        }
        while(!kbhit());
        return 0;
    }
    That outputs:

    1 3 6 10 15
    1 5 14 30 55
    1 9 36 100 225
    1 17 98 354 979

    I am at a loss for how to "flip it" I've tried what I thought would work, but changing it so that in the innermost loop n starts at 1 and goes to m, but that yields an infinite loop. Thanks again for help. The assignment is due tomorrow and I am on 4 hours of sleep, so, perhaps that's why I can't figure out some things.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured