Connecting Tech Pros Worldwide Help | Site Map

WHy my program doesn't work

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 24th, 2007, 10:15 AM
Duardo Mattheo
Guest
 
Posts: n/a
Default 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


  #2  
Old March 24th, 2007, 11:15 AM
Bo Persson
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

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


  #3  
Old March 24th, 2007, 12:35 PM
Colander
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

On Mar 24, 11:07 am, "Duardo Mattheo" <dudordoo123...@yahoo.dkwrote:
Quote:
#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;
}

  #4  
Old March 24th, 2007, 01:05 PM
Colander
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

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

  #5  
Old March 24th, 2007, 03:35 PM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

On 2007-03-24 14:02, Colander wrote:
Quote:
On Mar 24, 1:30 pm, "Colander" <colan...@gmail.comwrote:
Quote:
>On Mar 24, 11:07 am, "Duardo Mattheo" <dudordoo123...@yahoo.dkwrote:
>>
Quote:
#include<iostream.h>
#include<stdlib.h>
void main(void)
{
unsigned int u=int(1 000 000 000*rand()%RAND_MAX);
cout<<"Money summed = "<<(u);
>>
Quote:
}
>>
Quote:
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
  #6  
Old March 24th, 2007, 03:35 PM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

On 2007-03-24 12:09, Bo Persson wrote:
Quote:
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
  #7  
Old March 24th, 2007, 03:55 PM
SasQ
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

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

--
SasQ
  #8  
Old March 24th, 2007, 05:05 PM
Adrian Hawryluk
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

Erik Wikström wrote:
Quote:
#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).
Quote:
);
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/]______\/
  #9  
Old March 25th, 2007, 01:25 AM
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
 
Posts: n/a
Default Re: WHy my program doesn't work

On 2007-03-24 18:02, Adrian Hawryluk wrote:
Quote:
Erik Wikström wrote:
Quote:
>#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
 

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.