Thread: excercise

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    Talking excercise

    the book says: write a program that displays the following patterns seperately, one below the other. use for loops to generate the patterns. All stars should be printed by a single statement of the form Console.Write('*'); A statement of the form Console.WriteLine(); can be used to position the next line. A statement of the form Console.Write(' '); can be used to display spaces for the last two patterns. there should be no other output statements in the program.

    Code:
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
             *
            **
           ***
          ****
         *****
        ******
       *******
      ********
     *********
    **********
    the code is
    Code:
    using System;
    
    namespace ex5_7
    {
    	
    	class Class1
    	{
    
    
    		[STAThread]
    		static void Main(string[] args)
    		{
    			//first right triangle of stars bottom left
    			for(int outer=1; outer<=10; outer++)
    			{
    				for(int inner=1; inner<=outer; inner++)
    				{
    					Console.Write('*');
    				}
    				Console.WriteLine();
    			}
    			//second right triangle top left
    			for(int outer=10; outer>0; outer--)
    			{
    				for(int inner=1; outer>=inner; inner++)
    				{
    					Console.Write('*');
    				}
    				Console.WriteLine();
    			}
    			
    			
    			//third right triangle top right
    			int noofstars = 10;
    
    			for(int outer=1; outer<=10; outer++, noofstars--)
    			{		
    				for(int stars=noofstars; stars>0; stars--)
    				{
    					Console.Write("*");
    				}
    				Console.WriteLine();
    				if(outer==10)
    					break;
    				
    				for(int space=1; space<=outer; space++)
    					Console.Write(" ");
    			}
    
    			//fourth right triange bottom right
    			noofstars=1;
    
    			for(int outer=10; outer>0; outer--, noofstars++)
    			{		
    				for(int space=1; space<outer; space++)
    					Console.Write(" ");
    				
    				for(int stars=1; noofstars>=stars; stars++)
    				{
    					Console.Write("*");
    				}
    				Console.WriteLine();
    			}
    
    			
    		}
    	}
    }
    the next excercise asks...modify excercise 5.7 to combine your code from the four seperate triangles of asterisks into a single program that prints all four patterns side by side, making clever use of nested for loops.

    i have started by writing down what i need to do in pseudo code which is bacically a sequential list of actions but i have no idea how to 'get off the ground' on this one, i would appreciate some feedback please. the problem is the the cursor gets drawn linearly and nested for loops work like car mileometers, i cant think how to make both work together...pls help


    luigi
    Last edited by luigi40; 11-18-2005 at 10:43 AM. Reason: more info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 2
    Last Post: 10-16-2008, 09:23 PM
  2. Just learning, problem with an excercise.
    By medeshago in forum C Programming
    Replies: 8
    Last Post: 10-15-2008, 07:10 PM
  3. Excercise in book
    By gin in forum C++ Programming
    Replies: 6
    Last Post: 06-28-2008, 01:09 PM
  4. Creative Excercise
    By ... in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 01-29-2003, 10:18 AM