You are right...my thoughts were different...my sorry :(
Printable View
Ok, I cleaned up the code and took your advice. If I understood various nuances and pet peeves, I'd be a better programmer. I do welcome any structural comments. My averages though still yield zero. I'm trying to understand what am I missing here. Do you think the problem is with the printf function?
Here is the edited code:
Code:#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;
}
The average is not an integer, so why do you declare the variable as such, and then print it as an integer? :)
See, this is why it takes me so much time to do this. I can't see simple stuff like that. I corrected that to double. But the code is still obviously wrong because I can't get a negative average and all the averages must be between 0 and 1.
Code:#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; //defines i and average as integers
double x, y, ran, sum [15], average;
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;
}
Well, based on what you described in post #8, I am inclined to suggest something radical: ditch the current structure of your code.
Rather, create an array of 1000 doubles (let's name this numbers). Populate numbers with random numbers in the range [0.0, 1.0). Create another array of 1000 doubles to hold the powers of these random numbers (let's name this powers). Populate powers with 1s.
Now, implement this algorithm:
You could of course do this slightly differently, e.g., instead of populating powers with 1s, you populate it as a copy of numbers, and then modify the power computation algorithm to account for this.Code:for i in the range [1, 15]
for j in the range [0, 1000)
powers[j] = powers[j] * numbers[j]
sum the powers
compute the average
print the average
agrrrhr I see so many duplications of effort!!!!
Hi Lase...
A lot of this is in what you get used to, of course. There may be some common convention, but I've never been big on "banging the same gong" in the first place ;) The thing is, for some reason I just can't clearly see that leaging brace right after the bracket... so I put it where I can see it.
Thanks for the response.
Have you worked with other programmers on a project, especially one where more than one programmer might work on the same source file? If so, then you can recognise the benefit of "banging the same gong", at least for each given source file. Code formatting tools can alleviate this problem, but they also complicate the development process where other tools (e.g., file differencing tools) are concerned.Quote:
Originally Posted by CommonTater
You can use another common convention, e.g.,Quote:
Originally Posted by CommonTater
Code:for (i= 0; i<15; i++)
{
sum[i]+=y;
y=y*x;
}
Not yet. So far this is just a big ole time waster for me (read: "hobby") so differences in style have never been an issue... yet.
I actually started out that way... but it seemed like a waste of space so I eventually shifted over to the way I do it now... I suppose if it ever became an issue I could adapt.Quote:
You can use another common convention, e.g.,
Code:for (i= 0; i<15; i++)
{
sum[i]+=y;
y=y*x;
}
Here's a sample of what my code looks like... As you can see I format the text very carefully and comment the heck out of everything. I'm hoping to come back to this in 5 years and still be able to interpret it...
Code:// add program to list
BYTE TrackerAddProgram(HWND Handle)
{ TCHAR cl[MAX_PATH] = {0}; // command line
BYTE x; // counter
// prevent seeing self
if(Handle == MainWind)
return 0;
// find empty slot
for (x = 1; x <= Setting.Programs; x++)
{ if (Programs[x] == NULL) // slot found
{ // get memory
Programs[x] = malloc(sizeof(PGMINFO));
memset(Programs[x],0,sizeof(PGMINFO));
// get program handle
Programs[x]->Program = Handle;
// get the program name
GetPgmName(Programs[x]->PgmName);
// signal completion
PulseEvent(LaunchEvent);
AddToLog(L"Run",Programs[x]->PgmName);
return x; } }
AddToLog(L"Error",L"Too many programs");
return 0; }