Very simple but why doesn't it work?

1. Jan 12, 2006

bomba923

Very simple...but why doesn't it work!?

The program should output every number of a user-input base, containing a certain quantity of digits.
------------------------------------------
import java.io.*;

public class BaseCount {

. public static void main(String args[]) {

. int base = 0, dgts = 0;

. int[] bsq;

. try {
. System.out.println("Enter a natural base:");

. System.out.println("Enter the maximum number of digits:");
. }//try
. catch(IOException e) {}

. bsq = new int[dgts];

. for(int i=0; i<Math.pow(base,dgts); i++) {

. for(int k=0; k<dgts; k++) {
. bsq[k] = i / (int) Math.pow(base,k) % base;
. System.out.print(bsq[dgts-k-1]);
. }//for

. System.out.println();

. }//for

. }//main

}//BaseCount
---------------------------------------
However, there seem to be problems!

For example, let base = 2 and dtgs = 4.
The program outputs:

Code (Text):

Enter a natural base:
2
Enter the maximum number of digits:
4
0000
0001
0010
0011
0000
0101
0110
0111
0100
1001
1010
1011
1000
1101
1110
1111

The program does not output 0100 when i = 4.
Even more, the program outputs 0100 when it should output 1000.

Why is this so??

Last edited: Jan 12, 2006
2. Jan 12, 2006

chroot

Staff Emeritus
Look at these two lines:

PHP:

bsq[k] = i / (int) Math.pow(base,k) % base;
System.out.print(bsq[dgts-k-1]);

You're setting element k of the array, and then printing a different element. In other words, you're printing data that you calculated on the previous run, which is not at all what you intended.

Instead, why don't you just reverse the way the for(k) loop runs? Use for(k = dgts-1; k >= 0; --k), and just print each value immediately after it's calculated. In fact, you don't even need to store it in an array.

- Warren

3. Jan 12, 2006

bomba923

Thanks!

Regarding the array--well, I intend to remove the System.out.println statements out of the 'for' loop and add (to this program) a method modifying a copy of the array...which, upon satisfying a certain condition, will be printed out.
Actually, this is a simpler version of another program containing the same faulty 'for' loop...which I'll now fix :shy:

I was writing a program that will calculate (via brute force) $\{a_0,a_1,a_2,\ldots,a_n\}$ with user-input values of p,q, and r (of course I'll name the variables in java differently). The goal is to calculate $\{a_0,a_1,a_2,\ldots,a_n\}$ according to my statement in this this thread, assuming it is true. So far, it's been over a month and no proof/disproof has developed for that statement (also I'm quite busy and rarely have time for a genuine investigation ).

Here is the larger program, with the for-loop problem corrected:
---------------------------------------------------------------
import java.io.*;

public class TestMath1 {

public static void main(String args[]) {

int nmax = 0;

double num = 0, den = 0,
decm = 0, sum = 0;

double[] bsq;

try {
System.out.println("Enter the numerator of the base:");

System.out.println("Enter the denominator of the base:");

System.out.println("Enter any natural number:");
}//try
catch(IOException e) {}

nmax = (int) (Math.log(decm*den)/Math.log(num/den));
bsq = new double[nmax+1];

for(int i=0; i<Math.pow(num,nmax+1); i++) {

for(int k=0; k<=nmax; k++) {
bsq[k] = i / (int) Math.pow(num,k) % num;
sum += bsq[k]/den * Math.pow(num/den, k);
}//for

if(sum == decm/den) {

for(int k=nmax; k>=0; --k) {
System.out.print(bsq[k]/den + " ");
}//for

System.out.println("\n\nWhich represents:\n");

for(int k=nmax; k>=0; --k) {
System.out.print((int)bsq[k] + "/" + (int)den + " ");
}//for

System.exit(0);

}//if

sum = 0;

}//for

}//main

}//TestMath1
---------------------------------------------------------------
Indeed, I shouldn't assign values in loops. Also, I will find a better value with which to initialize i...because the program consumes much more time with larger input values.

Before you input values, remember that num > den and that num, den, and decm must be natural.

Last edited: Jan 13, 2006