Filling random numbers from cin of vector size

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);

	
	
    


}




Last edited on
It's not clear to me what the program is trying to accomplish. Do you want three vectors, each of different length? The problem definition suggests only one size for all the vectors.

Also, why are you using the loop index in your calculation of the random values?

And, I don't see any evidence of calling a sort routine. The first vector is pre-sorted, because of the way you're creating and populating it (using the loop index to determine the element contents).
thanx..well sort routine is the part of question but that wasn't problem i faced. the only focus i m having right now is filling the vectors. size really doesn't matter. i want to fill the three vectors with three condion: ordered, random and reverse .
So...are you asking how to use the STL sort?

Have you read:

http://www.cplusplus.com/reference/algorithm/sort/

It's fairly straightforward, though template functions can look a little daunting at first. Try implementing it on one of your vectors, and report back with your problems/results.
no i have no problem using sort. when i run the program it prints the element of ordered vector and when i input the size for random vector it doesn't print the elements of random vector, and the program crashes error is something like this :
"vector iterator not derefrenceable "

and the program crashes


this is the problem..
Last edited on
Can we see the code for your sorts?
yes u can see it on fill_randomVector method. I have edited the code . have a look at it?
Topic archived. No new replies allowed.