for语句嵌套问题
#include<stdio.h>#include<stdlib.h>
#include<time.h>
float tjcs(int);
int main ()
{
int totalnumbers;
float percentage;
int totals;
int i,j;
printf("\nPlease enter the totalnumbers:");
scanf("%f",&totalnumbers);
printf("\nPlease enter the totals:");
scanf("%d",&totals);
for (j=1;j<=totals;j++)
{
percentage=tjcs(totalnumbers);
printf("The percentage of %d is %f",j,percentage);
}
}
float tjcs(int totalnumbers)
{
int randvalue;
int total=0;
int i;
float percentage;
srand(time(NULL));
for (i=1;i<=totalnumbers;i++)
{
randvalue=1+(int)rand() % 100;
if(randvalue>50)
total++;
}
percentage=total/totalnumbers;
return(percentage);
}
此程序是本人所编求在有totals种可能,且每种可能性均相同的概率事件中,连续模拟实验totalnumbers次时,每种可能的概率。
但运行时出现问题,求解答是何原因。