C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,419 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,510 people online right now. Registration is fast and FREE... Join Now!




The Famous "Triangle" Project

 
Reply to this topicStart new topic

> The Famous "Triangle" Project, Tutorial: Use of Loops and Functions

Enfinik
Group Icon



post 1 Jul, 2009 - 01:46 PM
Post #1


*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 ): smile.gif

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." smile.gif
Hope, this was helpful smile.gif

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 11:57PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month