excercise

This is a discussion on excercise within the C# Programming forums, part of the General Programming Boards category; the book says: write a program that displays the following patterns seperately, one below the other. use for loops to ...

  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

  2. #2
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    30 views and still nothing, guess im on my own

  3. #3
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    my answer to execise 5.8

    this is the code i came up with, however, i dont think this is what Deitel are looking for, is this 'clever' use of nested loops?

    Code:
    using System;
    
    namespace Ex5_8
    {
    	
    	class Class1
    	{
    		[STAThread]
    		static void Main(string[] args)
    		{
    			int midspaces = 0, countd = 10, countu = 1;
    			
    
    			for(int outer=0; outer<10; outer++, countd--, countu++)
    			{
    				for(int h=0; h<=outer; h++)//1st stars triangle needs to inc on each loop from 1 to 10
    					Console.Write('*');
    
    				for(int i=1; i<=countd; i++)
    					Console.Write(' ');//1st space triangle needs to dec from 9 to 0
    					
    				for(int j=1; j<=countd; j++)
    					Console.Write('*');//2nd star triangle needs to dec from 10 to 1
    					
    				for(int k=0; k<midspaces; k++)
    					Console.Write(' ');//middle space triangle needs to inc from 0 to 18
    					midspaces = midspaces+2;//in steps of 2
    
    				for(int l=1; l<=countd; l++)//3rd star triangle needs to dec from 10 to 1
    					Console.Write('*');
    					
    				for(int m=1; m<=countd; m++)//4th space triangle needs to dec from 9 to 0
    					Console.Write(' ');
    
    				for(int n=0; n<countu; n++)//4th star triangle needs to inc from 1 to 10
    					Console.Write('*');
    
    				Console.WriteLine();//newline
    			}
    			
    		}
    	}
    }
    which produces the following output
    Code:
    *          ********************          *
    **         *********  *********         **
    ***        ********    ********        ***
    ****       *******      *******       ****
    *****      ******        ******      *****
    ******     *****          *****     ******
    *******    ****            ****    *******
    ********   ***              ***   ********
    *********  **                **  *********
    ********** *                  * **********
    i would really appreciate some feedback, there must be someone reading this forum who has worked with for nested for loops

    thanks in advance

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I don't know C#, but I was just wondering what the [STAThread] does? Does it indicate which class has Main?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    is this 'clever' use of nested loops?
    You could definitely do it in a "cleverer" way. In C (sorry, as mentioned, I don't know C#):
    Code:
    #include <stdio.h>
    
    int main(void) {
        int x, y, z;
    
        for(x = 0; x < 10; x ++) {
            for(z = 0; z < 4; z ++) {
                for(y = 0; y < 10; y ++) {
                    if(!z || z == 3) putchar(y <= x ? '*' : ' ');
                    else putchar(y <= (10-x) ? '*' : ' ');
                }
            }
            printf("\n");
        }
    }
    Something like that. I don't have a compiler on this computer, so I can't tell you if it works.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    dwks
    i think i read the [STAThread] is something to do with the dot net environment about threading, you can take it out and it doesnt seem to make any difference. thanks for your algorithm, the language doesnt matter, its the idea that helps


    luigi

  7. #7
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    reply

    hi dwks

    cant make sense of the if statement, it wint complie for me. had to make it if(z == 3)

    as z is an integer how can you have NOT z?

    i will play with the code anyway, anyone else willing to help please do

    luigi

  8. #8
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In C,
    Code:
    if(!z || z == 3)
    is the same as
    Code:
    if(z == 0 || z == 3)
    In C/C++, anything that is non-zero is true. So you can use
    Code:
    if(z)
    and the if will execute if z is anything but zero. This code
    Code:
    if(!z)
    does the opposite, executing the if if z is zero.

    Is C# like Java? (if(z) is invalid in Java . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    thanks dwks

    this is what i came up with...

    Code:
    using System;
    
    namespace Ex5_8
    {
    	
    	class Class1
    	{
    		[STAThread]
    		static void Main(string[] args)
    		{
    
    
    			int a, b, c;
    
    			for(a = 1; a <= 10; a++) 
    			{
    				for(b = 1; b <= 4; b ++) 
    				{
    					for(c = 1; c <= 10; c++) 
    					{
    					if(b==1)Console.Write(c <= a ? '*' : ' ');
    					else if(b==2)Console.Write(c <= (11-a) ? '*' : ' ');
    					else if(b==3)Console.Write(c >= a ? '*' : ' ');
    					else if(b==4)Console.Write(c <= (10-a) ? ' ' : '*');
    					}
    				}
    				Console.WriteLine();
    			}
    
    			
    //output 			
    //*         ********************         *
    //**        *********  *********        **
    //***       ********    ********       ***
    //****      *******      *******      ****
    //*****     ******        ******     *****
    //******    *****          *****    ******
    //*******   ****            ****   *******
    //********  ***              ***  ********
    //********* **                ** *********
    //***********                  ***********
    
    
    
    			
    		}
    	}
    }
    thank you for your feedback dwks
    any feedback on improvements would be welcomed

    luigi

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21