So in class this week were given the following problem to solve:

“Write an application that displays the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks () should be printed by a single statement of the form Console.Write( “” ); which causes the asterisks to print side by side. A statement of the form Console.WriteLine(); can be used to move to the next line. A statement of the form Console.Write( “ “ ); can be used to display a space for the last two patterns. There should be no other output statements in the application. [Hint: the last two patterns require that each line begin with an appropriate number of blank spaces.]”

Here is my solution:

 1 public static void Main( ) 
 2     {
 3         OutputAC( delegate( Int32 xAxis, Int32 i ) { return ( xAxis < i + 1 ); } );
 4 
 5         OutputBD( delegate( Int32 xAxis, Int32 i ) { return ( xAxis < i ); } );
 6 
 7         OutputAC( delegate( Int32 xAxis, Int32 i ) { return ( xAxis > = i ); } );
 8 
 9         OutputBD( delegate( Int32 xAxis, Int32 i ) { return ( i <= xAxis + 1 ); } );
10         Console.ReadLine( );
11     }
12 
13     private delegate Boolean CheckCondition( Int32 xAxis, Int32 charactersPerRow );
14 
15     private static void OutputAC( CheckCondition condition ) 
16     {
17         Int32 charactersPerRow = 0;
18         // loop through the y axis from row 0 to 10
19         for ( int yAxis = 0; yAxis < 10; yAxis++ ) 
20         {
21             // loop through x axis from column 0 to 10 for each row.
22             for ( int xAxis = 0; xAxis < 10; xAxis++ ) 
23             {
24                 // write the character for the current position
25                 Console.Write( condition( xAxis, charactersPerRow ) ? "*" : " " );
26             }
27             ++charactersPerRow;
28             Console.WriteLine( );
29         }
30     }
31 
32     private static void OutputBD( CheckCondition condition ) 
33     {
34         Int32 charactersPerRow = 10;
35         for ( int yAxis = 0; yAxis < 10; yAxis++ ) 
36         {
37             for ( int xAxis = 0; xAxis < 10; xAxis++ ) 
38             {
39                 Console.Write( condition( xAxis, charactersPerRow ) ? "*" : " " );
40             }
41             --charactersPerRow;
42             Console.WriteLine( );
43         }
44     }

Assignment1.zip (5.62 KB)

comments powered by Disqus