2

I wrote this program that prints a table that shows the powers of x, now i have to modify it so that it prints the sum of each row, after each row and i have no idea where to really start. i thought about putting the numbers into an array and adding them like that but that seems like its way too overly complicated.

public class table {
    public static void main(String[] args)
    {
    final int NMAX = 5;
    final double XMAX = 10;

    for(int n = 1; n <= NMAX; n++) 
    {
        System.out.printf("%10d", n);
    }
    System.out.println();
    for(int n = 1; n <= NMAX; n++)
    {
        System.out.printf("%10s", "x ");
    }
    System.out.println();
    for(double x = 1; x <= XMAX; x++)
    {
        for (int n = 1; n <= NMAX; n++)
        {
            System.out.printf("%10.0f", Math.pow(x, n));
        }
    System.out.println();
    }
}

}

Steve
  • 49
  • 11
  • Does putting them into an array work? – Jacob B. Apr 12 '18 at 20:39
  • A different, refactoring suggestion -- always separate calculations from presentation. – KevinO Apr 12 '18 at 20:41
  • i want to use an int variable for summation, i can add them and put the sums at the end of the program but i need to make an extra column on the end that has the sums of each row from top to bottom. i think i need to change something inside the last loop that makes it print an extra column with all the sums im just not sure how to do that. – Steve Apr 12 '18 at 20:43
  • Please format (indent) code correctly. – Andreas Apr 12 '18 at 20:47
  • You build one row at a time, and you just need a single `sum` value, so why would you need an array? Set `sum = 0` before the loop printing row values, add value to `sum` as you print them, then print sum after loop before the `println()`. – Andreas Apr 12 '18 at 20:50

2 Answers2

3

Okey i think that i understand you. Here is the code i made some changes but nothing too complex for understanding. If you have some questions feel free to ask.

    final int NMAX = 6;
    final double XMAX = 10;

    for(int n = 1; n <= NMAX; n++)
    {
        System.out.printf("%10d", n);
    }
    System.out.println();
    for(int n = 1; n <= NMAX; n++)
    {
        System.out.printf("%10s", "x ");
    }
    System.out.println();
    int counter = 0;
    for(double x = 1; x <= XMAX; x++)
    {
        for (int n = 1; n <= NMAX; n++)
        {
            if(n == 6){
                System.out.printf("%10d",counter);
                counter = 0;
            }else{
                System.out.printf("%10.0f", Math.pow(x, n));
                counter += Math.pow(x, n);

            }
        }
        System.out.println();
    }
Milos
  • 394
  • 2
  • 19
  • @Steve i have edited the printing stuff so now it is working perfect. Do you understand what i did in the code? – Milos Apr 12 '18 at 21:09
  • I get what this code is supposed to do but its not working for me and i cant figure out why, i used 5 instead of 6 because the original value was actually supposed to be 4 but i just get a blank column at the end labelled 5 and i cant figure out why it isnt printing the values. – Steve Apr 12 '18 at 21:10
  • oh wait a minute i didnt see the edit before i posted that comment – Steve Apr 12 '18 at 21:10
  • oh that %10d was why it wasnt printing right, thanks alot my program works now. – Steve Apr 12 '18 at 21:12
  • @Steve Am i didnt edit too much just this line System.out.printf("%10d",counter); instead of System.out.printf(" ",counter); – Milos Apr 12 '18 at 21:13
  • @Steve I am glad to hear that, if you find answer as useful just mark the answer as helpful. – Milos Apr 12 '18 at 21:15
2

You could achieve what you want changing the last part of your program like this (if NMAX is always the number of columns):

double pow, sum;
for(double x = 1; x <= XMAX; x++)
    {
        sum = 0;
        for (int n = 1; n < NMAX; n++)
        {
            sum += (pow = Math.pow(x, n));
            System.out.printf("%10.0f", pow);
        }
        System.out.printf("%10.0f", sum);
        System.out.println();
    }

This line may be confusing:

sum += (pow = Math.pow(x, n));

As explained here, the assignment operator returns the assigned value. In this case, we're assigning the result from Math.pow(x, n) to the variable pow and then add that same value to the variable sum.

This way you only call Math.pow once, since you're saving the value in a variable to print it later.

Doing it this way allows you to change the variable NMAX to any value without needing to change anything else in your program.

IvanGrasp
  • 118
  • 10