小编D.L*_*.L.的帖子

在C中生成随机整数,不在范围内

我想用随机的x和y坐标生成100个节点.但我不想指定任何范围.像rand(100)将只生成1到100之间的数字.但是我希望数字分布在一个大的区域,我希望它们是随机的.我如何使用C实现它?我试过了:

 int gen_rand_position(void)  
{  
     int i,j,a[100],b[100];  
    for(i=0,j=0;i<100,j<100;i++,j++)  
{
    x=rand();  
        y=rand();  
    a[i]=x;  
    b[j]=y;  
}  
}
Run Code Online (Sandbox Code Playgroud)

这不是随意选择的.我可以拥有更高效的随机功能吗?

c random

2
推荐指数
1
解决办法
542
查看次数

如何用C中的牛顿拉夫森方法检测一个循环

牛顿拉夫森方法的失效分析表明,"对于某些函数,一些起点可能进入无限循环,阻止收敛".我想在程序中检查它是否进入无限循环或不使用assert语句.如果它进入,那么程序将终止说使用这个初始猜测无法收敛.如何在程序中检测此循环?码:

int user_power, i=0, cnt=0, flag=0;
int coef[10]={0};
float x1=0, x2=0, t=0;
float fx1=0, fdx1=0;

void main()
{
    printf("\n\n\t\t\t PROGRAM FOR NEWTON RAPHSON GENERAL");
    printf("\n\n\n\tENTER THE MAXIMUM POWER:");
    scanf("%d",&user_power);

    for(i=0;i<=user_power;i++)
    {
        printf("\n\t x^%d:",i);
        scanf("%d",&coef[i]);
    }

    printf("\n");
    printf("\n\tINTIAL X1---->");
    scanf("%f",&x1);

    printf("\n ******************************************************");
    printf("\n ITERATION    X1    FX1    F'X1  ");
    printf("\n **********************************************************");

    do
    {
           cnt++;
           fx1=fdx1=0;
           for(i=user_power;i>=1;i--)
           {
                fx1+=coef[i] * (pow(x1,i)) ; //calculating f(x1)
           }
           fx1+=coef[0];
           for(i=user_power;i>=0;i--)
           {
                fdx1+=coef[i]* (i*pow(x1,(i-1))); //calculating f'(x1)
           }
           t=x2;
           assert(fdx1!=0);
           x2=(x1-(fx1/fdx1));

           x1=x2;

           printf("\n %d         %.3f  %.3f  %.3f ",cnt,x2,fx1,fdx1); …
Run Code Online (Sandbox Code Playgroud)

c assertions numerical-analysis

1
推荐指数
1
解决办法
654
查看次数

scala定义的命名约定

我试图在scala中定义名称+ I,如下所示.

def +I(i1: Int, i2: Int): Int = { .....}
Run Code Online (Sandbox Code Playgroud)

它说'='预期但找到了标识符.如果我做I_ +,它可以工作,但为了方便使用,我想要+我.是否不能为定义命名?

scala

1
推荐指数
1
解决办法
66
查看次数

标签 统计

c ×2

assertions ×1

numerical-analysis ×1

random ×1

scala ×1