Very simple but why doesn't it work?

  • Thread starter bomba923
  • Start date
  • Tags
    Work
In summary, the program should output every number of a user-input base, containing a certain quantity of digits, but it does not work for certain inputs. The problem is that the for-loop is not running in the order that it should, and this is causing incorrect values to be output.
  • #1
bomba923
763
0
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;

. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

. try {
. System.out.println("Enter a natural base:");
. base = Integer.parseInt(br.readLine ());

. System.out.println("Enter the maximum number of digits:");
. dgts = Integer.parseInt(br.readLine ());
. }//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! :frown:

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

Code:
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

:bugeye: 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:
Technology news on Phys.org
  • #2
bomba923 said:
Why is this so??

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
:smile: 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) [itex]\{a_0,a_1,a_2,\ldots,a_n\}[/itex] with user-input values of p,q, and r (of course I'll name the variables in java differently). The goal is to calculate [itex]\{a_0,a_1,a_2,\ldots,a_n\}[/itex] 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 :rolleyes: (also I'm quite busy and rarely have time for a genuine investigation :frown:).

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;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

try {
System.out.println("Enter the numerator of the base:");
num = Integer.parseInt(br.readLine ());

System.out.println("Enter the denominator of the base:");
den = Integer.parseInt(br.readLine ());

System.out.println("Enter any natural number:");
decm = Integer.parseInt(br.readLine ());
}//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:

1. Why is it important to understand why something doesn't work?

Understanding why something doesn't work is crucial for any scientist because it allows them to identify and correct any errors or flaws in their experiments or research. It also helps them to improve their methods and results in future studies.

2. What are some common reasons for something to not work?

There are several reasons why something may not work, including incorrect experimental design, faulty equipment or materials, insufficient data or sample size, or human error. It is essential to carefully analyze and troubleshoot each step of the process to determine the cause.

3. How can I troubleshoot when something doesn't work?

The first step in troubleshooting is to carefully review your experimental design and make sure all variables are controlled. Then, check your equipment and materials for any defects or malfunctions. You can also try repeating the experiment or changing one variable at a time to identify the issue.

4. Can simple experiments also fail?

Yes, even simple experiments can fail. The simplicity of an experiment does not guarantee its success. It is essential to carefully plan and execute any experiment, regardless of its complexity, to ensure accurate results.

5. What can I learn from a failed experiment?

A failed experiment can provide valuable insights and teach you important lessons. It can help you identify weaknesses in your experimental design, equipment, or methods. It can also lead to new hypotheses and further research. Embracing failure and learning from it is an essential part of the scientific process.

Similar threads

  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top