|
|
Subject:
C++ PROGRAMING
Category: Computers > Programming Asked by: purplepit-ga List Price: $40.00 |
Posted:
28 Mar 2003 05:18 PST
Expires: 27 Apr 2003 06:18 PDT Question ID: 182263 |
Using the Rand() function, can anyone answer this question, into a program that runs Part 1 As a first stage, develop and test your own random number function to generate random numbers within a range. The function will have 2 integer arguments to indicate the low and high values of the range. Thus a call cout << random(1,6); would display a value between 1 and 6 (inclusive) - just like a normal 6-sided die. I suggest you use simple assertions to ensure that the lower limit is not less than zero and the upper limit is not less than the lower limit. [This is a thinking problem. There is not an example in the notes to show you how to do this. Try and work out the algorithm on paper first.] Part 2 Write a program that declares 2 lists of integers l1 & l2. Using your random() function, generate 10 random numbers between 1 and 10 and store them in l1, and another 10 between 1 and 6 (yes of course there will be duplicates!) and store them in l2. Sort both lists using the sort() function, and then use the merge() member function to leave the combined list in l1. Display each list before and after merging. You will have to work out how to use merge() from the (very brief) details in the notes. [I assume you can only merge lists that are sorted. You might like to investigate what happens if either or both lists is not sorted.] Part 3 We have used the following code several times to display a list: for (it1 = l1.begin(); it1 != l1.end(); it1++) { cout << *it1 << ", "; } Clearly it should be possible to write a small function display() which can be called using a call display(l1) . To make life a bit more interesting we could allow the display() function to be defined with arguments to specify leading and trailing braces and a user-definable separator. Thus display(l1, '[', ']', ':') would display a sample list l1 as [1:2:3:4:5] The first argument of this function would be of type list<int> Modify the program to use such a function (i.e. you must write this display() function!) each time we display a list. (We shall see later a better way of doing this.) My output looks like this (I have used different characters to display the merged list to make it look different): Display the original lists ... l1 : {2:8:5:1:10:5:9:9:3:5} l2 : {6:6:2:4:2:6:2:3:4:1} And now the merged list ... Merged lists: [1 1 2 2 2 2 3 3 4 4 5 5 5 6 6 6 8 9 9 10] |
|
Subject:
Re: C++ PROGRAMING
Answered By: theta-ga on 29 Mar 2003 07:49 PST Rated: |
Hi purplepit-ga, The wording of your question left me slightly confused as to whether we were allowed to use the C/C++ library's rand() function or not. The first line says we can, while in Part 1 of your question you say that we are to implement our own. So, since I had time to kill, I implemented two versions of the solution, one which uses the rand() library function and one which doesn't. You can download a zip file containing the answer from http://www31.brinkster.com/tanm/GA/Random.zip The archive contains two folders, named [With Rand] and [Without Rand]. Each folder contains three files, [Part1.cpp],[Part2.cpp] and [Part3.cpp], which address the three parts of your question. Here is an explaination of the solution : To get a random number between a given range, you can take the following steps: - First generate a random number using any random number algorithm. You can use the C++ rand() function, or define you own (see below) - Calculate the range of numbers you are allowed. For example if you are asked to generate a random number between 2 and 8, the range of numbers is 8-2+1=7. - Performs a modulo operation on the random number generated in the first step, with the range you calculated in the second step. For eg., say the first step generated a random number N. In the second step, you got the range 7. So, on performing the operation N % 7, we will get a number from 0 to 6, depending on the value of N. - Take the value we got above, and add it to the lower limit of the range, which in our example is 2. So, the value we get in this step will range from 2+0 to 2+8, i.e from 2 to 8. This is exactly the range we were given, so our purpose is accomplished. If we are not allowed to used the inbuilt C++ rand() function, then we will have to write our own pseudo random generator. The solution to part 1 of your question remains the same. The only change is that in the first step, instead of calling the C++ rand() function, you call the one you wrote. If we are to write our own version of the C++ rand() function, we will need a suitable algorithm. There are a large number of algorithms available, employing a variety of computational techniques. For our needs, we will use a very simple Linear Congruential algorithm. For a simple and easy to understand explaination of this methodology, see this article: - How Computers Generate Random Numbers: Chapter 4 ( http://home1.gte.net/deleyd/random/random4.html ) Part 2 and Part 3 of your question are are pretty straight-forward. Please see the comments in the relevant cpp files. If you need more clarifications or modifications to the code, please do not hesitate to ask. I will be glad to help. Please do not rate this answer until you are completely satisfied with it. Regards, Theta-ga :-) =========================== Google Search Terms Used : c++ linear congruential random number algorithms |
purplepit-ga
rated this answer:
and gave an additional tip of:
$10.00
Excellent Answer!! Thank you very much for your help. Anthony |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |