#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[])
{
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int i,average; //defines i and average as integers
int j;
double x, y;
double ran;
double sum [15];
for (i=0; i<15; i++)
sum [i]=0;
for (j=0; j<1000; j++)
{
//printf ("%u\n", rand ());
x=y=(rand() / (RAND_MAX + 1.0));
for (i=0; i<15; i++) //error here - averages should be less than 1
sum[i]+=y;
y=y*x;
}
printf("The Sum of Random Numbers : %d ", sum);
for (i=0; i<15; i++)
{
average=sum[i]/j; //j values retained from the loop (1000); alternatively: sum[15]
printf("The Average of Random Numbers : %d i=%d",average, i);
}
return 0;
}
9 replies to this topic
#1
Posted 04 September 2010 - 12:17 PM
Hey guys. I am trying to have a code that will output the average of random numbers between 0 and 1, meaning that the average will always be less than 1. I have an error in this code - missing brackets around the j loop, but I can't figure out what's wrong. I am new to coding and can't see the language as easily. Any help would be appreciated.
|
|
|
#2
Posted 04 September 2010 - 03:07 PM
Can you give an example of what you're expecting to happen? It seems you think there's a logic error, but it's not clear what you're trying to do.
#3
Posted 04 September 2010 - 03:25 PM
Ok, let's see if this makes it clear:
I take 1000 random numbers between 0 and 1. I need two things from here:
1. Generate the sum of all these numbers.
2. Generate the AVERAGE of these numbers like this:
a. First do a regular average: add up all 1000 numbers together and then divide by 1000.
b. Now do the same, except SQUARE all the random numbers, add them up, and only then divide by 1000 to get the average.
c. Do the same ...... except do the cube this time.
...
...
...
continue like this until you hit the power of 15. So raise all of the random numbers to the power of 15, add them, and then divide by 1000 again to get an average.
Please note that the average in all these cases is going to be less than one because you're adding numbers that are less than one and get progressively smaller as you increase the power to which they are raised. This means that the generated output for the average must ALWAYS be smaller than 1, and get progressively smaller for larger and larger powers raised. My program still doesn't do this.
I hope this makes sense.
I take 1000 random numbers between 0 and 1. I need two things from here:
1. Generate the sum of all these numbers.
2. Generate the AVERAGE of these numbers like this:
a. First do a regular average: add up all 1000 numbers together and then divide by 1000.
b. Now do the same, except SQUARE all the random numbers, add them up, and only then divide by 1000 to get the average.
c. Do the same ...... except do the cube this time.
...
...
...
continue like this until you hit the power of 15. So raise all of the random numbers to the power of 15, add them, and then divide by 1000 again to get an average.
Please note that the average in all these cases is going to be less than one because you're adding numbers that are less than one and get progressively smaller as you increase the power to which they are raised. This means that the generated output for the average must ALWAYS be smaller than 1, and get progressively smaller for larger and larger powers raised. My program still doesn't do this.
I hope this makes sense.
#4
Posted 04 September 2010 - 04:01 PM
This generates numbers on [0, 10] interval and then divides by 10 to get [0, 1.0] interval, since you can't use % operator with double datatype. Maybe with some ugly casting and such... but let's leave it simple.
#include <iostream>
#include <iomanip>
#include <time.h>
using namespace std;
int main (int argc, char *argv[]) {
srand ((unsigned)time(NULL));
double sum = 0;
double avg = 0;
double num;
for (int i = 0; i < 1000; ++i) {
num = rand() % 11;
num /= 10;
sum += num;
avg += num;
}
avg /= 1000;
cout << setprecision(1);
cout << "AVG: " << avg << endl;
cout << setprecision(4);
cout << "SUM: " << sum << endl;
return 0;
}
The power part I leave to you. :)
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#5
Posted 04 September 2010 - 07:21 PM
Also, you are NOT doing the powers within your for(i) loop, so all your sums will be identical.
#6
Posted 05 September 2010 - 09:39 AM
I have revised the code. If I could just resolve the averages portion, this might just work. I am still not sure where the error is.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (int argc, char *argv[])
{
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
int i, j, average; //defines i and average as integers
double x, y, ran, sum [15];
for (i=0; i<15; i++)
sum [i]=0;
for (j=0; j<1000; j++)
{
//printf ("%u\n", rand ());
x=y=(rand() / (RAND_MAX + 1.0));
for (i=0; i<15; i++) //error here - averages should be less than 1 (bracket)
{
sum[i]+=y;
y*=x;} //y=y*x
}
printf("The Sum of Random Numbers : %d ", sum);
for (i=0; i<15; i++)
{
average=sum[i]/j; //j values retained from the loop (1000); alternatively: sum[15]
printf("The Average of Random Numbers : %d i=%d\n",average, i);
}
return 0;
}
#7
Posted 05 September 2010 - 11:02 AM
x=y=(rand() / (RAND_MAX + 1.0));You're using wrong operator here; you want to get remainder so use % operator instead. Note that you also can't use modolus operator with double (float aswell I think) datatype.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#8
Posted 05 September 2010 - 11:08 AM
You mean like this:
And you're saying that would only work with int? Because I can't use int for a number between 0 and 1
x=y=(rand() % (RAND_MAX + 1.0));?
And you're saying that would only work with int? Because I can't use int for a number between 0 and 1
#9
Posted 05 September 2010 - 11:40 AM
Excelsius said:
You mean like this:
And you're saying that would only work with int? Because I can't use int for a number between 0 and 1
x=y=(rand() % (RAND_MAX + 1.0));?
And you're saying that would only work with int? Because I can't use int for a number between 0 and 1
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#10
Posted 08 September 2010 - 04:10 PM
I dunno, I just did this:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define DECIMAL_PLACES 2
#define _STR_PROC(x) # x
#define STRINGIFY(x) _STR_PROC(x)
int main(void)
{
int multInt = pow(10, DECIMAL_PLACES);
float multFloat = pow(10.0, DECIMAL_PLACES);
srand(time(NULL));
float someNum = (rand() % multInt) / multFloat;
printf("This number: %." STRINGIFY(DECIMAL_PLACES) "f\n", someNum);
return 0;
}
The key line is the fourth one in main():float someNum = (rand() % multInt) / multFloat;Since you're dividing an int by a float value, the compiler will infer that you want the integer to be "promoted" to a float value, then to perform the division operation. That seems to work for me.
Wow I changed my sig!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









