Histogram Graphing through input file

My C++ class is working on arrays right now and I'm trying to create a histogram that looks like this:
0-100: *****
201-300: * (where the asterisks = the numbers)
Etc.
Up to 1000. All of the numbers come from input files. Here is one:
normal_dist.txt
500
396
88
616
447
351
501
787
413
405
260
507
350
134
498
524
846
334
496
162
397
198
628
549
299
612
601
613


Here is the code I have so far, it doesn't work the way I want to.
Numbers[] is the array that works, and int count works as well, I just don't know how to go about making the histogram.


const int ARRAY_SIZE = 1000;
int main()
{
//const int ARRAY_SIZE = 1000; // Array size.
int numbers[ARRAY_SIZE];
int count = 0; // Array with elements
// Loop counter variable.

// For the user to enter the file name.
string filename;
string outputname;
ifstream inputFile; // Input the file stream object.
ofstream Output;
// Get the file name from the user.

cout << "Enter the name of the file you wish to open: ";
cin >> filename;

inputFile.open(filename);
// Open the file.
if (inputFile) {
while (count < ARRAY_SIZE && inputFile >> numbers[count]) {
count++;
}
inputFile.close();

cout << "\nEnter the name of the output file for the data: ";
cin >> outputname;
outputname += ".txt";
Output.open(outputname.c_str());
Output << "Input file: " << filename << endl;
Output << "Output file: " << outputname << endl;

Integer_Data(numbers, count, Output);
}
else {
cout << "Error opening the file." << endl << endl;

}

cout << "Programmed by: " << PROGRAMMER_NAME << " -- ";
cout << __DATE__ << " " << __TIME__ << endl;
cout << endl;
system("pause");

Output.close();

return 0;
}
void Histogram(int numbers[], int count, ofstream &Output) {

int i = count;
cout << " 0-100: ";
for (i = 1; i <= count % 100; ++i) {
cout << "*";
}
cout << endl << "101-200: ";
for (i = 101; i <= count % 200; ++i) {
cout << "*";
}
cout << endl << "201-300: ";
for (i = 201; i <= count % 300; ++i) {
cout << "*";
}
cout << endl << "301-400: ";
for (i = 301; i <= count % 400; ++i) {
cout << "*";
}
cout << endl << "401-500: ";
for (i = 401; i <= count % 500; ++i) {
cout << "*";
}
cout << endl << "501-600: ";
for (i = 501; i <= count % 600; ++i) {
cout << "*";
}
cout << endl << "601-700: ";
for (i = 601; i <= count % 700; ++i) {
cout << "*";
}
cout << endl << "701-800: ";
for (i = 701; i <= count % 800; ++i) {
cout << "*";
}
cout << endl << "801-900: ";
for (i = 801; i <= count % 900; ++i) {
cout << "*";
}
cout << endl << "901-1000: ";
for (i = 901; i <= count % 1000; ++i) {
cout << "*";
}
cout << endl;
}

That is only the part of the program with the histogram.
Hi, can you please use the code format tool? It'll make it so much easier for us to see what's going wrong in your program. Also, are you trying to represent a quantity of number with asterisks? For example if there are 6 numbers in the 0-100 range, would it look something like

0-100: ******


 
Integer_Data(numbers, count, Output); // What is this suppose to do? 
Last edited on
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
const int ARRAY_SIZE = 1000;
int main()
{
//const int ARRAY_SIZE = 1000; // Array size.
int numbers[ARRAY_SIZE];
int count = 0; // Array with elements
// Loop counter variable.

// For the user to enter the file name.
string filename;
string outputname;
ifstream inputFile; // Input the file stream object.
ofstream Output;
// Get the file name from the user.

cout << "Enter the name of the file you wish to open: ";
cin >> filename;

inputFile.open(filename);
// Open the file.
if (inputFile) {
while (count < ARRAY_SIZE && inputFile >> numbers[count]) {
count++;
}
inputFile.close();

cout << "\nEnter the name of the output file for the data: ";
cin >> outputname;
outputname += ".txt";
Output.open(outputname.c_str());
Output << "Input file: " << filename << endl;
Output << "Output file: " << outputname << endl;

Histogram(numbers, count, Output);

//Integer_Data(numbers, count, Output);
}
else {
cout << "Error opening the file." << endl << endl;

}

cout << "Programmed by: " << PROGRAMMER_NAME << " -- ";
cout << __DATE__ << " " << __TIME__ << endl;
cout << endl;
system("pause");

Output.close();

return 0;
}
void Histogram(int numbers[], int count, ofstream &Output) {

int i = count;
cout << " 0-100: ";
for (i = 1; i <= count % 100; ++i) {
cout << "*";
}
cout << endl << "101-200: ";
for (i = 101; i <= count % 200; ++i) {
cout << "*";
}
cout << endl << "201-300: ";
for (i = 201; i <= count % 300; ++i) {
cout << "*";
}
cout << endl << "301-400: ";
for (i = 301; i <= count % 400; ++i) {
cout << "*";
}
cout << endl << "401-500: ";
for (i = 401; i <= count % 500; ++i) {
cout << "*";
}
cout << endl << "501-600: ";
for (i = 501; i <= count % 600; ++i) {
cout << "*";
}
cout << endl << "601-700: ";
for (i = 601; i <= count % 700; ++i) {
cout << "*";
}
cout << endl << "701-800: ";
for (i = 701; i <= count % 800; ++i) {
cout << "*";
}
cout << endl << "801-900: ";
for (i = 801; i <= count % 900; ++i) {
cout << "*";
}
cout << endl << "901-1000: ";
for (i = 901; i <= count % 1000; ++i) {
cout << "*";
}
cout << endl;
}


Integer_Data is just another function in my program, I commented it out here.

Yes, that's what I'm trying to do with the numbers. For every number in 0-100, it would represent an asterisk, and continue to do that until 1000
1-100:
101-200:
201-300:
301-400:
401-500:
501-600:
601-700:
701-800:
801-900:
901-1000:
And whatever numbers are in those categories would display as an asterisk in that range.
Last edited on
OK, Sarah. Here is how I would approach it. I would have ten variables.
1
2
3
4
5
6
7
8
9
10
int _1_100 = 0; // begin the var name with an underscore because c++ does not allow them to start with a number
int _101_200 = 0;
int _201_300 = 0;
int _301_400 = 0;
int _401_500 = 0;
int _501-600 = 0;
int _601_700 = 0;
int _701_800 = 0;
int _801_900 = 0;
int _901_1000 = 0;


As you are reading from the file, use if to see if the input falls into a specific range. If it does, you want to increment the variable name that it falls under.

So let's say we read 10 numbers that fell into the 901-1000 range. We would print it like this

1
2
3
4
for(int i = 0; i < _901_1000; i++)
{
     cout << "*"; // this will print until i is no longer less than _901_1000, which means it will print 10 times
}
Registered users can post here. Sign in or register to post.