Hi there!
I have the following code which generates a population of N Gaussian numbers.
Code:
#include <stdio.h>
#include<math.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int iseed,i,j,N,l,m,k,y,z;
float a,b,e,stand,c,d,sum=0.00;
printf("Enter the number of Gaussion numbers needed\n");
scanf("%d", &N);
printf("Enter the standard deviation (larger or equal 1) \n");
scanf("%f", &stand);
z=(6*stand);
y=0-z;
int values[6+z+1];
for(j=0; j<6+z+1; j++) {
values[j] = 0; /* sets whole array set to 0 */
}
iseed=1934871;
srand(time(NULL));
for (j=0; j<N; j++) {
for (i=0; i<=12 ; i++)
{
a=(float)rand();
e=a/(float)RAND_MAX;
sum = sum + e;
}
b=sum-6;
c=stand*b;
for (l=y-1; l<=z; l++){ /* For loop starting at a value of -6xstandard dev and going to =6xstandard dev */
if((l-0.5)<=c) /* If C falls withing +/- 0.5 of the current value of l then we add 1 to the array values */
d=c;
if(d<(l+0.5)) {
values[l+z] += 1;
break;
} /* if the if statement is satisfied exit the loop */
}
sum=0.00;
}
for (m=y;m<0;++m){
printf("%d %d",m,values[m+z]);
for(k=0; k<(values[m+z]);++k) {
printf("%c",'*');
}
printf("\n");
}
for (m=0;m<=z;++m){
printf(" %d %d",m,values[m+z]);
for(k=0; k<(values[m+z]);++k) {
printf("%c",'*');
}
printf("\n");
}
return 0;
}
The programme currently generates a * every time an element is read in the array values[]. However, as I am using large population sizes it would be better if the programme generated a * for every n times the element is read. Could someone provide assistance on how to do this? Many Thanks!