d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Question
12Next
Add Reply New Topic New Poll
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Sep 17 2014 03:26pm
Okay, so I've been working on this for a while, to no avail, and was wondering if anyone can help.

I believe it's fairly simple but my brain is apparently numb to this sort of logic...

I need to print the following numbers out in this exact format:

121
12321
1234321
123454321
12345654321
1234567654321


I need to use 3 for-loops, 2 nested within 1 the outer for-loop.

In this sort of fashion:


Code
for(int x = 0; x < 6; x++)
{
for(int y = -; y < -; -----)
{
System.out.print(some var);
for(int z = -; z < -; -----)
{
System.out.print(some var);
}
System.out.println(); // creates next line
}
}


Any help would be much appreciated!
Keep in mind, the variables' increments could be anything in the 2 nested for-loops.
The outer for-loop controls how many lines that's going to be printed (in this case, 6)
The second, im guessing, controls the first number of each line, (1) and the numbers between the max number in each line and the last number in each line (2, 3, 4, 5, 6, 7 respectively)
While the last controls the max number in each line, BUT I really am not 100% sure

This post was edited by DD_Rocking on Sep 17 2014 03:40pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 17 2014 03:55pm
what is the problem you're facing?

this is how i'd approach the problem:

1) given n, figure out how to write out 1, 2, ..., n - 1, n
eg: given 4, write out 1234

2) given n, figure out how to write out 1, 2, ..., n -1, n, n - 1, .... 2, 1
eg: given 4, write out 1234321

3) given n, figure out how to write out 1, 2, ..., n -1, n, n - 1, .... 2, 1 and then put a new line at the end
eg: given 4, write out 1234321\n

4) assuming the above is done, loop it from 2 to n

do it in this order. don't try to set up all the loops at once, and everything is wrecked. you want to be able to show progress at each step so you can understand it and narrow down where the problem is
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Sep 17 2014 04:08pm
Quote (carteblanche @ Sep 17 2014 04:55pm)
what is the problem you're facing?

this is how i'd approach the problem:

1) given n, figure out how to write out 1, 2, ..., n - 1, n
eg: given 4, write out 1234

2) given n, figure out how to write out 1, 2, ..., n -1, n, n - 1, .... 2, 1
eg: given 4, write out 1234321

3) given n, figure out how to write out 1, 2, ..., n -1, n, n - 1, .... 2, 1 and then put a new line at the end
eg: given 4, write out 1234321\n

4) assuming the above is done, loop it from 2 to n

do it in this order. don't try to set up all the loops at once, and everything is wrecked. you want to be able to show progress at each step so you can understand it and narrow down where the problem is


this NEEDS to be written in 3 for-loops
and cannot use any other variables than ones declared and initialized within the for-loops

so, I would guess this would have to compare the variable in the 2nd for-loop to the variable in the first for-loop in some way in order to print the first number, then any following on each line
then compare the variable in the third to the second or first

this is where I am sort of lost

If I was able to, I would just use print statements with extra variables, id simply assign new variables and print them: var1 = 1, var2 = 2, var3 = 3, etc.

This post was edited by DD_Rocking on Sep 17 2014 04:08pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 17 2014 04:15pm
Quote (DD_Rocking @ Sep 17 2014 06:08pm)
this NEEDS to be written in 3 for-loops
and cannot use any other variables than ones declared and initialized within the for-loops

so, I would guess this would have to compare the variable in the 2nd for-loop to the variable in the first for-loop in some way in order to print the first number, then any following on each line
then compare the variable in the third to the second or first

this is where I am sort of lost

If I was able to, I would just use print statements with extra variables, id simply assign new variables and print them: var1 = 1, var2 = 2, var3 = 3, etc.


following my procedure, i was able to get it done to your teacher's satisfaction without even looking at your requirements. steps 1 and 2 are the two inner loops, step 4 is the outer loop.

so how about you show me your code from step 1, step 2, step 3, and step 4? if it doesn't fit your assignment's requirements, we'll work on it.


This post was edited by carteblanche on Sep 17 2014 04:18pm
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Sep 17 2014 04:17pm
Quote (carteblanche @ Sep 17 2014 05:15pm)
following my procedure, i was able to get it done without even looking at your requirements. steps 1 and 2 are the two inner loops, step 4 is the outer loop.

so how about you show me your code from step 1, step 2, step 3, and step 4? if it doesn't fit your assignment's requirements, we'll work on it.


well, its not really an assignment, my prof said it is extra credit on the exam I will be taking in a few hours

anyway, here's what I've got, keep in mind, this is completely incorrect in some aspects.

Code
for(int i = 0; i < 2; i++)
{
for(int j = i+1; j < i+3; j++)
{
System.out.print(j);
for(int k = j; k < i+1; k++)
{
System.out.print(i+2);
}
}
System.out.println();
}


