Alright so, I'm new to nested for loops adn I'm having a bit of an issue on understanding them. I've read many guides, but I still don't fully understand.
Alright the prompt:
Write nested for loops that produce the following output:
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
What I have so far
for(int num2 = 0; num2 <= 9; num2++) {
for(int num1 = 0; num1 <= 2; num1++) {
System.out.println(num2 + " " + num2 + " " + num2);
}
}
And the output is
0 0 0
0 0 0
0 0 0
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
4 4 4
4 4 4
4 4 4
5 5 5
5 5 5
5 5 5
6 6 6
6 6 6
6 6 6
7 7 7
7 7 7
7 7 7
8 8 8
8 8 8
8 8 8
9 9 9
9 9 9
9 9 9
What am I doing wrong?
You got 3 copies of each number.
the outer loop:
for (int i = 0; i < 10; i++) {
chooses which number you want to print so that is fine.
The inner loop however is comparing j against the chosen number. You want 3 copies, not a variable number of copies. This change will make 3 copies:
for (int j = 0; j < 3; j++) {
You also don't need this:
System.out.println(i);
EDIT: I just noticed you need 3 of these outputs.
add an outer loop:
for (int x = 0; x < 3; x++) {
and a blank space
System.out.println(" ");
So the final result should be:
for (int x = 0; x < 3; j++) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(i);
}
}
System.out.println(" ");
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(j + "" + j + "" + j);
}
System.out.println();
}
In the program provided by you the following events take place:-
In first loop variable i is initiated, condition for loop is checked and then it moves to the second loop if condition is true.
Now second loop iterates over the value of j until the condition is false and then control returns to the first loop.
Try to follow the working of loop and you can see yourself where were you wrong.
Try this:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 3; k++) {
System.out.print(j);
}
}
System.out.println("");
}
loop 1: You want the sequence 3 times, each occurrence on its own line.
loop 2: You want the sequence to have digits 0 through 9 ascendingly.
loop 3: You want the sequence to have each digit in succession 3 times.
for (int k = 0; k<3, k++){
for (int i = 0; i< 10; i++) {
for (int j = 0; j < 3;j++) {
System.out.println(i);
}
}
System.out.println("")};
}
}
for(int k=0;k<3;k++) {
for (int i = 0; i< 10; i++) {
for (int j = 0; j < 3;j++) {
System.out.println(i);
}
}
}
Although I am by far most inexperienced guy here, I think this should give exact output you're looking for.
Related
I wanna ask you something about my code. I want to display output as below:
2 3 4 1
6 7 8 5
10 11 12 9
14 15 16 13
But the output what is shown:
2 3 4 1
6 3 0 -3
2 -1 -4 -7
-2 -5 -8-11
Here is my current code:
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
System.out.printf("%3d", number);
if(j==2){
plus=-3;
}
number+=plus;
}
number+=8;
System.out.println("");
}
Can you tell me what's wrong with it? Thank you
You need to reset plus to 1 at the end of each iteration of the outer loop. See the below code in action here.
int number = 2;
int plus = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.printf("%3d", number);
if (j == 2) {
plus = -3;
}
number += plus;
}
number += 8;
plus = 1;
System.out.println("");
}
I know this is extremely simple, but I need help with printing a code that displays
1
2 3
4 5 6
7 8 9 10
For some reason, my brain can't figure out how to do it, even though I am familiar with for loops, this is what I have so far.
for(int i = 1; i <= 4; ++i)
{
for(int j = 1; j <= i; ++j)
{
System.out.print(j);
}
System.out.println(" ");
}
I don't know what to do from here, it's been bugging me all day. Thank you!
int counter = 1;
for (int i = 1; i <= 4; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print(counter++);
System.out.print(" ");
}
System.out.println();
}
Use a counter variable to reach 10. Also move the space in the inner loop and add a line break in the outer loop.
Output:
1
2 3
4 5 6
7 8 9 10
Try this:
int i=0,j=0,n=4,k=1;
for(i=1; i<n+1; i++)
{
for(j=0; j<i; j++)
System.out.print(" "+k++);
System.out.println(" ");
}
I've been struggling with this for loop pattern in Java(with spaces on both sides)
0
000
00000
0000000
00000
000
0
Heres what I have been doing:
for (int i = 1; i <= 5; i++) {
for (int s = 5; s > i; s--) {
System.out.print(" ");
}
for (int j = 1; j < i; j++) {
System.out.print("0");
}
for (int j = 1; j < i; j++) {
System.out.print("0");
}
System.out.println("");
}
for (int i = 1; i <= 5; i++) {
for (int s = 1; s < i; s++) {
System.out.print(" ");
}
for (int j = 5; j > i; j--) {
System.out.print("0");
}
for (int j = 5; j > i; j--) {
System.out.print("0");
}
System.out.println("");
}
As you can probably tell, I've been able to figure out how to print this pattern using an odd number of 0's in each row, but I can't for the life of me figure out how to do it with odd numbers. Thanks.
Sometimes it helps to parameterize the problem a bit; then you can write it down on paper and see if there are any patterns. For example, in your desired output:
0
000
00000
0000000
00000
000
0
First, let's pick a convenient way to describe a row. Let's say a row i is s spaces followed by z zeros, and the whole pattern can be rows rows long. Now write it out. When rows is 7:
i s z
0 3 1
1 2 3
2 1 5
3 0 7
4 1 5
5 2 3
6 3 1
How about if rows is, say, 5:
i s z
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
What about even numbers? We have to pick how we want it to look, how about:
00
0000
000000
000000
0000
00
So, when rows is 6:
i s z
0 2 2
1 1 4
2 0 6
3 0 6
4 1 4
5 2 2
Ok, so now, let's look for patterns! z is easy: We can see, both from the numbers and visually that z is a function of s and rows:
z = rows - (2 * s);
That is, we know the total width is equal to the height (rows), and we know there are the same number of spaces before and after the zeroes.
So now it boils down to figuring out s from i and rows. Well, after a bit of head scratching and experimenting, we can see that when i < rows / 2:
s = (rows - 1) / 2 - i;
And when i >= rows / 2:
s = i - rows / 2;
And now, it all comes together, e.g.:
void diamond (int rows) {
for (int i = 0; i < rows; ++ i) {
int s;
if (i < rows / 2)
s = (rows - 1) / 2 - i;
else
s = i - rows / 2;
int z = rows - (2 * s);
// print s spaces, then z zeroes, then a newline
}
}
I'll leave the task of printing s spaces and z zeroes as an exercise to the reader.
This is a fairly general problem solving technique for this type of problem:
Parameterize.
Write it down.
Look for patterns.
Step 1 is the most important step, as it defines a way to convert your complex-looking task (drawing a diamond) into a much simpler problem (determining s as a function of i and rows).
you can try this -
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
try this,
public static void main(String []args){
int i = -1;
for (int j = 0; j < 7; j +=2) {
i++;
for (int k = i; k < 3; k++)
System.out.print(" ");
for (int z = 0; z <= j; z++) {
System.out.print(0);
}
System.out.println();
}
i=0;
for(int j=4; j>=0;j-=2){
i++;
for(int k=0;k<i;k++)
System.out.print(" ");
for(int z=0;z<=j ;z++)
System.out.print(0);
System.out.println();
}
}
How about just changing the first j=1 to j=2 and changing the first j=5 to j=4, so that you get one less zero from each of those loops?
I wanted to write a function which should fill a two dimensional array like this:
1 -- 3 -- 5 -- 7 -- 9
3 -- 5 -- 7 -- 9 -- 11
5 -- 7 -- 9 -- 11 -- 13
7 -- 9 -- 11 -- 13 -- 15
9 -- 11 -- 13 -- 15 -- 17
Here's it what I could come up with ... by all my logic it should work, but obviously I made some mistake:
public class Array {
public static void TwoDimFill(int[] [] array, int start, int inc){
array[0] [0] = start;
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){enter code here
array[i][j+1] = array[i][j] + inc;
}
array [i+1][0] = array[0][i+1];
}
}
public static void main(String[] args){
int[] [] b = new int[5] [5];
TwoDimFill(b, 1, 2);
for (int x = 0; x < b.length; x++){
for (int y = 0; y<b[x].length; y++){
TextIO.put(b[x][y]+"\t");
}
TextIO.putln();
}
}
By the way: This TextIO.class is something we use for printing, compareable to system.out ...
Edit: Answered! Thank you a lot guys, you're great!
i+1 and j+1 will be out of bounds because you loop until < length. Change it to < length -1.
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length -1; j++){
array[i][j+1] = array[i][j] + inc;
}
if (i < array.length -1)
array [i+1][0] = array[0][i+1];
}
EDIT: As pointed out on comments, the last output line was incorrect. Variable i can be looped until < length, but it must be verified for the last line that the index won't get out of bounds. Corrected the code above.
There is an excess number of operations you are doing. There is a much cleaner, simpler and faster solution. Sometimes a different approach can solve the problem better.
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array[i].length; j++)
array[i][j]=((i+j)<<1)+1;
You need to check if the index is out of bounds before executing the last line in your loop, as well as only making your inner loop iterate up to length-1 (since indexes for arrays in Java start at 0 and end at 1 less than their length):
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length - 1; j++) {
array[i][j + 1] = array[i][j] + inc;
}
if(i + 1 < array.length) {
array[i + 1][0] = array[0][i + 1];
}
}
This produces:
1 3 5 7 9
3 5 7 9 11
5 7 9 11 13
7 9 11 13 15
9 11 13 15 17
Note that simply clipping the outer loop to array.length-1 doesn't work (since in this case you still want the first part of the loop to execute), you'd end up with the following:
1 3 5 7 9
3 5 7 9 11
5 7 9 11 13
7 9 11 13 15
9 0 0 0 0
Bolding is mine, note the last line is obviously incorrect.
This is wrong because of array[i][j+1]. If j is the last position j+1 doesn't exist. You can try this:
public static void TwoDimFill(int[] [] array, int start, int inc){
int aux = 0;
for(int i = 0; i < array.length; i++) {
aux = start;
for(int j = 0; j < array[i].length; j++) {
array[i][j] = aux;
aux += inc;
}
start += inc;
}
}
You can do this really simply, use the following
public class TestClass {
public static void main(String args[]) {
int[][] temp = new int[5][5];
int[][] output = fill2dim(temp,1,2);
for (int i = 0; i < output.length; i++) {
System.out.println(java.util.Arrays.toString(output[i]));
}
}
public static int[][] fill2dim(int[][] arr, int start, int inc) {
int[][] output = new int[arr.length][arr[0].length];
for (int i = 0;i<arr.length;i++) {
for (int j = i;j<arr[0].length;j++) {
output[i][j] = (i+j)*inc+start;
output[j][i] = (i+j)*inc+start;
}
}
return output;
}
}
You can remove some loops by just considering the upper triangular matrix, since the lower triangular matrix will be a reflection of the upper triangle about the diagonal
I have homework to make a triangle that looks like this:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
I have been able to create almost half the triangle with the following code:
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
I have been unable to figure out how to mirror the other half of the triangle with my code to look like the triangle above. The instructor hinted that using the for loop with the tab return \t is the way to do this.
Like #Maroun's answer but simpler
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i; j < size; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
or
int size = 6;
for (int i = 1; i <= size; i++) {
for (int j = i - size + 1; j <= i; j++)
System.out.print((j > 0 ? j : "") + " ");
System.out.println();
}
prints
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
You can try this one:
int x = 0;
for(int i = 1; i <= 5; i++) {
for(int k = x; k < 4; k++) {
System.out.print(" ");
}
x++; //less spaces will be printed in the next iteration
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
I wrote another loop that begins with 4 spaces and then the amount of spaces is reduced.
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Tip: Always open { for your loops and conditions, even for one operation, this might prevent bugs.