Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
10-23-2007, 12:15 PM
|
#1
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Rep:
|
write a C++ program
anyone can help with this question?
Q: Write a C++ language program to perform the following task.
generate a sample of 1000 pseudo-random sequences in a range of 0-000000 to 1.000000 using multiplication congruential method.
The equation used in this method is :
Xi+1= 125Xi (mod m)
where m is a positive numbers. the equation reads using the last number generated in the sequences ,Xi multiply it by 125 , divide the result by m and use the remainder of the division process as Xi+1
Urgent please help..
|
|
|
10-23-2007, 12:24 PM
|
#2
|
Member
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200
Rep:
|
We are not here to do the homeworks
|
|
|
10-23-2007, 12:26 PM
|
#3
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
yewjingcho, we are not here to do your homework. Please read the forum rules. We can help indirectly, but please do not ask us to directly do your work for you.
If you show what you have so far and ask specific questions then we can help to guide you, but you have to show that you are at least trying.
If you are totally stuck and have no idea where to start, you should talk to your course tutor.
|
|
|
10-23-2007, 12:31 PM
|
#4
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Original Poster
Rep:
|
i learnt c++ years ago and now i forgot how to write a program with it. that's why i am asking for help here. definitely no idea.
|
|
|
10-23-2007, 12:42 PM
|
#5
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep:
|
This should refresh your memory:
http://www.cplusplus.com/
ta0kira
|
|
|
10-23-2007, 03:13 PM
|
#6
|
Senior Member
Registered: Jul 2003
Posts: 1,257
Rep:
|
My programming experience in c++ was too long ago to produce the code you want.
Here is a good description (section 3) with some snippets of code:
http://russell.vcharite.univ-mrs.fr/...rap/random.pdf
http://www.ncsa.edu/UserInfo/Resourc.../pessl146.html
http://www.coyotegulch.com/products/...oad/index.html
There is a section referring to linear multiplicative congruential srand calls for C++ on that page.
A discussion and the same code is presented here:
http://cer.freeshell.org/renma/Libra...omNumber_sec_c
http://en.wikipedia.org/wiki/Linear_...tial_generator
For problems of numerical computing, I would highly recommend you look at this book.
http://worldcat.org/isbn/0521750334
It presents cookbook examples of just about every numerical computing task you could imagine. It is extremely popular, and you can find it in many technical library offerings, get it through inter-library loan, or purchase it at many large bookstore chains.
Here is what the free software people at GNU offer in terms of documentation for the free software package descriptions for random numbers
http://www.gnu.org/software/gsl/manu...ributions.html
http://www.gnu.org/software/gsl/
The link mentioned below provides a plethora of online C++ books and tutorials, to refresh you memory.
http://www.mathtools.net/C_C__/Books...als/index.html
The first program on this page seems to be very related to what you are doing, but is probably overkill. By following its example, you should probably be able to code a program that at least compiles and runs:
http://www.firstpr.com.au/dsp/rand31...r-carta.cc.txt
The package MLCG uses a multiplicative linear congruential generator. Again, overkill.
The second like is the full description of it
http://www.cs.ru.nl/A.vanWeelden/index.php?p=downloads
http://aips2.nrao.edu/docs/aips/impl...CG:description
I found this information googling for c++ random number sequences multiplicative congruential method.
Hopefully, between this information, what you can learn of basic c++ coding, and your teacher and classmates, you can make progress.
|
|
|
10-25-2007, 10:55 AM
|
#7
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Original Poster
Rep:
|
this is what i have done. but still errors and i dont know where are them. anyone please help??
#include <cmath>
#include <iostream>
using namespace std;
int main()
const int z[i];
float z[i];
for (int i=1; i<1001; i++)
{
x[i+1] = 125*x[i]%m;
z[i] = x[i+1];
}
for (int i=1; i<1001; i++)
{
cout << " x " << i << z[i] << endl;
return 0;
}
|
|
|
10-25-2007, 11:20 AM
|
#8
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
Please post sourcecode in [code] tags to preserve formatting and aid readability.
There are a number of serious problems with this: - main is a function, but you didn't enclose the body of the function in { braces }.
- When you declare the array z, you use i to specify the length, but i is not defined at that point in the program.
- x is never declared, but is used.
I think you need to start with an even simpler program to understand the basic minimal structure of a program in C++.
Last edited by matthewg42; 12-15-2007 at 07:18 PM.
|
|
|
10-25-2007, 11:33 AM
|
#9
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Original Poster
Rep:
|
i am not learning programming. but still i have an assignmnet regarding c++. can you help me on this question? still i dont know where are the errors..
|
|
|
10-25-2007, 12:53 PM
|
#10
|
Member
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200
Rep:
|
Like I'm not learning how to cook but I am cooking.
I have no idea what is an upper limit of the generated reminder but I guess it's either m or the given 125. You should know. Also I did not know how to initialize the x[0]. But that should be enough for you ...
Code:
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int i;
float z[1001];
int x[1001];
int m = 123;
x[0] = 100234;
for (i = 1; i < 1001; i++) {
x[i] = (125 * x[i-1]) % m;
z[i] = (float)x[i] / (float)m;
}
for (i = 1; i < 1001; i++) {
cout << " z[" << i << "] = " << z[i] << endl;
}
return 0;
}
|
|
|
10-27-2007, 02:05 PM
|
#11
|
LQ Newbie
Registered: Oct 2007
Posts: 1
Rep:
|
Code:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main ()
{
char prev;
ofstream outfile("random.txt");
int x[1001];
x[0] = 1;
for (int i=0;i<1000;i++)
{x[i+1] = 125 * x[i] * 1000000 % 613818;
outfile << "0.";
outfile.width (6); //6 decimal places
prev = outfile.fill ('0');
outfile << x[i+1] << endl;
cout.fill(prev);
x[i] = x[i+1] % 10; //find the last number
if (x[i]==0) //if the last is 0,
x[i] = x[i+1] % 100; //find the second last
else
x[i] = x[i+1] % 10;
}
return 0;
}
anything wrong?
i use the last generated number as new x[i]
fixed to 6 decimal place
Last edited by dragon86; 10-27-2007 at 02:07 PM.
|
|
|
10-30-2007, 04:39 AM
|
#12
|
LQ Newbie
Registered: Oct 2007
Posts: 5
Original Poster
Rep:
|
thanks for help. now i want divide the number generated in the question above into 20 intervals from 0 to 1. i need to calculate the frequency of the number generated in each interval. i used the code below but it doesn't seems to work. why?
int a,b,c,d,e,f,g,h,ii,j,k,l,mm,n,o,p,q,r,s,t
{
if(0<z[i]&&z[i]<0.05)
a++;
if(0.05<z[i]&&z[i]<0.1)
b++;
if(0.1<z[i]&&z[i]<1.5)
c++;
if(0.15<z[i]&&z[i]<2)
d++;
if(0.2<z[i]&&z[i]<2.5)
e++;
if(0.25<z[i]&&z[i]<3)
f++;
if(0.3<z[i]&&z[i]<3.5)
g++;
if(0.35<z[i]&&z[i]<4)
h++;
if(0.4<z[i]&&z[i]<4.5)
ii++;
if(0.45<z[i]&&z[i]<5)
j++;
if(0.5<z[i]&&z[i]<5.5)
k++;
if(0.55<z[i]&&z[i]<6)
l++;
if(0.6<z[i]&&z[i]<6.5)
mm++;
if(0.65<z[i]&&z[i]<7)
n++;
if(0.7<z[i]&&z[i]<7.5)
o++;
if(0.75<z[i]&&z[i]<8)
p++;
if(0.8<z[i]&&z[i]<8.5)
q++;
if(0.85<z[i]&&z[i]<9)
r++;
if(0.9<z[i]&&z[i]<9.5)
s++;
if(0.95<z[i]&&z[i]<1.00)
t++;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << e << endl;
cout << f << endl;
cout << g << endl;
cout << h << endl;
cout << ii << endl;
cout << j << endl;
cout << k << endl;
cout << l << endl;
cout << mm << endl;
cout << n << endl;
cout << o << endl;
cout << p << endl;
cout << q << endl;
cout << r << endl;
cout << s << endl;
cout << t << endl;
}
|
|
|
10-30-2007, 10:17 AM
|
#13
|
Member
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200
Rep:
|
Please look again at the structure of that simple programs that we posted before. Where did you put your new piece of code? Where is 'i' variable coming from in your code? You don't need to have all that alphabet of variables. Use another array and the loop. In the for loop just increase some variable by 0.5.
|
|
|
11-01-2007, 07:05 PM
|
#15
|
Member
Registered: Oct 2005
Posts: 970
Rep:
|
WARNING: way of topic.
Quote:
Originally Posted by student04
And please hit the pound symbol (#) after highlighting your code so that it will insert [ CODE ] [ /CODE ] around the actual code.
|
Can I have a big sack of #, erm no make that pounds £'s lol
I personally just use the very old method of typing two tags, I didn't even know this board had a feature like this.
|
|
|
All times are GMT -5. The time now is 09:02 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|