Home > Software design >  How can i do a nested loop with 5 repetitions, but the starting number (initialization) contradicts
How can i do a nested loop with 5 repetitions, but the starting number (initialization) contradicts

Time:04-05

so here is the code the problem is with the 2nd nested loop where it involves numbers also the 2ND NESTED LOOP HAS A DECREMENT OF -3 EVERY REPETITION IT MAKE

`// b is the repetition and cn is the starting point which it means it falsify this 14 <= 5 which is wrong

String name = "Hotdogs" int b = 5, cn = 14 char k = name.charAt(0);

           //start of the 1st loop
        for (int i = 1; i <= b; i  ) {
            for (int p = i; p <= b; p  ) {
        System.out.print(k  " ");
        }
        System.out.println();
           //end of the 1st loop

            //start of the 2nd loop
        for (int a = cn; a <= b; a= a-3) {
            for (int z = cn; z <= a; z  ) {
        System.out.print(cn  " ");
        }
        System.out.println();
            //end of the 2nd loop`

so im expecting the 2nd nested loop is to do a half pyramid pattern which is this: the one with letters is the 1st nested loop and the one with number is the 2nd nested loop HERE IS THE EXPECTED OUTPUT

CodePudding user response:

We can solve this quite easily by modifying how the loops work. This looks like a homework question, so rather than just copying the code below I encourage you to read through the official tutorial to understand how it works, and even to play with the loop variables in the code below to see what changes as you modify them.


A quick explanation of how a for loop works. So if we wrote a for loop like this:

for (int i = 0; i < 5; i  ) {...

This is what is happening under the hood:

for (initialization; termination; increment) {...

The first part int i = 0 is the initialization of where the loop starts from.

The second part i < 5 is the termination condition that causes the loop to stop when i is no longer less than 5.

The last part i is the increment, or what changes each time the loop finishes

Note that for the final increment condition we can do all sorts of things, it does not have to be i , we could decrease it with i-- or even do a bit of math i = i*2.


So when it comes to your question. In the first loop the simple solution is to work backwards, by starting high using the b value for the initialization like so for (int i = b;, and we want to work backwards so we use i-- as the increment action, and finally we use i > 0; in the middle as the termination so that we stop one we hit 0:

//start of the 1st loop
for (int i = b; i > 0; i--) {
    //...
}

Now for the inner loop we just need to modify it slightly so that it runs the correct number of times and add System.out.println(); after the inner loop so that the lines are correct:

//start of the 1st loop
for (int i = b; i > 0; i--) {
    for (int p = 0; p < i; p  ) {
        System.out.print(k  " ");
    }
    //Move to the next line
    System.out.println();
}

For the second loop we simply start from 0 and work up like normal:

//start of the 2nd loop
for (int i = 0; i < b; i  ) {
    //...
}

The interesting part is the inner loop, it also works as normal by starting from 0, and we just use a bit of logic, a simple short calculation int value = cn - (p*3); to work out what the printed value needs to be. This works because the value of p changes each loop and will subtract a multiple of 3 from cn to give 14 11 8 5... and so on:

//start of the 2nd loop
for (int i = 0; i < b; i  ) {
    for (int p = 0; p <= i; p  ) {
        //calculate value using cn - i*3 (To get -3 for each number)
        int value = cn - (p*3);
        System.out.print(value  " ");
    }
    //Move to the next line
    System.out.println();
}
//end of the 2nd loop`

And the output is:

H H H H H 
H H H H 
H H H 
H H 
H 

14 
14 11 
14 11 8 
14 11 8 5 
14 11 8 5 2 

CodePudding user response:

Assuming two different sets of nested for loops, here's an alternate approach for both:

String name = "Hotdogs";
int b = 5, cn = 14;
char k = name.charAt(0);

for (int row=1; row<=b; row  ) {
  for (int col=row; col<=b; col  ) {
    System.out.print(k   " ");
  }
  System.out.println();
}
for (int row=1; row<=b; row  ) {
  for (int col=1, x=cn; col<=row; col  , x=x-3) {
    System.out.print(x   " ");
  }
  System.out.println();
}

Note that in the second set of nested for loops that generates the numbers, in the INNER for loop, we're declaring two different variables and have two different incrementors separated by commas.

Output generated:

H H H H H 
H H H H 
H H H 
H H 
H 
14 
14 11 
14 11 8 
14 11 8 5 
14 11 8 5 2
  •  Tags:  
  • java
  • Related