Question

I just finished a code to allow a user to input two numbers (a and b) and the program would do the computation of a^b. This code had to be done without using Math.pow method.

I have to save the results of from 1-10 to the power of 1-3 in an array. When I run my code 4 is stored in all. Here is my whole code in the javadoc is the question.

/**
 * a. Declare a two dimensional double array with 10 rows and 3 columns. b.
 * Store the results from the question above in a 2D array. c. Use a nested loop
 * to print this array out and also add up all the array values. d. Print this
 * sum to the screen. 7. Calling public static methods from another class: a.
 * Write as second class called MyTestProgram which has only a main method in
 * it. b. In this main method make use of the toPowerOf method defined in
 * BlueTest2 to calculate 73 (7 cubed or 7*7*7) and write the result to the
 * screen.
 * 
 */

public class BlueTest2 {
    public static void main(String[] args) {
        int result = toPowerOf(20, 5);
        System.out.println("The power of these numbers is: " + result);
        {
            for (int i = 1; i <= 10; i++) {
                for (int j = 1; j <= 3; j++) {
                    int loopResult = toPowerOf(i, j);
                    System.out.println(i + " to the power of " + j + " is: "
                            + loopResult);
                }
            }

        }
        {
            int[][] array3d = new int [10] [3];
         for (int i = 1; i <= array3d.length; i++) 
          {
         for (int j = 1; j <= array3d[0].length; j++) 
          {
        int loopResult = toPowerOf(i, j);
        array3d[i][j] = loopResult;
        System.out.println("The variable here is: " + array3d[i][j]);
                }
            }
        }
    }

    public static int toPowerOf(int a, int b) {
        int t = a;
        int result = a;
        for (int i = 1; i < b; i++) {
            t = t * a;
            result = t;
        }
        return result;
    }
}

My new changes are just to the second part of my main method

       {
        int[][] array3d = new int [10] [3];
        for (int i = 1; i <= array3d.length; i++) 
          {
         for (int j = 1; j <= array3d[0].length; j++) 
           {
            int loopResult = toPowerOf(i, j);
            array3d[i][j] = loopResult;
            System.out.println("The variable here is: " + array3d[i][j]);
           }
           } 
      }
Était-ce utile?

La solution

The problem is that you're calculating a^b in every iteration, and you're initializing a and b as 1. So, it would be better calculating (i+1)^(j+1) per iteration:

for (int i = 0; i < array2d.length; i++) {
    for (int j = 0; j < array2d[0].length; j++) {
            //int a = 1;
            //a++;
            //int b = 1;
            //b++;
            int loopResult = toPowerOf(i+1, j+1);
            array2d[i][j] = loopResult;
            System.out.println("The variable at " + i + " " + j
                    + " is: " + array2d[i][j]);
    }
}

Autres conseils

{
 int a = 1;
 a++;
 int b = 1;
 b++;
 int loopResult = toPowerOf (a, b);
 array2d[i][j] = loopResult;
 System.out.println("The variable at " + i + " " + j + " is: " +array2d[i][j]);
}

You are setting a to 1 and incrementing it and then setting b to 1 and incrementing it every time the loop runs. if a and b need to increment every time the loop does then they need to be initialized outside of the loop.

You are always calculating 2^2 here:

int a = 1;
a++;
int b = 1;
b++;
int loopResult = toPowerOf(a, b);

You want something like what you have above (where you print the results), like this:

for (int i = 0; i < array2d.length; i++) {
    for (int j = 0; j < array2d[0].length; j++) {
        {
            int loopResult = toPowerOf(i, j);
            array2d[i][j] = loopResult;
            System.out.println("The variable at " + i + " " + j
                + " is: " + array2d[i][j]);
        }
    }
}

The problem in your modifications is the escape conditions in the for loops.

Arrays are 0-indexed, so an array of length 5 goes from indexes 0 to 4. You can travel throught the array like this:

for (int j = 0; j < array2d[0].length; j++)

But you are doing this:

for (int j = 1; j <= array2d[0].length; j++)

Going from 1 to 5 in an array that has indexes 0 to 4, so once you arrive at 5 you get an exception.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top