1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/*
Program Name: Function Fun
Programmer: Kevin Bond
Date: 11-10-14
Purpose:
Use functions to implement the different parts of loop fun
*/
#include<iostream>
#include<iomanip>
using namespace std;
// function prototyping
int getInteger(int Low, int High);
void printRangeAndAvg(int Low, int High);
void printBox(int Height, int Width);
void printWedge(int Height);
void main()
{
//create variables
int Width;
int Height;
// prompt for height and width using getinteger function
cout << "Enter box height (between 3 and 10): ";
Height = getInteger(3, 10);
cout << "Enter box width (between " << Height << " and 20): ";
Width = getInteger(Height+1, 20);
//output results of other functions
printRangeAndAvg(Height, Width);
printBox(Height, Width);
printWedge(Height);
//keep the console open
cin.get();
cin.ignore();
}
//================================================================
// Function: getInteger
// Description: Prompt user to repeat input of Height and Width if
// out of bounds
//Arguments: Low (int), High (int) -sets low and high paramenters
// Return value: Prompt to repeat if out of bounds
//=================================================================*/
int getInteger(int Low, int High)
{
int Height;
cin >> Height;
//prompt user to repeat if out of bounds
while(Height < Low || Height > High)
{
cout << "That number is out of bounds: Try again: ";
cin >> Height;
}
return Height;
}
//================================================================
// Function: printRangeAndAvg
// Description: Determines integers between height and width, the
// sum of them and average of the integers
// Input Arguments: Height, Width (int) - the integers entered by the user
// Return value: outputs All integers between Height and Width,
// the sum and the average of those integers
//=================================================================*/
void printRangeAndAvg(int Height, int Width)
{
//define local variables
int NumSum = 0;
int NumCount = 0;
float Average;
//output the integers between height and width
cout << "\nThe integers between " << Height << " and " << Width
<< " are:" << endl <<"\t";
//calculate the number of numbers between height and width
NumCount = (Width - Height) + 1;
//calculate sum of integers between height and width
for (int i=Height; i<=Width; i++)
{
cout << i << " ";
NumSum = NumSum+i;
}
cout << endl;
//calculate the average of numbers between height and width
Average = static_cast<float>(NumSum) / NumCount;
cout << "and the average of those numbers is " << Average << endl << endl;
}
//================================================================
// Function: printBox
// Description: Prints out a rectangle of asterisks with user Height and Width
// Arguments: Height (int), Width(int)- integers set by user
// Return value: Prints rectangle with user Height and Width
//=================================================================*/
void printBox(int Height, int Width)
{
//define local variable
char Asterisk = '*';
//output hollow rectangle with user input height and width
cout << Asterisk << setw(Width-1) << setfill ('*') << Asterisk << endl;
for(int a=0; a<Height-2; a++)
{
cout << Asterisk << setw(Width-1) << setfill(' ') << Asterisk << endl;
}
cout << Asterisk << setw(Width-1) << setfill ('*') << Asterisk << endl;
cout << endl;
}
//================================================================
// Function: printWedge
// Description: prints a triangle with user height, starting with 2 asterisks
// on top and adding 2 asterisks per row
// Arguments: Height(int) - sets number of rows to user height
// Return value: Prints Triangle with set criteria
//=================================================================*/
void printWedge(int Height)
{
//Output triangle with row = height, starting with 2 asterisks on top
for (int x=1; x<=Height; x++)
{
for(int y=1; y<=x; y++)
{
cout << "**";
}
cout << endl;
}
}
|