Hi everyone
I am absolutely freaked out because of one of homework assignment project. I am writing codes to fill three vectors with data conditions: in order, random order and reverse order.
In-order vector works fine, but I am having problem with running the program for random ordered vector and reverse vector.
The question is like this:
Write a C++ program that
1. gets a data set size, n, from cin,
2. fills three vectors with doubles in order, randomly ordered doubles, and doubles in reverse order,
3. checks the vectors to see if they are sorted,
4. uses the Standard Template Library sort routine to sort the vectors into ascending order, and
5. checks the results to see if they are sorted.
Can you help me find where I have error in my code?
my code is as follows :
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
#include<algorithm>
using std::sort;
#include<string>
using std::string;
#include<vector>
using std::vector;
void fill_orderedVector(vector<double> &num)
{
double maxnumsize;
cout<<"Enter maxnumsize:";
cin>>maxnumsize;
while(cin.fail())
while(std::cin.fail())
{
std::cout << "Invalid Entry\nEnter 0-9" << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
std::cin >> maxnumsize;
}
cout<<"enter maxnumsize:"<<endl;
for(int i=1;i<=maxnumsize;i++)
{
num.push_back(i);
}
cout<<"vector before sorting:"<<endl;
vector<double>::iterator iter;
for(iter=num.begin();iter!=num.end();iter++)
cout<<*iter<<endl;
}
void fill_randomVector(vector<double> data)
{
double randomVsize;
cout<<"Enter random vector size:";
cin>>randomVsize;
for (int i=1;i<=randomVsize;i++)
{
data.push_back((rand()/(double)RAND_MAX)*randomVsize);
}
cout<<"vector before sorting:"<<endl;
vector<double>::iterator iter;
for(iter=data.begin();iter!=data.end();iter++)
cout<<*iter<<endl;
if(!is_sorted(data.begin(), data.end()))
sort(data.begin(), data.end());
cout<<"vector after sorting:"<<endl;
for(int i=0;i<data.size();i++)
{
cout<<data[i]<<endl;
}
}
void fill_reverseVector(vector<double> &revVector)
{
double reverseVsize;
cout<<"Enter reverse vector size:";
cin>>reverseVsize;
for (int i=1;i<=reverseVsize;i++)
{
revVector.push_back(reverseVsize-i+1);
}
cout<<"vector before sorting:"<<endl;
vector<double>::iterator iter;
for(iter=revVector.begin();iter!=revVector.end();iter++)
cout<<*iter<<endl;
}
int main()
{
vector<double>vec1(0);
cout<<"the ordered vector vec1"<<endl;
fill_orderedVector(vec1);
vector<double>vec2;
cout<<"The random vector vec2"<<endl;
fill_randomVector(vec2);
vector<double>vec3(0);
cout<<"The reverse vector vec3"<<endl;
fill_reverseVector(vec3);
}
|