Jump to content

Nested for loops?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
fluteplayer

fluteplayer

    Newbie

  • Members
  • Pip
  • 2 posts
Hi guys! I'm working on nested for loops in computer science at the moment, and am stuck on this one lab! I have to print out a picture of a rocket, but there's just so many symbols it's confusing me. Here's the link to the page:

http://cyfair3.schoo...jects/Lab17.pdf

The lab that I need, Lab17E, is at the very bottom. Thank you!

#2
code analyst

code analyst

    Newbie

  • Members
  • Pip
  • 4 posts
The basic idea is that you need to divide the rocket in triangles and try to get the pattern. even the spaces need to be considered in terms of the triangle.

hope that helps you

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
90 points for

*******

******

*****

****

***

**

*

And only 100 for the rocket ?! :O

Anyway, the nose / tail is fairly simple:

         /**\ 

        //**\\ 

       ///**\\\ 

      ////**\\\\ 

     /////**\\\\\ 

    //////**\\\\\\ 

   ///////**\\\\\\\ 

  ////////**\\\\\\\\ 

 /////////**\\\\\\\\\ 


If you did the previous exercises, you propably know how to do the 3 parts separately:

         /

        //

       ///

      ////

     /////

    //////

   ///////

  ////////

 /////////


**

**

**

**

** 

**

**

**

**


\ 

\\ 

\\\ 

\\\\ 

\\\\\ 

\\\\\\ 

\\\\\\\ 

\\\\\\\\ 

\\\\\\\\\ 

If you have these 3 double loops, you'll notice the outer loop is the same with all of them.
You then just have to cut and paste the 3 inner loops in 1 outer loop.
Like

88888881 

88888812 

88888123

88881234

88812345

88123456

81234567

12345678

consist of

8888888

888888

88888

8888

888

88

8

and

1

12

123

1234

12345

123456

1234567

12345678


Resulting in:

for( int i=0 ; i<8 ; i++){

      for( int j=0 ; j<7-i ; j++ ) {

        System.out.print("8");

      }

}


for( int i=0 ; i<8 ; i++){

      for(int k=0 ; k<i+1; k++){

        System.out.print(k+1); 

      }

      System.out.println("");

}

And you just put both inner loops together:

for( int i=0 ; i<8 ; i++){

      for( int j=0 ; j<7-i ; j++ ) {

        System.out.print("8");

      }

      for(int k=0 ; k<i+1; k++){

        System.out.print(k+1); 

      }

      System.out.println("");

    }


The body is slightly harder.
Will try to write some code tomorrow.. :P

#4
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Don't write the code for him man. Look, as code_analyst said, you look at the output, divide it into logical components and solve each task individually leading to the whole. Consider the rocket nose cone. You need one for loop to loop through each display line. Now ask yourself about how you will display the spaces, the slashes, and the astrix. How does the number of each correspond to the line you are on. You could probably do this with just a single for loop even.

#5
fluteplayer

fluteplayer

    Newbie

  • Members
  • Pip
  • 2 posts
Thank guys! I guess I just need to look at the smaller picture. I will give it a try :)

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

sam_l said:

Don't write the code for him man. Look, as code_analyst said, you look at the output, divide it into logical components and solve each task individually leading to the whole. Consider the rocket nose cone. You need one for loop to loop through each display line. Now ask yourself about how you will display the spaces, the slashes, and the astrix. How does the number of each correspond to the line you are on. You could probably do this with just a single for loop even.

I didn't write any of his code, did I?

#7
jmpotter

jmpotter

    Newbie

  • Members
  • Pip
  • 2 posts
can someone help me in making the code for this output? but the lines are composed of asterisk(*). Thanks!


Posted Image




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users