### Horizontal Histogram

Hi all,
I am writing a program which takes in an ArrayList of numbers to create a horizontal histogram of the numbers' frequency. The first two numbers typed in will be the range of the histogram and all the other numbers thereafter are graphed with a '*'. Each time I try running the program, I get a Segmentation fault (core dumped) error. In the end, it should look something like this:

<1 3 1 1 2 2 2 3 3 3>
1| **
2| ***
3| ****

If anyone has time, would you be able to help me solve the Segmentation fault error and help me see any other errors in my code? I greatly appreciate your time and effort, thank you!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172`` ``````/** * 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 #include 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"; }``````
Last edited on
` for(int i = 1; 1 < largestNum`
1 < largestNum? You probably meant i.

Also, remember that arrays start at index 0, not 1.
So, this, for example, might be wrong: `int largestNum = nums[1];`
if nums[0] is actually the greatest number.

__________________________

If you're getting a segmentation fault, it means you're going out of bounds of an array somewhere. Use a debugger or gratuitous print statements and try to figure out exactly where the segfault is happening.
Last edited on
Oh, I didn't catch that! Thank you, I will update that and check my array bounds. I appreciate the help :)