Very simple but why doesn't it work?

  • Thread starter Thread starter bomba923
  • Start date Start date
  • Tags Tags
    Work
Click For Summary
SUMMARY

The discussion focuses on a Java program designed to output numbers in a user-defined base with a specified number of digits. The initial implementation contains a logical error in the nested loop structure, causing incorrect outputs, particularly failing to display the correct binary representation. The solution proposed involves reversing the inner loop to print values immediately after calculation, eliminating the need for an array. Additionally, a more complex program is referenced, which also suffers from similar issues but has been corrected in the latest version.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of loops and conditional statements
  • Basic knowledge of number systems and bases
  • Experience with input handling in Java using BufferedReader
NEXT STEPS
  • Review Java loop structures and their implications on variable scope
  • Learn about number base conversions and their applications
  • Explore optimization techniques for Java programs to improve performance
  • Investigate error handling in Java, particularly with user input
USEFUL FOR

Java developers, computer science students, and anyone interested in algorithm optimization and debugging Java applications.

bomba923
Messages
759
Reaction score
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 {

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


. [/color]int base = 0, dgts = 0;

. [/color]int[] bsq;

. [/color]BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

. [/color]System.out.println("Enter the maximum number of digits:");
. [/color]dgts = Integer.parseInt(br.readLine ());
. [/color]}//try
. [/color]catch(IOException e) {}

. [/color]bsq = new int[dgts];

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

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

. [/color]System.out.println();

. [/color]}//for

. [/color]}//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
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
 
: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) \{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 :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:

Similar threads

  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
20K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K