SebastianLawe

Untitled

Jan 28th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. // C5E3IsocelesTriangle.cpp : Defines the entry point for the console application.
  2. //Sebastian Lawe 12/6/2013 - Chapter 5 Exercise 3 - Creates an isoceles triangle.
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6. //Declare enumerable.
  7. enum TriangleType{RightAngle, Isocelese};
  8. //Declare funtions.
  9. void Triangle(TriangleType triangleType, int size);
  10. void DrawBar(int length);
  11. void DrawBar(int length, int spacing);
  12. int _tmain(int argc, _TCHAR* argv[])
  13. {
  14.     //Call "Triangle" to print isocelese triangle.
  15.     Triangle(Isocelese, 4);
  16.     //Keep program from closing.
  17.     std::getchar();
  18.     return 0;
  19. }
  20. void Triangle(TriangleType triangleType, int size){
  21.     //Using switch statsment to check what triangle is wanted.
  22.     switch(triangleType){
  23.     case RightAngle:
  24.         //In the case a right angle triangle is wanted, create right angle triangle.
  25.         //Using "for loop" to create a right angle triangle.
  26.         for(int i = 1; i <= size; i++){
  27.             //Call function to print a bar of asterisks.
  28.             DrawBar(i);
  29.         }
  30.         break;
  31.     case Isocelese:
  32.         //In the case an isocelese triangle is wanted, create isocelese triangle.
  33.         //Declare integer.
  34.         int spacing = size - 1;
  35.         int barLength = 1;
  36.         //Using "for loop" to create an isocelese triangle.
  37.         for(int i = 1; i <= size; i++){
  38.             //If "i" is an odd number...
  39.             if(barLength % 2 != 0){
  40.                 //Use "DrawBar" to print bar with spacing.
  41.                 DrawBar(barLength, spacing);
  42.                 //Decrement "spacing".
  43.                 spacing--;
  44.                 //Increment "barLength"
  45.                 barLength++;
  46.             }
  47.             //Use "while loop" to ensure "barLength" is odd.
  48.             while(barLength % 2==0){
  49.                 barLength++;
  50.             }
  51.         }
  52.         break;
  53.     }
  54. }
  55. void DrawBar(int length){
  56.     //Using "for loop" to draw the amount of asterisks "length" asks for.
  57.     for(int i = 0; i < length; i++){
  58.         //Print asterisk.
  59.         cout<<"*";
  60.     }
  61.     cout<<"\n";
  62. }
  63. void DrawBar(int length, int spacing){
  64.     //Using "for loop" to create spacing defined by "spacing".
  65.     for(int j = 0; j < spacing; j++){
  66.         //Print space.
  67.         cout<<" ";
  68.     }
  69.     //Using "for loop" to print bar length defined by "length".
  70.     for(int i = 0; i < length; i++){
  71.         //Print asterisk.
  72.         cout<<"*";
  73.     }
  74.     //Create new line.
  75.     cout<<"\n";
  76. }
Advertisement
Add Comment
Please, Sign In to add comment