Extra value in Histogram

I built this program in Visual Studio 2010 and it needed to display a histogram of the values input from the command line. I did that but there is an extra value appearing that was not imput at the command line when I did
"program.exe 1 2 3 5 3 7 8 9 4 7 9 3 2 1 "
Anyone have any idea where it is and why I am getting it??
Thanks
Here is my code

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int main(int argc,char* argv[])
{
//make vector
vector<int> numVector;
vector<int>::iterator pos,pos2;

//fill vector
int i;
int vectorsize=(argc-1);
for(i=0;i<(argc-1);i++)
{
numVector.push_back(atoi(argv[i]));
}
numVector.push_back(i);
sort(numVector.begin(),numVector.end());

// find and print minimum and maximum elements
pos = min_element (numVector.begin(), numVector.end());
pos2=max_element(numVector.begin(), numVector.end());
cout << "The minimum value is: " << *pos << endl;
cout<< " The maximum value is: "<<*pos2<<endl;
//find the arithmetic mean
double sum = 0;
for(int i=0;i!=numVector.size();i++)
{
sum += numVector[i];
}
//cout<<"The sum of the vector is "<<sum<<endl;
double mean=(sum/numVector.size());
cout<<"The arithmetic mean of the array is :"<<mean<<endl;
//find the median value of the vector
double median=0;
median=numVector[numVector.size()/2];
cout<<" The median of the the values is :"<<median<<endl;
//find variance
double variance=0;
for(int i=0;i!=numVector.size();i++)
{
double root=(numVector[i]-mean);
variance=variance+(root*root);
}
variance=variance/numVector.size();
cout<<" The variance of the set of values is :"<<variance<<endl;
//find the Standard Deviation
double deviation=sqrt(variance);
cout<<" The Standard Deviation of the st of values is :"<<deviation<<endl;
//map and print out the histogram
map<int, int> freq;
for ( size_t i = 0; i < numVector.size(); i++ )
++freq[numVector[i]];
map<int, int>::const_iterator it = freq.begin();
cout<<"Histogram"<<endl;

int t=0;

for(t=0;t<1;t++){
map<int, int>::const_iterator it = freq.begin();
while ( it != freq.end() ) {
cout<< it->first <<"| ";
int v=0;
for(v=0;v<it->second;v++) {
cout<< "-" ;

}
++it;cout<<endl;
}

}
}
This is a start:
I have indicated the updated lines
They may or may not be other errors


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
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int main(int argc,char* argv[])
{
    //make vector
    vector<int> numVector;
    vector<int>::iterator pos,pos2;

    //fill vector
    int i;
    int vectorsize=(argc-1);
    for(i=1;i<(argc);i++) //<<**************************  Note the change to i
    {
        numVector.push_back(atoi(argv[i]));
    }
   // numVector.push_back(i);//<<*************Get rid of this line
    sort(numVector.begin(),numVector.end());

    // find and print minimum and maximum elements
    pos = min_element (numVector.begin(), numVector.end());
    pos2=max_element(numVector.begin(), numVector.end());
    cout << "The minimum value is: " << *pos << endl;
    cout<< " The maximum value is: "<<*pos2<<endl;
    //find the arithmetic mean
    double sum = 0;
    for(int i=0;i!=numVector.size();i++)
    {
        sum += numVector[i];
    }
    //cout<<"The sum of the vector is "<<sum<<endl;
    double mean=(sum/numVector.size());
    cout<<"The arithmetic mean of the array is :"<<mean<<endl;
    //find the median value of the vector
    double median=0;
    median=numVector[numVector.size()/2];
    cout<<" The median of the the values is :"<<median<<endl;
    //find variance
    double variance=0;
    for(int i=0;i!=numVector.size();i++)
    {
        double root=(numVector[i]-mean);
        variance=variance+(root*root);
    }
    variance=variance/numVector.size();
    cout<<" The variance of the set of values is :"<<variance<<endl;
    //find the Standard Deviation
    double deviation=sqrt(variance);
    cout<<" The Standard Deviation of the st of values is :"<<deviation<<endl;
    //map and print out the histogram
    map<int, int> freq;
    for ( size_t i = 0; i < numVector.size(); i++ )
        ++freq[numVector[i]];
    map<int, int>::const_iterator it = freq.begin();
    cout<<"Histogram"<<endl;

    int t=0;

    for(t=0;t<1;t++){
        map<int, int>::const_iterator it = freq.begin();
        while ( it != freq.end() ) {
            cout<< it->first <<"| ";
            int v=0;
            for(v=0;v<it->second;v++) {
                cout<< "-" ;

            }
            ++it;cout<<endl;
        }

    }
}
Thanks for the iteration fix. I had tried dropping the second line but then it throws "vector iterator not dereferencable" and "standard C++ libraries are out of range" and I do not get what loop is doing it?
// numVector.push_back(i);//<<*************Get rid of this line This line adds an extra number into the vector which just makes nonsense of the
the actual values enetered on the commaned line.

I had tried dropping the second line but then it throws "vector iterator not dereferencable"

If you call the program without any parameters then the vector will be empty and you will get this error.
So get rid of that line, and put in a check to make sure that argc is at least 2 (that is the program.exe followed by one value.)
Put this check right at the start of main and abort if argc is less than 2.
Last edited on
Thanks alot that worked perfectly. Here is the completed code

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

int main(int argc,char* argv[])
{
if (argc<2)
{
cout<<"You did not enter some values:";

}
if(argc>1)
{
//make vector
vector<int> numVector;
vector<int>::iterator pos,pos2;

//fill vector
int i;
int vectorsize=(argc-1);
for(i=1;i<(argc);i++)
{
numVector.push_back(atoi(argv[i]));
}
//numVector.push_back(i);
sort(numVector.begin(),numVector.end());

// find and print minimum and maximum elements
pos = min_element (numVector.begin(), numVector.end());
pos2=max_element(numVector.begin(), numVector.end());
cout << "The minimum value is: " << *pos << endl;
cout<< " The maximum value is: "<<*pos2<<endl;
//find the arithmetic mean
double sum = 0;
for(int i=0;i!=numVector.size();i++)
{
sum += numVector[i];
}
//cout<<"The sum of the vector is "<<sum<<endl;
double mean=(sum/numVector.size());
cout<<"The arithmetic mean of the array is :"<<mean<<endl;
//find the median value of the vector
double median=0;
median=numVector[numVector.size()/2];
cout<<" The median of the the values is :"<<median<<endl;
//find variance
double variance=0;
for(int i=0;i!=numVector.size();i++)
{
double root=(numVector[i]-mean);
variance=variance+(root*root);
}
variance=variance/numVector.size();
cout<<" The variance of the set of values is :"<<variance<<endl;
//find the Standard Deviation
double deviation=sqrt(variance);
cout<<" The Standard Deviation of the st of values is :"<<deviation<<endl;
//map and print out the histogram
map<int, int> freq;
for ( size_t i = 0; i < numVector.size(); i++ )
++freq[numVector[i]];
map<int, int>::const_iterator it = freq.begin();
cout<<"Histogram"<<endl;

int t=0;

for(t=0;t<1;t++){
map<int, int>::const_iterator it = freq.begin();
while ( it != freq.end() ) {
cout<< it->first <<"| ";
int v=0;
for(v=0;v<it->second;v++) {
cout<< "-" ;

}
++it;cout<<endl;
}}

}
}
Topic archived. No new replies allowed.