Got Alot to make u stunn and thirsty about this Binary World.... HapPy SurFInG

Like us on Facebook

Monday, November 18, 2013

Solid Square & Tree Program (User Generated size)‏ (C++ Program)



/* Project: Tutorial
* Author: Modgil
* Date: 18/11/2013
* Description: Program uses a main function to take a user generated defined number to
deine the size of a square and calls a drawSquare function to generate the
columns and rows of the square using asterisks.
*/
 
#include <iostream> // Uses output stream
using namespace std; // C++ standard library declaration
 
int drawSquare(int myNumbers) // Draw square function uses nested for loop to draw square
// passing 1 parameter as an argument
{
int myNumber; // passing integer myNumber as argument
myNumber = myNumbers; // assigning argument
 
// for loop draws first user generated rows
for (int i = 0; i < myNumber; i++)
{
for (int i = 0; i < myNumber; i++) // nested for loop draws columns
{
cout << "* "; // outputs asterisk to screen with space between each loop cycle
}
cout << endl; // ends outputs line to suggest next row
} // end of inner for
 
return (myNumber); // return for looped asterisk
} // end of draw square function
 
int drawTree(int width, int height)
{
int treeWidth = width; // Initialise variables to paramaters
int treeHeight = height;
int m = treeWidth - 1; // tree width logic
int q = (treeWidth + 1) / 2; // correct amount of horizontal asterisks for bottom half of tree
 
 
// FIRST NESTED FOR LOOP DRAWS THE TOP HALF OF THE TREE
for (int l=0; l < treeWidth; l++) // for loop control structure, columns
{
for (int i = m; i > 0; i--) // nested loop for pyramid spacing
{
cout << " ";
}
for (int j= 0; j <= l; j++) // asterisk pyramid output top half of tree
{
cout << "* ";
}
cout << "\n"; // prints new line every cycle
m--; // decrement for tree width logic so different amounts of asterisks are drawn each cycle
} // End of tree for loop
 
// SECOND NESTED FOR LOOP DRAWS THE BOTTOM HALF OF THE TREE
for (int i = 0; i < treeHeight; i++) // first for loop draws the correct amount of bottom tree asterisks supplies by the logic assigned to q above
{
for (int y =0; y < treeWidth / 2; y++) // tree width logic assigns trunk spacing to correct position
{
cout << " "; // prints spacing
}
for (int i =0; i < q; i++) // prints asterisks for tree trunk
{
cout << "* ";
}
cout << "\n"; // print snew line
}
 
return (treeWidth, treeHeight); // returns parameters to main function
}
 
int main() // main function acts as entry point
{
// Declares integer variable for user generated input
int number;
int width;
int height;
 
// Prompts user to enter a number
cout << "Enter a number: ";
cin >> number; // stores user inp ut in integer variable
 
drawSquare(number); // Calls draw square function with user generated number passed as parameter
 
// Prints a new line
cout << endl;
// Prompts user to enter width of tree
cout << "Enter width: " << endl;
cin >> width; // Stores input in variable
 
cout << "Enter height: " << endl;
cin >> height;
 
// Calls drawTree function
drawTree(width, height);
 
cin.ignore(2); // stops program from exiting
return 0;
 
} // End of main function
Share:

0 comments:

Post a Comment