473,385 Members | 1,343 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,385 software developers and data experts.

WHy my program doesn't work

#include<iostream.h>
#include<stdlib.h>
void main(void)
{
unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
cout<<"Money summed = "<<(u);
}

thank you,
Duardo

Mar 24 '07 #1
8 1624
Duardo Mattheo wrote:
:: #include<iostream.h>
:: #include<stdlib.h>
:: void main(void)
:: {
:: unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
:: cout<<"Money summed = "<<(u);
:: }
::
:: thank you,
:: Duardo

Depending on the machine you have, 1 billion times some value is highly
likely to be larger than what an unsigned int can store.
Bo Persson
Mar 24 '07 #2
On Mar 24, 11:07 am, "Duardo Mattheo" <dudordoo123...@yahoo.dkwrote:
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
cout<<"Money summed = "<<(u);

}

thank you,
Duardo
Because you have to write

'std::cout' where you wrote 'cout'.

Because you can't write a number with spaces in it.

Because main has to return int.

So the next will work:
#include<iostream>
#include<stdlib.h>
int main(void)
{
unsigned int u=int(1000000000*rand()%RAND_MAX);
std::cout<<"Money summed = "<<(u);

return 0;
}

Mar 24 '07 #3
On Mar 24, 1:30 pm, "Colander" <colan...@gmail.comwrote:
On Mar 24, 11:07 am, "Duardo Mattheo" <dudordoo123...@yahoo.dkwrote:
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
cout<<"Money summed = "<<(u);
}
thank you,
Duardo

Because you have to write

'std::cout' where you wrote 'cout'.

Because you can't write a number with spaces in it.

Because main has to return int.

So the next will work:
#include<iostream>
#include<stdlib.h>
int main(void)
{
unsigned int u=int(1000000000*rand()%RAND_MAX);
std::cout<<"Money summed = "<<(u);

return 0;

}
(Replying to one selfs, what does the world do to me)

Now that we have the systax right, we can look at wat you are trying
to do.

I guess you want a random number between 0 or 1 and 1000000000.

This is not what the programme does....

Please lookup srand and rand in your manual.

srand is a function that will give you a new/different random number
each time you run your progrogramme.

Doing a modulo operation makes sure that a number is in a range, in
your case the range will be [0, RAND_MAX), and not [0, 1000000000).

Good luck

Mar 24 '07 #4
On 2007-03-24 14:02, Colander wrote:
On Mar 24, 1:30 pm, "Colander" <colan...@gmail.comwrote:
>On Mar 24, 11:07 am, "Duardo Mattheo" <dudordoo123...@yahoo.dkwrote:
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
cout<<"Money summed = "<<(u);
}
thank you,
Duardo

Because you have to write

'std::cout' where you wrote 'cout'.

Because you can't write a number with spaces in it.

Because main has to return int.

So the next will work:
#include<iostream>
#include<stdlib.h>
int main(void)
{
unsigned int u=int(1000000000*rand()%RAND_MAX);
std::cout<<"Money summed = "<<(u);

return 0;

}

(Replying to one selfs, what does the world do to me)

Now that we have the systax right, we can look at wat you are trying
to do.

I guess you want a random number between 0 or 1 and 1000000000.

This is not what the programme does....

Please lookup srand and rand in your manual.

srand is a function that will give you a new/different random number
each time you run your progrogramme.

Doing a modulo operation makes sure that a number is in a range, in
your case the range will be [0, RAND_MAX), and not [0, 1000000000).
And I'd like to point out that the range of the values output by rand
already is [0, RAND_MAX], after all that is what RAND_MAX means, the max
number that rand can output. So the modulo (%) operator is not needed.
Perhaps a / was intended so that the value will be between 0 and 1000000000?

Notice also that you need to seed rand before usage, or there's a great
chance that it will return the same value each time you run the
application, you can use the current time to get a quite good (but not
cryptographically secure) seed:

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));
unsigned int nr = static_cast<unsigned int>(1000000000 *
rand() / double(RAND_MAX)
);
std::cout << nr;
return 0;
}

