Search code examples
cstructcomplex-numbers

Complex numbers using struct


I want to write a program that reads an array of complex numbers until 0+0j is entered and calculates the absolute value of these numbers and gives the maximum value.

  1. i create a struct which includes im and re.
  2. i take numbers from user and check it whether it is equal to 0+0j
  3. i send the inputs array to absc function
  4. in absc function i created a new array which is equal to sqrt(re^2+im^2) and i send this new array to another function find_max
  5. in find_max i find the max value of absolute array.
  6. Then i print the max value.

However, i fail and i don't understand where should correct.

#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;
}

Solution

  • #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;
    }