Very simple but why doesn't it work?

  • Thread starter Thread starter bomba923
  • Start date Start date
  • Tags Tags
    Work
AI Thread Summary
The discussion centers on a Java program intended to output numbers in a user-defined base with a specified number of digits, but it encounters issues with incorrect outputs. The main problem arises from the way values are stored and printed in the nested loops, leading to the display of previously calculated values instead of the current ones. A suggested solution is to reverse the inner loop to print values immediately after calculation, eliminating the need for an array. Additionally, the user mentions plans to modify the program to include a method for handling arrays more effectively. The conversation highlights the importance of properly managing variable assignments within loops to ensure accurate results.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top