--
Erik Wikström
Mar 24 '07 #5
On 2007-03-24 12:09, Bo Persson wrote:
Duardo Mattheo wrote:
:: #include<iostream.h>
:: #include<stdlib.h>
:: void main(void)
:: {
:: unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
:: cout<<"Money summed = "<<(u);
:: }
::
:: thank you,
:: Duardo

Depending on the machine you have, 1 billion times some value is highly
likely to be larger than what an unsigned int can store.
It will probably be enough on a modern 32-bit machine. What the OP
should watch out for is the fact that he/she is casting the result to
int but assigning to an unsigned int.

--
Erik Wikström
Mar 24 '07 #6
Dnia Sat, 24 Mar 2007 05:30:55 -0700, Colander napisa³(a):
#include<stdlib.h>
#include <cstdlib>
std::cout<<"Money summed = "<<(u);
Why 'u' in parenthesis?

--
SasQ
Mar 24 '07 #7
Erik Wikström wrote:
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));
unsigned int nr = static_cast<unsigned int>(1000000000 *
rand() / double(RAND_MAX)
I don't think that will work. Assuming that the largest value is about
4Gib, you will overflow most of the data that rand() outputs when
multiplying it by 1000000000, loosing most of your resolution. The
following should work:

unsigned int nr = static_cast<unsigned int>(
(rand() * rand()) % 1000000000)

Assuming that RAND_MAX*RAND_MAX >= 1000000000-1 (in actual math, not on
a CPU's integer arithmetic set) this should work. And although (rand()
* rand()) could overflow, it wouldn't matter as that information is not
needed when trying to get a value in the range [0, 1000000000).
);
std::cout << nr;
return 0;
}
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 24 '07 #8
On 2007-03-24 18:02, Adrian Hawryluk wrote:
Erik Wikström wrote:
>#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
srand(time(0));
unsigned int nr = static_cast<unsigned int>(1000000000 *
rand() / double(RAND_MAX)

I don't think that will work. Assuming that the largest value is about
4Gib, you will overflow most of the data that rand() outputs when
multiplying it by 1000000000, loosing most of your resolution. The
following should work:

unsigned int nr = static_cast<unsigned int>(
(rand() * rand()) % 1000000000)
I was more thinking along the lines of this (notice the added parentheses):
unsigned int nr = static_cast<unsigned int>(1000000000 *
(rand() / double(RAND_MAX))

It will not be able to produce all values in the range 0-1000000000, but
then again, with that range who would notice :-)

--
Erik Wikström
Mar 25 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: SK | last post by:
I am calling an exe thru' href, but when it executes, I get the message if I want to open the file(exe file). Is there any way I can suppress this from appearing and open the program? Thank...
6
by: William J. Leary Jr. | last post by:
I'm running a VB6 program, the IDE for a compiler. Paste doesn't work if the text was cut/copied from within the program. That is: 1. Cut/copy inside the program, can't paste. Program:...
17
by: Gladiator | last post by:
When I am trying to execute a program from "The C Programming Language" by Dennis Ritchie, I tried to run the following program.I am using Dev++ as a compiler software. The Program is presented...
29
by: tele-commuter | last post by:
Hi folks, I want to understand how exactly is an image(compiled c code and loaded into memory) stored in memory. What exactly is a linker script? I work with a lot of c code on a daily...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
13
by: John Salerno | last post by:
If I want to write my code in a separate text editor (I like UltraEdit) but then press a single button to have that code run in the IDLE environment, is that possible? I know that you can configure...
1
by: Marty | last post by:
I need to catch exceptions thrown by programs started by the os.system function, as indicated by a non-zero return code (e.g. the mount utility). For example, if I get the following results in a...
4
by: Billy | last post by:
Hi! I'm using: -Compiler: Borland Command Line C++ Compiler 5.5.1 -Code Editor: SciTE 1.75 Anybody know how we can in Scite with command Tools|Go (F5) compile and run my c or cpp program at...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.