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
//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<< "-" ;
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
usingnamespace 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.
//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<< "-" ;