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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/**
* The following program creates a histogram which scales itself to fit in the
* range of a list of numbers, with the first number entered being the minimum
* and the second number entered being the maximum.
**/
// Import statements:
#include <stdio.h>
#include <iostream>
using namespace std;
// Create prototypes:
int readData(int nums[], int max);
int axisWidth(int nums[], int biggestFrequency);
void drawHorizontalAxis(int axisSize);
void labelHorizontalAxis(int axisSize, int nums[]);
void drawCount(int value, int largestNum);
int greatestFrequency(int nums[]);
// Main method:
int main(){
const int MAX = 25;
int nums[MAX];
int value = readData(nums,MAX);
int largestNum = nums[1];
readData(nums, MAX);
int biggestFrequency = greatestFrequency(nums);
int axisSize = axisWidth(nums, biggestFrequency);
drawHorizontalAxis(axisSize);
labelHorizontalAxis(axisSize, nums);
drawCount(value, largestNum);
int variable = axisWidth(nums, biggestFrequency);
return 0;
}
/**
* @brief Method to read the data inputted:
* @param num
* @param max
*/
int readData(int nums[], int max){
int i = 0;
cin >> nums[i]; // saves the number the user entered
while (cin){
++i;
cin >> nums[i]; // update number of times num appeared in the input
}
return i;
}
/**
* @brief Method to compute the width of the axis
* @param num : the array of numbers entered by the user
* @param biggestFrequency : the most frequent number entered in the num[] array
* @return : the size of the horizontal axis
*/
int axisWidth(int nums[], int biggestFrequency){
int axisSize = biggestFrequency + 10- biggestFrequency % 10;
if (axisSize < 10){
axisSize = 10;
}
return axisSize;
}
/**
* @brief Method to draw the horizontal axis; the largest increment is an axis width of 10, with a "+" mark every 5 values
* @param axisSize : The size of the horizontal axis
*/
void drawHorizontalAxis(int axisSize){
int width = axisSize;
// write spaces to align the first + with the vertical axis
for(int i = 0; i <= (width/10); i++){
// write ----+----+ for width /10 times
cout << "----+----+";
}
cout << "\n"; // prints a new line
}
/**
* @brief Method to label the horizontal axis
* @param axisSize : The size of the horizontal axis
* @return
*/
void labelHorizontalAxis(int axisSize, int nums[]){
int width = axisSize;
// write spaces to align the first + with the vertical axis
// note the input allows up to 3 digit values; if a larger num is included, the rows may not line up nice
// print a 0 aligned with the vertical axis
int count = 10;
for(int i=0; i<=width;i++){
if (width%10 == 0){
// if width mod 5 = 0 , print out counter, starting with 5
cout << count;
count = count+10;
// iterate through the array:
for(int i = 0; i <= width; i++){
// if it is 1 char, print 2 spaces
if(nums[i] <= 10){
cout << " ";
}
// if it is 2 characters, print 1 space
else if(10 < nums[i] && nums[i] < 100){
cout << " ";
}
// if it is 3 characters, print 0 spaces
else{
cout << "";
}
}
cout << " ";
}
cout << "\n";
}
}
/**
* @brief This method will find the number with the greatest frequency in the nums[] array
* @param nums : The array of nums entered by the user
* @return : number with greatest frequency
*/
int greatestFrequency(int nums[]){
int count = 1;
int maxCount = 1;
int largestNum = nums[1];
// iterate through the array to find the most frequent number:
for(int i = 1; 1 < largestNum; i++){
if(nums[i] == nums[i-1]){
count++;
}
else{
if(count > maxCount){
maxCount = count;
largestNum = nums[-1];
}
count = 1;
}
}
// if the last element is the most frequent:
if (count > maxCount){
maxCount = count;
largestNum = nums[count-1];
}
return largestNum;
}
/**
* @brief Draws the frequency of the numbers entered, represented by a '*'
* @param value : The number entered
* @param quantity : Frequency of a number
* @param largestNum : The largest number entered by the user
* @return
*/
void drawCount(int value, int largestNum){
// print the quantity with enough spaces in front of it to line up 3-digit values
cout << " |";
// print "#" quantity times:
while(value < largestNum){
for(int z = 0; z < largestNum; z++){
cout << "*";
}
cout << " ";
value++;
}
cout << "\n";
}
| |