Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用struct的复数_C_Struct_Complex Numbers - Fatal编程技术网

使用struct的复数

使用struct的复数,c,struct,complex-numbers,C,Struct,Complex Numbers,我想写一个程序,读取复数数组,直到输入0+0j,然后计算这些数字的绝对值,并给出最大值 我创建了一个包含im和re的结构 我从用户那里获取数字,并检查它是否等于0+0j 我将输入数组发送到absc函数 在absc函数中,我创建了一个等于sqrt(re^2+im^2)的新数组,并将此新数组发送给另一个函数find_max 在find_max中,我找到绝对数组的最大值 然后我打印最大值 然而,我失败了,我不知道应该在哪里改正 #include<stdio.h> #include<m

我想写一个程序,读取复数数组,直到输入0+0j,然后计算这些数字的绝对值,并给出最大值

  • 我创建了一个包含im和re的结构
  • 我从用户那里获取数字,并检查它是否等于0+0j
  • 我将输入数组发送到absc函数
  • 在absc函数中,我创建了一个等于sqrt(re^2+im^2)的新数组,并将此新数组发送给另一个函数find_max
  • 在find_max中,我找到绝对数组的最大值
  • 然后我打印最大值
  • 然而,我失败了,我不知道应该在哪里改正

    #include<stdio.h>
    #include<math.h>
    #define SIZE 5
    
    struct stComplex
    {
        int re, im; 
    }; 
    
    typedef struct stComplex Complex; 
    
    float absc(float[]);
    float find_max(float[]);
    
    int main()
    {
        Complex inputs[SIZE]; //Array for inputs
    
        int i;
        float max;
    
        for(i = 0; i < SIZE; i++
            printf("Enter real part of complex number: ");
            scanf("%d", &inputs[i].re);
            printf("Enter imaginary part of complex number: ");
            scanf("%d", &inputs[i].im);
    
            if(inputs[i].re != 0 and inputs[i].im != 0)
            {
                continue;
            }
            else
            {
                return 1;
            }   
        }
    
        max = absc(Complex inputs[SIZE]); //Sending the real and imaginary parts to calculate absolute value
    
        printf("The biggest absolute value is %f", max);
    
        return 0;
    }
    
    float absc(Complex x[SIZE]) 
    {
        int i;
        float absolute[SIZE], max; 
        for(i = 0; i < SIZE; i++)
        {
            absolute[i] = sqrt(pow(inputs[i].re, 2) + pow(inputs[i].im, 2));
        }
        max = find_max(absolute[SIZE]); 
    
        return max; 
    }
    
    float find_max( float array[SIZE] )
    {
        int i, max;
        max = array[0];
        for( i = 1; i < SIZE; i++ ) 
        {
            if( max < array[ i ] )
                max = array[ i ];
        }
        return max;
    }
    
    #包括
    #包括
    #定义尺寸5
    结构stComplex
    {
    int-re,im;
    }; 
    typedef结构stComplex;
    浮动absc(浮动[]);
    float find_max(float[]);
    int main()
    {
    复杂输入[SIZE];//输入数组
    int i;
    浮动最大值;
    对于(i=0;i
    #包括
    #包括
    #定义尺寸5
    结构stComplex
    {
    int-re,im;
    }; 
    typedef结构stComplex;
    浮点absc(复杂输入[]);
    float find_max(float[]);
    int main()
    {
    复杂输入[SIZE];//输入数组
    int i;
    浮动最大值;
    对于(i=0;i这看起来更像C,而不是C++,所以我不能用C++的新的C<代码>复数< /代码>类型来做这个“但是,我失败了”。怎么?你的输入、预期输出和实际输出是什么?我必须用结构简化来做到:<代码>绝对[i]=qRT(POW(输入[i],RE,2)+PoW(输入[i],IM,2))
    -->
    绝对[i]=hypot(输入[i].re,输入[i].im);
    不会像预期的那样将数组传递给函数,而是传递位于
    SIZE
    位置的数组元素。
    #include<stdio.h>
    #include<math.h>
    #define SIZE 5
    
    struct stComplex
    {
        int re, im; 
    }; 
    
    typedef struct stComplex Complex; 
    
    float absc(Complex inputs[]);
    float find_max(float[]);
    
    int main()
    {
        Complex inputs[SIZE]; //Array for inputs
    
        int i;
        float max;
    
        for(i = 0; i < SIZE; i++)
        {
            printf("Enter real part of complex number: ");
            scanf("%d", &inputs[i].re);
            printf("Enter imaginary part of complex number: ");
            scanf("%d", &inputs[i].im);
    
            if(inputs[i].re != 0 && inputs[i].im != 0)
            {
                continue;
            }
            else
            {
                return 1;
            }   
        }
    
        max = absc(inputs); //Sending the real and imaginary parts to calculate absolute value
    
        printf("The biggest absolute value is %f", max);
    
        return 0;
    }
    
    float absc(Complex inputs[SIZE]) 
    {
        int i;
        float absolute[SIZE], max; 
        for(i = 0; i < SIZE; i++)
        {
            absolute[i] = sqrt(pow(inputs[i].re, 2) + pow(inputs[i].im, 2));
        }
        max = find_max(absolute); 
    
        return max; 
    }
    
    float find_max( float array[SIZE] )
    {
        int i, max;
        max = array[0];
        for( i = 1; i < SIZE; i++ ) 
        {
            if( max < array[ i ] )
                max = array[ i ];
        }
        return max;
    }