LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 10-23-2007, 12:15 PM   #1
yewjingcho
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Rep: Reputation: 0
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..
 
Old 10-23-2007, 12:24 PM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
We are not here to do the homeworks
 
Old 10-23-2007, 12:26 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 10-23-2007, 12:31 PM   #4
yewjingcho
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 10-23-2007, 12:42 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
This should refresh your memory:
http://www.cplusplus.com/

ta0kira
 
Old 10-23-2007, 03:13 PM   #6
ehawk
Senior Member
 
Registered: Jul 2003
Posts: 1,257

Rep: Reputation: 48
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.
 
Old 10-25-2007, 10:55 AM   #7
yewjingcho
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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;
}
 
Old 10-25-2007, 11:20 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 10-25-2007, 11:33 AM   #9
yewjingcho
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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..
 
Old 10-25-2007, 12:53 PM   #10
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
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;
}
 
Old 10-27-2007, 02:05 PM   #11
dragon86
LQ Newbie
 
Registered: Oct 2007
Posts: 1

Rep: Reputation: 0
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.
 
Old 10-30-2007, 04:39 AM   #12
yewjingcho
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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;
}
 
Old 10-30-2007, 10:17 AM   #13
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 200

Rep: Reputation: 30
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.
 
Old 11-01-2007, 03:35 PM   #14
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
Exclamation

http://www.cplusplus.com/doc/tutoria...structure.html
http://cs.fit.edu/~mmahoney/cse2050/how2cpp.html
http://www.cppreference.com

And please hit the pound symbol (#) after highlighting your code so that it will insert [ CODE ] [ /CODE ] around the actual code.

So this:

#include <iostream>
int main() {
cout << "I are program" << endl;
}


will look like this
Code:
#include <iostream>
int main() {
     cout << "I are program" << endl;
}
 
Old 11-01-2007, 07:05 PM   #15
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
WARNING: way of topic.

Quote:
Originally Posted by student04 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write data to CD or DVD by program with C/C++ linux_hy Programming 4 12-15-2006 02:12 AM
C program advice write files exvor Programming 9 10-16-2006 08:47 AM
show me how to write a daemon program? shrike_912 Programming 2 06-04-2004 02:28 PM
i want to write game program,shall i know which knowledge ywchen2000 Linux - Games 3 04-18-2004 09:00 AM
Write to port (C program) dummyagain Programming 8 10-24-2003 11:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration