*The Famous Triangle Project ( Made Easy ):Well, I have decided to just write a tutorial that I have seen several questions about from time to time here at D.I.C.
The goal of this program is to illustrate different ways loops and functions can be used. It's great PRACTICE!!!
What does the program do?1. The program will take user input and create a triangle using characters.
2. The way it works is the user enters a character of his or her choice such as '!' or '*' etc.
3. Then the user enters in the base of the triangle. The base size will be the total number of characters that the user previously entered. This will make up the bottom of the triangle (the base).
Let's Get Started:Let's start by looking at the code of the entire program in its entirety:
CODE
#include <iostream>
using namespace std;
// Function Prototypes:
void Welcome( char&, int& );
void charLoader( char, int );
void Triangle( int, char );
void Output( int );
int main( )
{
// Declared Variables:
char charType; // Character Type: defined by user
int base; // Will be the number of asterisks @ the bottom ( base ) of the triangle
// Function Calls :
Welcome( charType, base ); // Welcome MSG && User Input
Triangle( base, charType ); // Sends character type to be Printed
Output( base ); // Prints Triangle Info
return 0;
}// End of main
//*******************************************************************
void Welcome( char& charType, int& base ) // Function Heading
{
// Welcome Message && User Input
cout << "The Famous Triangle Project ( Made Easy ) " << endl;
cout << "___________________________________________" << endl;
cout << "Enter the char type: ";
cin >> charType;
cout << "Enter the Base: ";
cin >> base;
cout << endl << endl;
cout << "___________________________________________" << endl;
cout << "Your Triangle: " << endl;
cout << "___________________________________________" << endl;
}// End of Function Definition
//*******************************************************************
void charLoader( char charType, int currentBase )// Function Heading
{
for( int i = 0; i < currentBase; i++ )
cout << charType;
}// End of Function Definition
//*******************************************************************
void Triangle( int base, char charType )
{
for ( int currentBase = 1; currentBase <= base; currentBase++ )
{
charLoader( charType, currentBase );
cout << endl;
}// End of for
}// End of Function Definition
//*******************************************************************
void Output ( int base ) // Function Heading
{
cout << "___________________________________________" << endl;
cout << "Your Triangle's Base: " << base << endl << endl;
}// End of Function Definition
//*******************************************************************
Let's analyze what each block of code does:CODE
#include <iostream>
using namespace std;
// Function Prototypes:
void Welcome( char&, int& );
void charLoader( char, int );
void Triangle( int, char );
void Output( int );
The Function Protypes are functions that we are declaring. We will be defining these functions outside of main's scope. We will be calling these functions within the scope of main: 1. The function Welcome( ... ) will simply display a welcome message. It will also request from the user the type of character they want to use such as '!' or '*' etc.
2. The function charLoader( ... ) will print the "Current Base" of the Triangle.
3. The function Triangle( ... ) will be printing each "Current Base " up to "Base" times.
4. The function Output( ... ) will simply print the base of the triangle after the triangle has been printed.
The following code will be composed of the Function Calls within main( ):CODE
int main( )
{
// Declared Variables:
char charType; // Character Type: defined by user
int base; // Will be the number of asterisks @ the bottom ( base ) of the triangle
// Function Calls :
Welcome( charType, base ); // Welcome MSG && User Input
Triangle( base, charType ); // Sends character type to be Printed
Output( base ); // Prints Triangle Info
return 0;
}// End of main
Alright, the previous code was pretty much self-explanatory. Now for the fun stuff ( Function Definitions ):
The Welcome( ... ) Function:CODE
void Welcome( char& charType, int& base ) // Function Heading
{
// Welcome Message && User Input
cout << "The Famous Triangle Project ( Made Easy ) " << endl;
cout << "___________________________________________" << endl;
cout << "Enter the char type: ";
cin >> charType;
cout << "Enter the Base: ";
cin >> base;
cout << endl << endl;
cout << "___________________________________________" << endl;
cout << "Your Triangle: " << endl;
cout << "___________________________________________" << endl;
}// End of Function Definition
This subroutine's main function is to print a welcome message, and request the user for input.
We use two
pass by reference variables. charType and base will store the addresses of the variables in the main routine so we can later access those variables' information and pass that information into our other functions as we see necessary. "charType" is simply what character the user wants to use to make his or her triangle with, such as: '!' or '*' etc... The variable "base" will be the base of the triangle ( base == the number of characters at the bottom of the triangle:
Example:charType == &
base == 4
Output == &
&&
&&&
&&&&
The Function charLoader( ... ):CODE
void charLoader( char charType, int currentBase )// Function Heading
{
for( int i = 0; i < currentBase; i++ )
cout << charType;
}// End of Function Definition
This is probably one of the most important parts of the program. currentBase will be the index variable from the for loop within the next function Triangle ( ... ):
This is is what controls the printing of the "Current Base."
Example:& current base == 1
&& current base == 2
&&& current base == 3
&&&& current base == 4 ( now, currentBase == base )
The Function Triangle ( ... ):CODE
void Triangle( int base, char charType )
{
for ( int currentBase = 1; currentBase <= base; currentBase++ )
{
charLoader( charType, currentBase );
cout << endl;
}// End of for
}// End of Function Definition
This function contains the iterator which acts as the outer loop and will print the triangle's "current base " up to "base" times...
The Function Output( ... ):CODE
void Output ( int base ) // Function Heading
{
cout << "___________________________________________" << endl;
cout << "Your Triangle's Base: " << base << endl << endl;
}// End of Function Definition
This function just simply prints the base of the triangle.
Well, there goes the "Famous Triangle Project."

Hope, this was helpful
