Visual C# 6.15 (Displaying Triangles)

This Solution is for the “Displaying Triangles” of chapter 6 in the textbook; Visual c# How to program 6th edition by Deitel.

Please Note:
This solution contains four Classes that must be included with the program in order for it to work. The Classes are provided first and the program is last.

using System;
class shapeA

{
public void SHAPEY()
{
//initialises variables
int maxlength = 10;
int outrlpcount;
int starcountr;//tracks stars being printed

//sets outrloops variable to 1
//also as long as the outrloops is less than the maxlegth loops
//then increments outrloops by 1
for (outrlpcount = 1; outrlpcount <= maxlength; ++outrlpcount)
{
//as long as starcounter is less than outerloopcount, prints asterisks
//sets starcounter back to 1 on each inner loop
//then increments starcounter by 1
for (starcountr = 1; starcountr <= outrlpcount; ++starcountr)
{
Console.Write(“*”);
}
//when inner loop is exited
Console.WriteLine();//drops to next line
}
Console.WriteLine();//adds an empty line at end of the class
}
}

SECOND CLASS

using System;
class shapeB

{
public void SHAPEYB()
{
//initialises variables
int maxlength = 10;
int outrlpcount;
int starcountr;//tracks stars being printed
int countdown;

//sets outerloopcounter to 1
//as long as outerloopcounter is less than maxlength loops,
//increments outerloopcounter by 1
for (outrlpcount = 1; outrlpcount <= maxlength; ++outrlpcount)
{
countdown = 1;//sets countdown to 1 each loop

//as long as countdown is less than 10 minus the outerloopcounter value, loops
//this while statement increments countdown to 10 minus the outerloopcounter value
while (countdown <= 10 – outrlpcount)//with each loop, outerloop value increases and countdown decreases

++countdown;//increments countdown by 1
{
//as long as starcounter is less than outerloops prints asterisks
//sets starcounter back to 1 on each inner loop
//then increments starcounter by 1
for (starcountr = 1; starcountr <= countdown; ++starcountr)
{
Console.Write(“*”);
}
//when inner loop is exited
Console.WriteLine();//drops to next line
}

}
Console.WriteLine();//adds an empty line at end of the class
}
}

THIRD CLASS

using System;
class shapeC

{
public void SHAPEYC()
{
int maxlength = 10;
int outrlpcounter;
int starcountr;

//sets outerloopcounter to 0
//as long as outerloopcounter is less than maxlength, loops
//increments outerloopcounter by 1
for (outrlpcounter = 0; outrlpcounter <= maxlength; ++outrlpcounter)
{
//sets starcount to 0 each loop
starcountr = 0;

//as long as starcount is less than the outerloopcounter, loops
while ( starcountr < outrlpcounter )
{
Console.Write(‘ ‘);//prints empty char
++starcountr;//increments starscounter by 1
}
//as long as starcount is less than maxlength minus the outerloopcounter value’ loops
//increments starcount by 1
for (starcountr = 1; starcountr <= maxlength – outrlpcounter; ++starcountr)
{
Console.Write(“*”);//prints an asterisk
}
Console.WriteLine(); //new line after each row
}

}
}

FOURTH CLASS

using System;
class shapeD

{
public void SHAPEYD()
{
int maxlength = 10;
int outrlpcount;
int starcountr;
int countdown;

//sets outerloopcount to 1
//as long as outerloopcounter is less or equal to maxlength, loops
//increments outerloopcounter by 1
for (outrlpcount = 1; outrlpcount <= maxlength; ++outrlpcount)
{
countdown = 1;//sets countdown to 1 each loop

//as long as countdown is less than the maxlength minus the outerloopcounter value, loops
while ( countdown <= maxlength – outrlpcount)
{
Console.Write(‘ ‘);//prints empty char
++countdown;//increments countdown by 1
}
//as long as starcounter is less than outerloopcount, prints asterisks
//sets starcounter back to 1 on each inner loop
//then increments starcounter by 1
for (starcountr = 1; starcountr <= outrlpcount; ++starcountr)
{
Console.Write(“*”);
}
//when inner loop is exited
Console.WriteLine();//drops to next line
}
Console.WriteLine();//adds an empty line at end of the class
}
}

MAIN PROGRAM

//Program Name: C06P615
//Author: Alexander Anaya Ashe
//Class: MIS118 Spring 2017
//Date: 5/10/17
/*Description:
Uses for loops to generate and display the specified patterns, one below the other.
*/
using System;

public class ShapesRunner
{
public static void Main()
{
Console.WriteLine(“(a)”);
shapeA application = new shapeA();
application.SHAPEY();

Console.WriteLine(“(b)”);
shapeB application2 = new shapeB();
application2.SHAPEYB();

Console.WriteLine(“(c)”);
shapeC application3 = new shapeC();
application3.SHAPEYC();

Console.WriteLine(“(d)”);
shapeD application4 = new shapeD();
application4.SHAPEYD();
}
}

PROGRAM OUTPUT

(a)
*
**
***
****
*****
******
*******
********
*********
**********

(b)
**********
*********
********
*******
******
*****
****
***
**
*

(c)
**********
*********
********
*******
******
*****
****
***
**
*

(d)
*
**
***
****
*****
******
*******
********
*********
**********

Press any key to continue . . .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s