Histogram C++ (Values to Intervals)

I am trying to create a histogram by converting the values into intervals.
I am not sure how to go about this and running into many problems.

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
  #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

void arrSelectSort(int [], int);
void rowsOfTen(int [], int);
void displayAscend(int [], int);

int main()
{
    int *pointer = nullptr;
    srand(time(nullptr));
    int numbOfTests = rand() % (200 - 1 + 1) + 1;

    pointer = new int[numbOfTests];

    for(int i = 0; i < numbOfTests; i++)
    {
        pointer[i] = rand() % (99 - 55 + 1) + 55;
    }

    cout << "Ascending Order\n";
    cout << string(30,'-') << endl;

    arrSelectSort(pointer, numbOfTests);

    displayAscend(pointer, numbOfTests);
    cout << endl << endl;;
    cout << "Rows Of Ten\n";
    cout << string(30,'-') << endl;

    rowsOfTen(pointer, numbOfTests);


    //I am trying to get the intervals to display here
    cout << endl;
    cout << "(score intervals)\n";
    cout << "55-99|";
    cout << endl;
    cout << "60-64|";
    cout << endl;
    cout << "65-69|";
    cout << endl;
    cout << "70-74|";
    cout << endl;
    cout << "80-84|";
    cout << endl;
    cout << "85-89|";
    cout << endl;
    cout << "90-94|";
    cout << endl;
    cout << "95-99|";
    cout << endl;
    cout << "|----|----|----|----|----|----|----|----|\n";
    cout << "0    5    10   15   20   25   30   35   40\n";
    cout << "    (numbers of values in an interval)";
    

    delete [] pointer;

    return 0;
}

void arrSelectSort(int arr[], int size)
{
    int startScan, minIndex;
    int minElem;
    for(startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minElem = arr[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if((arr[index]) < minElem)
            {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
}

void displayAscend(int array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << " ";
    }
}

void rowsOfTen(int array[], int size)
{
    for(int cols = 0; cols < size; cols++)
    {
        for(int rows = 0; rows < 10; rows++)
        {
            cout << array[cols] << " ";
        }
        cout << endl;
    }

}
Your code is well organized for a beginner.

Here is a version that prints a histogram with a legend. I tried to add an explanation in the comments. Let me know if it isn't clear.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

void arrSelectSort(int [], int);
void rowsOfTen(int [], int);
void displayAscend(int [], int);

int main()
{
    int *pointer = nullptr;
    srand(time(nullptr));
    int numbOfTests = rand() % (200 - 1 + 1) + 1;

    pointer = new int[numbOfTests];

    for(int i = 0; i < numbOfTests; i++)
    {
        pointer[i] = rand() % (99 - 55 + 1) + 55;
    }

    cout << "Ascending Order\n";
    cout << string(30,'-') << endl;

    arrSelectSort(pointer, numbOfTests);

    displayAscend(pointer, numbOfTests);
    cout << endl << endl;;
    cout << "Rows Of Ten\n";
    cout << string(30,'-') << endl;

    rowsOfTen(pointer, numbOfTests);
    delete [] pointer;

    return 0;
}

void arrSelectSort(int arr[], int size)
{
    int startScan, minIndex;
    int minElem;
    for(startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        minElem = arr[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if((arr[index]) < minElem)
            {
                minElem = arr[index];
                minIndex = index;
            }
        }
        arr[minIndex] = arr[startScan];
        arr[startScan] = minElem;
    }
}

void displayAscend(int array[], int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << array[i] << " ";
    }
}

// Print a histogram
void rowsOfTen(int array[], int size)
{
    // First, get the min and max values
    int minVal = array[0];
    int maxVal = array[size-1];

    // Now get the width of each row. We'll print 8 rows.
    int width = (maxVal-minVal) / 8; // width of each bar

    // Initialize the high mark for the current row.
    // We initialize it to a low value so the first row
    // gets printed properly.
    int high = minVal;
    
    // Now go through the values and print '*' characters until we
    // hit the high mark.  At that point we'll start a new row.
    //we'll move on to the next row
    for (int i=0; i<size; ++i) {
	if (array[i] >= high) {
	    // This value is the start of a new row. Print the legend for it.
	    cout << '\n';
	    cout << high << '-' << (high+width-1) << ": ";

	    // And now increment the high mark
	    high += width;
	}
	// Print a '*' for this value
	cout << '*';
    }
}

A couple of points (re OP code):

L13, 17 can be combined:

 
const auto pointer {new int[numbOfTests]};


L15. As the value of NumberOfTests isn't going to change, this could be defined as const.

L68 startscan can be defined as part of the for statement

L72,73 minIndex and minElem can be defined here rather than earlier.

Topic archived. No new replies allowed.