Second array not printing - java

When I run my code in my compiler, the second array is not printing for some reason even though it is essentially the same code for creating the first array copy and pasted. The first array does print.
Can someone tell me why the second array is not printing?
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n;
int i = 0;
int count = 0;
int x;
int d = 0;
int count2 = 0;
System.out.println("Enter a base ten number between 0 and 255, inclusive.");
n = s.nextInt();
System.out.println("Enter a base ten number between 0 and 255, inclusive.");
x = s.nextInt();
int[] bin = new int[8];
int[] bin2 = new int[8];
while (count < 8) {
bin[i] = n % 2;
i++;
n = n / 2;
count++;
}
System.out.print("First binary number: ");
for (int j = i - 1; j >= 0; j--) {
System.out.print(bin[j] + " ");
}
while (count2 < 8) {
bin2[d] = x % 2;
d++;
x = x / 2;
count2++;
}
System.out.print("\n\nSecond binary number: ");
for (int z = x - 1; z >= 0; z--) {
System.out.print(bin2[z] + " ");
}
}

Change your loop from
for (int z = x - 1; z >= 0; z--) {
To
for (int z = d - 1; z >= 0; z--) {
As in your earlier loop, x might have become 0 and you are trying to do like:
z = -1 and z >=0
in your for loop, which is why it doesn't enter the for loop.

In the first loop, you are counting down from i to zero. In the second loop, the equivalent of i is d.
But instead of counting down from d, you are counting down from x, which gives you the incorrect result. So change that do:
for (int z = d - 1; z >= 0; z--) {
System.out.print(bin2[z] + " ");
}

Change this
System.out.print("\n\nSecond binary number: ");
for (int z = x - 1; z >= 0; z--) {
System.out.print(bin2[z] + " ");
}
To
System.out.print("\n\nSecond binary number: ");
for (int z = d - 1; z >= 0; z--) {
System.out.print(bin2[z] + " ");
}

Related

Draw pattern in Java using numbers and (*)

I'm trying to write a program using Java, that (outputs) the following pattern depending on an input (integer) (n = 5):
0********1
23******45
678****901
2345678901
As you noticed:
input(3) represent 3 rows
single row digits (n * 2)
Digits should start from 0 to 9 and then repeat until the pattern is fully done
First row should have only 2 numbers (start 0 end 1)
(*) will be in between
Next row should have 4 numbers (start 23 end 45) and so on
How can this program written?
Here is my code:
import java.util.Scanner;
public class b_test_2 {
public static void main (String arug[]) {
String star = "*";
int star_count, digit = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Please type a number (int)");
int n = sc.nextInt();
while (n != 0){
star_count = n * 2 - 2;
for (int i=0; i<n; i++) {
System.out.print(star);
i = i + 1;
}
String stars = star;
n = n - 1;
for (int i2=0; i2<n; i2++) {
System.out.print(star);
i2 = i2 + 1;
int x = 0;
x = digit;
x = x + 1;
if (x == 10){
x = 0;
System.out.print(digit + stars + digit);
}
}
}
}
}
There are any parts missing in your code, but you also seem to make it more complicated than it is.
To illustrate, and hopefully help you to go in the right direction, here is compact code to do it. Do not hand in this code unless you fully understand how it works.
static void printPattern(int n) {
for (int row = 1, digit = 0; row <= n; row++) {
for (int i = 0; i < row; i++, digit = (digit + 1) % 10)
System.out.print(digit);
for (int i = (n - row) * 2; i > 0; i--)
System.out.print('*');
for (int i = 0; i < row; i++, digit = (digit + 1) % 10)
System.out.print(digit);
System.out.println();
}
}
Test
printPattern(4);
Output
0******1
23****45
678**901
23456789
I case you haven't learned it yet, the % operator calculates the remainder after division.

How to quit scanner when input is negative?

This is the instructions.
Write a program that reads a sequence of input values and displays a bar chart of the values using asterisks. You may assume that all values are positive. First figure out the maximum value. That's value's bar should be drawn with 40 asterisks. Shorter bars should use proportionally fewer asterisks.
This is what I came up so far. It's all good except I need to enter a letter instead of a negative number to quit scanning. I have tried (if( < 0) things) but those didn't work.
import java.util.Scanner;
public class BarChart1 {
public static void main(String [] args) {
int[] arr = new int[100];
int currentSize = 0;
System.out.println("Enter a sequence of positive integers. "
+ ("Enter a negative value to quit:"));
Scanner in = new Scanner(System.in);
while(in.hasNextInt()) {
int num = in.nextInt();
if (num < 0) {
break;
}
else {
arr[currentSize] = in.nextInt();
currentSize++;
}
}
//will find the max
double max = arr[0];
int y = 0;
for (int i = 1; i < arr.length; i++) {
y = i + 1;
if(max < arr[i]) {
max = arr[i];
//y = i + 1;
}
}
System.out.println("Max number is: " + max);
System.out.println("Number of digits = " + y);
System.out.println(Math.abs(-1));
double scale = 40/max;
System.out.println("Scale = " + scale);
for (int i = 0; i < y; i++) {
double h = scale * arr[i];
if (h != 0) {
for (int j = 1; j <= h; j ++) {
System.out.print("*");
}
System.out.println();
}
}
}
}
This is the result.
1
2
3
4
-1
Max number is: 4.0
Number of digits = 100
Scale = 10.0
********************
****************************************
I only need the asterisks. Everything else that is being printed is just for checking purposes.
You can try this:
while(in.hasNextInt()) {
int num =in.nextInt();
if(num <0){
break;
}
else{
arr[currentSize] = num;
currentSize++;
}
}

3 Dimensional Tic Tac Toe prints only one dimension

I'm starting on a program to make a 3D tic tac toe game, and am running into a few problems. The game is supposed to be 4x4x4. I'm not sure why the multiple dimensions are not being printed, and I'm also not sure why my entered value isn't appearing on the one level that shows. Code below. Any help would be awesome. Thanks!
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int draw = 64;
int n = 0;
int board[][][] = new int[4][4][4];
while (n < draw) {
System.out.println("Type your move as one three digit number(lrc)");
int input = scan.nextInt();
int level = input / 100;
int row = input % 100 / 10;
int column = input % 10;
System.out.println(level);
System.out.println(row);
System.out.println(column);
board[level][row][column] = 1;
for (int i = 3; i >= 0; i--) { //level
for (int h = 3; h >= 0; h--) { //row
for (int temp = h; temp >= 0; temp--) {
System.out.print(" ");
}
System.out.print(i + "" + h + " ");
for (int j = 0; j <= 3; j++) { //column
if (board[i][h][j] == 0) {
System.out.print("_ ");
}
if (board[i][h][j] == 1) {
System.out.print("X ");
n++;
}
if (board[i][h][j] == 5) {
System.out.print("O ");
n++;
}
}
System.out.println();
}
System.out.println();
}
System.out.println("\n 0 1 2 3");
}
}
}
In this line
for (int temp = h; h > 0; h--) {
you are decrementing h rather than temp so in the loops below the value of h will always be 1
After this change
change for (int temp = h; temp >= 0; temp--) {
System.out.println(" ");
}
to simply System.out.println(" ");

Brute Force Algorithm Issue

I've look over the code several time but I keep getting a Array Out of Bound at the line that states:
sum = sum + vectorArray[z]; }
Can anyone see what's wrong?
public class HW1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// The numbers for n are not relevant
System.out.println("Please enter a number for the length of n.");
int n = input.nextInt();
// Creates an array with n values
int[] vectorArray = new int[n];
// Inputs random numbers into the array ranging from -100 to 100.
int dummy;
int temp = 0;
// Loop to generate negative and positive numbers into the array
for (int i = 0; i < n; i++) {
dummy = (int)(Math.random()*2);
if (dummy == 0) {
temp = -1;
} else {
temp = 1; }
vectorArray[i] = ((int)(Math.random()*101)) * temp;
System.out.println(vectorArray[i]); }
int max = -1;
int sum;
for (int x = 0; x < vectorArray.length; x++) {
for (int y = x; x < vectorArray.length; y++) {
sum = 0;
for (int z = x; z <= y; z++) {
sum = sum + vectorArray[z]; }
max = Math.max(max, sum);
} }
System.out.println("The max: " + max);
}
}
change x to y in the for loop
for (int y = x; y < vectorArray.length; y++) {
^
sum = 0;
for (int z = x; z <= y; z++) {
sum = sum + vectorArray[z]; }

Lagrange interpolation in JAVA

I have done a search around, but there isnt any code available in java hence I have write my own and I have encountered some issue. I actually got this code from a c++ source and trying hard to convert it into a workable java program.
http://ganeshtiwaridotcomdotnp.blogspot.sg/2009/12/c-c-code-lagranges-interpolation.html
public static void main(String[] args) {
int n;
int i, j;
int a;
int x[] = null;
int f[] = null;
int sum = 0;
int mult;
Scanner input = new Scanner(System.in);
System.out.println("Enter number of point: ");
n = input.nextInt();
System.out.println("Enter value x for calculation: ");
a = input.nextInt();
for (i = 0; i < n; i++) {
System.out.println("Enter all values of x and corresponding functional vale: ");
x = input.nextInt();
f = input.nextInt();
}
for (i = 0; i <= n - 1; i++) {
mult = 1;
for (j = 0; j <= n - 1; j++) {
if (j != i) {
mult *= (a - x[j]) / (x[i] - x[j]);
}
sum += mult * f[i];
}
}
System.out.println("The estimated value of f(x)= " + sum);
}
For Lagrange Interpolation Formula -
You should use double data type Array
Your should make Arrays with specific number of items
Look at the program below, you will understand.
double product, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number of Terms: ");
int n = sc.nextInt();
double[] x = new double[n];
double[] y = new double[n];
System.out.println("Enter all the x, y terms: ");
for (int i = 0; i < n; i++) {
x[i] = sc.nextDouble();
y[i] = sc.nextDouble();
}
System.out.println("x = {" + Arrays.toString(x) + "}");
System.out.println("x = {" + Arrays.toString(y) + "}");
System.out.print("Enter a point to Find it's value: ");
int xPoint = sc.nextInt();
// End of inputs
product = 1;
// Peforming Arithmatic Operation
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
product *= (xPoint - x[j]) / (x[i] - x[j]);
}
}
sum += product * y[i];
product = 1; // Must set to 1
}
System.out.println("The value at point " + xPoint + " is : " + sum);
// End of the Program
}
}
int x[] = null;
int f[] = null;
...
for (i = 0; i < n; i++) {
....
x = input.nextInt();
f = input.nextInt();
}
Looks clear enough. Simply create array instance for x and f:
x = new int[n];
f = new int[n];
Somewhere after:
n = input.nextInt();
Before above for loop, and modify the for body:
...
x[i] = input.nextInt();
f[i] = input.nextInt();
public static void main(String[] args) {
System.out.println("Langrages");
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of terms: ");
int n = sc.nextInt();
float[] ax = new float[n];
System.out.println("Enter values of x");
for(int i=0 ;i<n;i++) {
ax[i] = sc.nextFloat();
}
float[] ay = new float[n];
System.out.println("Enter values of y");
for(int i=0 ;i<n;i++) {
ay[i] = sc.nextFloat();
}
System.out.println("Enter the value of x\n at which you find y");
int x = sc.nextInt();
float num,den;
float term =0;
for(int i=0;i<n;i++) {
num=1;
den=1;
for(int j=0;j<n;j++) {
if(j!=i) {
num=num*(x-ax[j]);
den = den*(ax[i]-ax[j]);
}
}
term += (num/den)*ay[i];
}
System.out.println(term);
sc.close();
}

Categories

Resources