I am confused how I would go about getting it to print each number in between the max number and the end, including the end :(

So far, I've been able to get it to print 121 on a single line, but adding more lines causes it to print incorrectly
Also, I've been able to get it to print 1, 12, 123, 1234, etc. on each line for 6 lines (last line = 123456)

This post was edited by DD_Rocking on Sep 17 2014 04:19pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 17 2014 04:19pm
Quote (DD_Rocking @ Sep 17 2014 06:17pm)
well, its not really an assignment, my prof said it is extra credit on the exam I will be taking in a few hours

anyway, here's what I've got, keep in mind, this is completely incorrect in some aspects.

Code
for(int i = 0; i < 2; i++)
  {
  for(int j = i+1; j < i+3; j++)
  {
    System.out.print(j);
    for(int k = j; k < i+1; k++)
    {
    System.out.print(i+2);
    }
  }
  System.out.println();
  }


I am confused how I would go about getting it to print each number in between the max number and the end, including the end :(


your instructions are slightly conflicting.

Quote
I need to use 3 for-loops, 2 nested within 1 the outer for-loop.

the way that's worded, the code should look like this:
Code
for (...){
for (...){
}
for (....){
}
}


which is different from your sample. what did your teacher give you, precisely?

regardless, how about you write code specifically for each of my 4 steps that i listed? then we'll go from there.

This post was edited by carteblanche on Sep 17 2014 04:20pm
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Sep 17 2014 04:23pm
Quote (carteblanche @ Sep 17 2014 05:19pm)
your instructions are slightly conflicting.


the way that's worded, the code should look like this:
Code
for (...){
    for (...){
    }
    for (....){
    }
}


which is different from your sample. what did your teacher give you, precisely?


It was just a question that was on our practice exam
then he went over the example in-class, but doesn't post the answers, and I wasn't able to get it written down in its entirety :(

but, basically he did this:

Code

for(...){
for(...){
System.out.print(...);
for(...){
System.out.print(...);
} // end 3rd for
} // end 2nd for
System.out.println(); // move to next line
} // end first for

Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Sep 17 2014 04:30pm
Quote (carteblanche @ Sep 17 2014 04:55pm)
what is the problem you're facing?

this is how i'd approach the problem:

1) given n, figure out how to write out 1, 2, ..., n - 1, n
eg: given 4, write out 1234

2) given n, figure out how to write out 1, 2, ..., n -1, n, n - 1, .... 2, 1
eg: given 4, write out 1234321

3) given n, figure out how to write out 1, 2, ..., n -1, n, n - 1, .... 2, 1 and then put a new line at the end
eg: given 4, write out 1234321\n

4) assuming the above is done, loop it from 2 to n

do it in this order. don't try to set up all the loops at once, and everything is wrecked. you want to be able to show progress at each step so you can understand it and narrow down where the problem is


on #2, how would I do 1234321 in 1 for loop?
I can do 1234
like this:

for(int i = 1; i <= 4; i++)
{
System.out.print(i);
}

Thats no problem, but how would I now go about going from 3 to 1 on the same line? Would I utilize the other for loop to write it on the same line but going from i (while it's 4) down to 1?

I could understand that :)
Member
Posts: 13,042
Joined: Apr 12 2008
Gold: 0.00
Sep 17 2014 04:45pm
Here's what I've got thus far:

Code
for(int i = 1; i < 7; i++)
{
for(int j = 1; j <= i+1; j++)
{
System.out.print(j);
for(int k = j; k > i; k--)
{
System.out.print(j-1);
}
}
System.out.println();


}


Output:

121
1232
12343
123454
1234565
12345676


The problem is that I can't get the remaining numbers at the end print...
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 17 2014 04:56pm
Quote (DD_Rocking @ Sep 17 2014 06:45pm)
Here's what I've got thus far:

Code
for(int i = 1; i < 7; i++)
  {
  for(int j = 1; j <= i+1; j++)
  {
    System.out.print(j);
    for(int k = j; k > i; k--)
    {
    System.out.print(j-1);
    }
  }
  System.out.println();
 
 
  }


Output:

121
1232
12343
123454
1234565
12345676


The problem is that I can't get the remaining numbers at the end print...


couple suggestions.

1) stop worrying about the outer loop and just focus on the two inner loops to create 1 line of output.

replace your outer loop with just this:

int i = 7;

2) if you're having a problem with a specific loop, then write the code WITHOUT loops.

ex: instead of something like this:
for (int j = 0; j < 4; j++){
print(j);
}

you would write this:

print(0);
print(1);
print(2);
print(3);

then once you get the exact output you want, look at the numbers you had to use and figure out the loop that makes it
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll