Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C5E3IsocelesTriangle.cpp : Defines the entry point for the console application.
- //Sebastian Lawe 12/6/2013 - Chapter 5 Exercise 3 - Creates an isoceles triangle.
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- //Declare enumerable.
- enum TriangleType{RightAngle, Isocelese};
- //Declare funtions.
- void Triangle(TriangleType triangleType, int size);
- void DrawBar(int length);
- void DrawBar(int length, int spacing);
- int _tmain(int argc, _TCHAR* argv[])
- {
- //Call "Triangle" to print isocelese triangle.
- Triangle(Isocelese, 4);
- //Keep program from closing.
- std::getchar();
- return 0;
- }
- void Triangle(TriangleType triangleType, int size){
- //Using switch statsment to check what triangle is wanted.
- switch(triangleType){
- case RightAngle:
- //In the case a right angle triangle is wanted, create right angle triangle.
- //Using "for loop" to create a right angle triangle.
- for(int i = 1; i <= size; i++){
- //Call function to print a bar of asterisks.
- DrawBar(i);
- }
- break;
- case Isocelese:
- //In the case an isocelese triangle is wanted, create isocelese triangle.
- //Declare integer.
- int spacing = size - 1;
- int barLength = 1;
- //Using "for loop" to create an isocelese triangle.
- for(int i = 1; i <= size; i++){
- //If "i" is an odd number...
- if(barLength % 2 != 0){
- //Use "DrawBar" to print bar with spacing.
- DrawBar(barLength, spacing);
- //Decrement "spacing".
- spacing--;
- //Increment "barLength"
- barLength++;
- }
- //Use "while loop" to ensure "barLength" is odd.
- while(barLength % 2==0){
- barLength++;
- }
- }
- break;
- }
- }
- void DrawBar(int length){
- //Using "for loop" to draw the amount of asterisks "length" asks for.
- for(int i = 0; i < length; i++){
- //Print asterisk.
- cout<<"*";
- }
- cout<<"\n";
- }
- void DrawBar(int length, int spacing){
- //Using "for loop" to create spacing defined by "spacing".
- for(int j = 0; j < spacing; j++){
- //Print space.
- cout<<" ";
- }
- //Using "for loop" to print bar length defined by "length".
- for(int i = 0; i < length; i++){
- //Print asterisk.
- cout<<"*";
- }
- //Create new line.
- cout<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment