Issue with Base Changing..
Focus on the code inside the While Loop
Code :
System.out.println("\nThis program is built to enhance your Base Changing skills");
System.out.println("Change the Base 10 number to the Base n number");
System.out.println("Enter the number of equations to work on below:");
int amount = scan.nextInt();
int a = 0;
int correct = 0;
int wrong = 0;
int score = 0;
double start = System.currentTimeMillis();
while(a < amount){
int x = 100 + (int)(Math.random()*899);
int n = 2 + (int)(Math.random()*7);
int last = x % n;
int next1 = (int)((x/n))%n;
int next2 = (int)((x/(n*n)))%n;
int next3 = (int)((x/(n*n*n)))%n;
int next4 = (int)((x/(n*n*n*n)))%n;
int next5 = (int)((x/(n*n*n*n*n)))%n;
int next6 = (int)((x/(n*n*n*n*n*n)))%n;
int next7 = (int)((x/(n*n*n*n*n*n*n)))%n;
int next8 = (int)((x/(n*n*n*n*n*n*n*n)))%n;
int next9 = (int)((x/(n*n*n*n*n*n*n*n*n)))%n;
int first = (int)((x/(n*n*n*n*n*n*n*n*n*n)))%n;
int answer1 = (last*1)+(next1*10)+(next2*100);
int answer2 = (next4*1000)+(next5 *10000)+(next6*100000)+(next7*1000000);
int answer3 = (next8*10000000)+(next9*100000000)+(first*1000000000);
int answer = answer1 + answer2 + answer3;
System.out.println(x + " base 10 to base " + n + " _____");
System.out.println(answer1);
System.out.println(answer2);
System.out.println(answer3);
System.out.println(answer);
System.out.println(last);
System.out.println(next1);
System.out.println(next2);
System.out.println(next3);
System.out.println(next4);
System.out.println(next5);
System.out.println(next6);
System.out.println(next7);
System.out.println(next8);
System.out.println(next9);
System.out.println(first);
int reply = scan.nextInt();
if(reply == answer){
System.out.println("Correct!");
correct++;
score += 5;
}
else{
System.out.println("Wrong!");
wrong++;
score -= 9;
}
amount--;
}
double now = System.currentTimeMillis();
double time = (now-start)/1000;
System.out.println("\nSTATISTICS:");
System.out.println("Correct: " + correct);
System.out.println("Wrong: " + wrong);
System.out.println("Score: " + score);
System.out.println("Time taken: " + time + " seconds");
System.out.println("Time per Question: " + time/(wrong + correct) + " seconds");
I've made this (admittedly poorly done) algorithm to change from Base 10 to Base n. However, once I get around the 1,000's place. Some issues begin to arise. I printed out all the values for the variables... say I get:
last = 1
next1 = 2
next2 = 2
next3 = 1
The answer will give 221, instead of 1221. Any help please? (Also, sometimes it works perfectly fine. Usually anything past 3 digits will result in a "false" answer"
Re: Issue with Base Changing..
Can you show what is on the console for when the code works and when the code fails?
Add comments to the output describing what is wrong and what you\'d like the output to be.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select \'Select All\' - The selection will show
Click in upper left again
Select Edit and click \'Copy\'
Paste here.
Re: Issue with Base Changing..
Quote:
Originally Posted by
Norm
Can you show what is on the console for when the code works and when the code fails?
Add comments to the output describing what is wrong and what you'd like the output to be.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
This program is built to enhance your Base Changing skills
Change the Base 10 number to the Base n number
Enter the number of equations to work on below:
1
859 base 10 to base 4 _____
123 (answer1)
3000 (answer2)
0 (answer 3)
3123 (answer)
3 (ones place)
2 (tens place)
1 (hundreds place)
1 (thousands place)
3 (ten thousands place)
0 (hundred thousands place)
0 (etc.. etc.. etc..)
0
0
0
0
The answer should be 31123, not 3123..
Re: Issue with Base Changing..
What code produces the output you have posted? I don't see the any of the (...) Strings in your code?
You have over a dozen println statements that print numbers without and ids on them.
Can you change them like this to show what is being printed:
System.out.println("last=" + last);
I don't understand what the user is to enter here:
int reply = scan.nextInt();
Can you explain your algorithm for generating a String representation for a given integer value?
For example for the value ten: "10" base 10, "11" base 9, "12" base 8, ... or "1010" base 2
Re: Issue with Base Changing..
Quote:
Originally Posted by
Norm
What code produces the output you have posted? I don't see the any of the (...) Strings in your code?
You have over a dozen println statements that print numbers without and ids on them.
Can you change them like this to show what is being printed:
System.out.println("last=" + last);
I don't understand what the user is to enter here:
int reply = scan.nextInt();
Can you explain your algorithm for generating a String representation for a given integer value?
For example for the value ten: "10" base 10, "11" base 9, "12" base 8, ... or "1010" base 2
This program is built to enhance your Base Changing skills
Change the Base 10 number to the Base n number
Enter the number of equations to work on below:
1
226 base 10 to base 5 _____
answer1 = 401
answer2 = 0
answer3 = 0
answer = 401
last = 1
next1 = 0
next2 = 4
next3 = 1
next4 = 0
next5 = 0
next6 = 0
next7 = 0
next8 = 0
next9 = 0
first = 0
This one's answer should be 1401...
For int reply = scan.nextInt(); ...... This takes the user's answer as an integer called reply (It works, don't question it please. I have several other functions that work fine)
For my algorithm... I have an integer x that has a range from 100-999. I have another integer n that ranges from 2-9. x = the Base 10 value. And n is the "Base n" to convert to. The way my algorithm works is so:
In order to change from Base 10 to a specific Base n.. You must do these steps:
Say we have "132 Base 10", and we wish to convert it to Base 4...
We must divide 132 by 4 which comes out to be 33 remainder 0. We record the 0. This 0 will be the ones place.
We then take the remaining integer 33, and divide that by 4. We get 8 remainder 1. Record the 1 for tens place.
Then take the 8 and divide by 4. We get 2 remainder 0. Record the 0 for the hundreds place.
Finally take the 2 and divide it by 4. We got 0 remainder 2. Record the 2 for the thousands place.
Overall, our answer should be 2010 Base 4. That's how the conversion works. In order to work for all bases 2-9.. I had to make 10 different place values (last, next1, next2, next3, next4, ..., first). You can see that I made every place value by doing so... last*1, next1*10, next2*100, next3*1000... And I JUST FOUND MY MISTAKE. Incredible. My issue lied with not including "next3 * 1000 within my algorithm. I left it out. Thanks for being around once again to help Norm.
Re: Issue with Base Changing..
Your algorithm description sounds like what you would do in a loop:
Dividing by a number and concatenating the remainder to a String to build the results.
Continue until all divisions are done.
Your current code is a mess.
Re: Issue with Base Changing..
What Norm is saying is you need to do something on the lines of
Code java:
int remainder;
long next;
String result = "", newLine = System.getProperty("line.separator"); //Not sure if you need this...
while(next > 0) //I don't know o.O
{
remainder = next%n;
next = next/n;
result = remainder+result; //Put the remainder in front of the String
}
System.out.println(result);