I have created a histogram program that has the user enter the lower limit of the range, then generates a 20 number array with that number at array[0] and the lower limit + 20 at array[20]. It then does a random number generation where 85% of the randoms are within the range and 15% are outside, and counts how many fall outside of the range. Then it counts occurances and prints a histogram that shows the amount below range, then prints the range (number, count, #'s or *'s), a # sign is printed for every 10 occurances that a number is counted and a * for every 1 after that 10 or if there are not 10 occurances. Just wanted to explain what the program did to allow for better knowledge of the situation.
Now, my problem is that whenever the user enters a negative number whose number does not fall into a positive range, such as user enters -50, creates a range of -50 to -30, as opposed to entering -10 and range is -10 to 10, there seems to be a problem with the randoms generating any occurances of the last 2-3 slots in the array, array[18 through 20], sometimes it's just the last 2, sometimes the last 3, never more than the last 3 slots though. I have tried many many negative numbers where the entire range stays negative and this happens to every one. Now if the range falls into positive, or even ends a 0 the randoms generate for the end of the array just fine and all goes well.
My other problem is that when the histogram generates an even 10, or multiple of 10, occurances for a specific number in the range, it will print out # signs then one *. So, if there are 10 occurances the histogram is showing 11, yet if there is 11 or 12, etc.. occurances then the histrogram prints out fine.
Any help and/or advice is greatly appreciated, sorry for the long post, my code is as follows:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
const int occurSize = 21;
int randomGenSize, lowerLimit, percentValue, belowRange = 0, aboveRange = 0;
int countOccurances[occurSize] = {0}, rangeNums[21];
int* randomNumbers = 0;
srand(time(0));
cout << "Please enter the lower limit of the range: ";
cin >> lowerLimit;
cout << "How many random numbers would like generated (1-10000): ";
cin >> randomGenSize;
if(randomGenSize > 10000)
{
cout << "Number entered does not fall between 1 and 10000.\n\n";
cout << "Please enter a number between 1 and 10000: ";
cin >> randomGenSize;
}
for(int z = 0; z < 21; z++) //create boundary array
{
rangeNums[z] = lowerLimit;
lowerLimit++;
}
randomNumbers = new int[randomGenSize];
for(int a = 0; a < randomGenSize; a++) //create random numbers 85% within range, 15% within or outside range
{
percentValue = rand() % 100 + 1;
if(percentValue <= 85)
{
if(lowerLimit >= 0)
{
randomNumbers[a] = rand() % ((rangeNums[20] + 1) - rangeNums[0]) + rangeNums[0];
}
else if(lowerLimit < 0)
{
randomNumbers[a] = rand() % ((rangeNums[20] - 1) - rangeNums[0]) + rangeNums[0];
}
}
else
{
randomNumbers[a] = rand() - 16000;
}
if(randomNumbers[a] < rangeNums[0]) //count numbers below and above range boundaries
{
belowRange++;
}
else if(randomNumbers[a] > rangeNums[20])
{
aboveRange++;
}
}
for (int b = 0; b < randomGenSize; b++) //count the occurances of each number in the range
{
for(int c = 0; c < occurSize; c++)
{
if(randomNumbers[b] == rangeNums[c])
{
countOccurances[c]++;
}
}
}
cout << "\n\nQuantity Below Range: " << belowRange << "\n\n";
for(int d = 0; d < occurSize; d++) //create the histogram
{
cout << setw(5) << rangeNums[d] << setw(3) << "(" << countOccurances[d] << ")" << setw(3);
for(int e = 0; e < countOccurances[d]; e++)
{
while(countOccurances[d] >= 10)
{
cout << "#";
countOccurances[d] -= 10;
}
if(countOccurances[d] < 10)
{
cout << "*";
}
}
cout << endl;
}
cout << "\nQuantity Above Range: " << aboveRange << "\n\n\n";
return 0;
}