histogram for data

Hello, I'm very new to programming, been doing it only for a few weeks. My college professor kind of likes to throw us out on our own without a lot of instruction, so I've been kind of doing this on my own. I've gotten a bit through it, but she wants us to make a histogram chart to show the categories of packages that appear when the data set: 4.1 9.5 5.0 5.1 10.0 9.4 3.2 0.0 is entered into the input

So like there should be a chart that looks something like this:

Histogram of category counts:
----------------------------
1
2 ***
3 **
4 ***


However I have no idea how to make a for loop for this with the data and variables that I have currently.

Any help would be amazing, thanks!

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
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
//Title
cout<< "Statistics by me"<<endl<< endl;

	//All variables for loop
double weights;
double sum=0;
double count=0;
double average;

//Variables for if statements
int category;
int category1 =1;
int category2 =2;
int category3 =3;
int category4 =4;
double shipping;
double shipping1=2.75;
double shipping2=4.50;
double shipping3=6.75;
double shipping4=8.25;

//Input for program
cout <<"Enter Package Weights (0 to quit): ";
cin >>weights;

// If no packages are entered, displays as so.
if (weights==0){
	cout << "No packages entered.";
 system("pause"); return 1;
}

//Top of table
cout <<setw(13)<<left<<"Category"<<setw(13)<<"Weight(Lbs.)"<<setw(13)<<"Shipping"<<endl;
cout <<"---------------------------------"<<endl;

cout<<fixed<<showpoint<<left<<setprecision(2);

while (weights !=0)
{
	count++;
// If to determine category of package, based on weight
if (weights <=2.0){ category=category1;}
if (2.0< weights && weights <= 5.0) {category=category2;}
if (5.0<weights && weights <=8.0){category=category3;}
if (8.0<weights && weights <=10.0){category=category4;}

// If to determine shipping cost of package, based on weight
if (weights <=2.0){ shipping=shipping1;}
if (2.0< weights && weights <= 5.0) {shipping=shipping2;}
if (5.0<weights && weights <=8.0){shipping=shipping3;}
if (8.0<weights && weights <=10.0){shipping=shipping4;}

	cout<<setw(15)<<category<<setw(15)<<weights<<"$"<<shipping<<endl;
	sum= sum+shipping;
	cin>>weights;

}

cout<<"Total number of packages:"<<count<<endl;
if (count>0)
{
	average=double(sum)/count;
	cout<<"Average weight of all packages:"<<average<<endl;
}

cout<<"Total shipping charges:"<<"$"<<sum<<endl;
cout<<"Lightest weight:"<<endl;
cout<<"Heaviest weight:"<<endl<<endl;
cout<<"Histogram of category counts:"<<endl;
cout <<"---------------------------------"<<endl;

system("pause");
return 0;
}
you need a way to store what you have processed, how about an array?

up before the while loop:
int histo[4] = {0};

then as you process items, you can count them in a running total..

in the while loop:
histo[category-1] ++; //where category is 1-4 ....

and after the while loop, try a double for loop.

int j,k;
for(j = 0; j < 4; j++)
{
for(k = 0; k < histo[j]; k++)
cout << '*';
cout << endl; //this is in the outer loop..
}


you can add some prettys to make the output say what category or whatever else.


Topic archived. No new replies allowed.