Luigi

Luigi

  • NA
  • 7
  • 0

book exercise

Nov 18 2005 11:43 AM
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. here is my output... Code: * ** *** **** ***** ****** ******* ******** ********* ********** ********** ********* ******** ******* ****** ***** **** *** ** * ********** ********* ******** ******* ****** ***** **** *** ** * * ** *** **** ***** ****** ******* ******** ********* ********** the code i produced... 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=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 hell knows how to format the text on this forum...no help to read