473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 5672
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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
5
by: Muffin | last post by:
I hope somebody can show me why I need to have a messagebox to get "random" numbers in my example. If I comment out the message box call that is in RollAbility() the numbers generated are not...
9
by: Tuxedo | last post by:
I'd like to reorganize the third, fourth, fifth and sixth, as well as any elements thereafter in an array in random order: var a = new...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.