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!


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";
}
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 :)
Topic archived. No new replies allowed.