471,591 Members | 1,757 Online

# Generating random numbers in reverse order???

94
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers in reverse order as follows "list[i] = 25.88 - sqrt(i)" and perform all my sorting functions no this list. I have successfully run my program on numerous lists of random numbers.. I just have no clue how to get a reverse generated list of random numbers.

Any help is appreciated. Thanks.
Nov 9 '06 #1
5 5261
dav3
94
still looking for any help on this. Its got me bamboozeled.
Nov 10 '06 #2
sicarie
4,677 Expert Mod 4TB
still looking for any help on this. Its got me bamboozeled.
Are you sure you need a list of random numbers? Anything where you have a set seed value and algorithm to produce the number will not be a "random" number.

But it sounds like you need to perform the operation 25 times, with i being 25->0.
Nov 10 '06 #3
macklin01
145 100+
I'm not entirely sure what you're asking for.

Do you want a list of random numbers, but each successive number should be smaller? e.g.,

1239.295
99.482
83.218
47.671
14.23148
12.184
11.19847
8.193
8.0582
6.2

etc.?

Well, here's the simplest way I can think of to do that:

float largest_number = 10000;
float scale = largest_number;

for( int i=0; i < total_count ; i++ )
{
list[i] = scale * rand() / (float) RAND_MAX;
scale = list[i];
}

In this way, the next random number will be between 0 and the previous number.

Is this the type of thing that you're looking for? -- Paul
Nov 10 '06 #4
dav3
94
After emailing my instructor it seems I am supposed to generate the list as follows

list[0] = 25.88 - sqrt(0) = 25.88
list[1] = 25.88 - sqrt(1) = 24.88
list[2] = 25.88 - sqrt(2) = 24.4658

however I am having issues getting my program running. I get this error and am not sure what to make of it

Error 1 error C2676: binary '[' : 'orderedArrayListType<elemType>' does not define this operator or a conversion to a type acceptable to the predefined operator

The code that generates said error is this:

Expand|Select|Wrap|Line Numbers
1. #include "stdafx.h"
2. #include <iostream>
3. #include "orderedArrayListType.h"
4. #include <time.h>
5. #include <math.h>
6. using namespace std;
7.
8. //double sqrt(double, double);
9.
10. int _tmain(int argc, _TCHAR* argv[])
11. {
12.     double i;
13.
14.     int maxSize;
15.
16.     cout << "Please enter the size of the list: 10, 100 or 1000"<<endl;
17.     cin >> maxSize;    //User determined size of list
18.
19.     cout<<"Testing selection sort!!!"<<endl;
20.     orderedArrayListType<int>list(maxSize);
21.     cout<<"Size of list = "<<list.maxListSize();
22.
23.     for(i = 0; i < maxSize; i++)
24.     {
25.         list[i] = 25.88 - sqrt(i);
26.     }
27.
28.         cout<<"The list before sorting:"<<endl;
29.         list.print();
30.         cout<<endl;
31.
32.         list.selectionSort();
33.
34.         cout<<"The list after sorting:"<<endl;
35.         list.print();
36.         cout<<endl;
37.
38.         cout << "Selection sort: Number of comparisons = "<<list.getSortComparisons()<<endl;
39.         cout << "Selection sort: Number of swaps = "<<list.getSortSwaps()<<endl;
40.         cout<<endl;
41.
42.
43.     return 0;
44. }
45.
The specific line where the problem is
Expand|Select|Wrap|Line Numbers
1. for(i = 0; i < maxSize; i++)
2.     {
3.         list[i] = 25.88 - sqrt(i);
4.     }
5.
Nov 11 '06 #5
dav3
94
found my mistake. Thank you all who gave advice:)
Nov 11 '06 #6