Home > other >  complex number calculator in c using functions and structs
complex number calculator in c using functions and structs

Time:10-30

Kind of a continuation of my last post, I'm trying to write a complex number calculator using structs and functions. My program has to have a function for reading in complex numbers from user input, and it has to have another function for adding them. This is the function prototype I was given:

Complex read_complex(void)

This is the prototype I have to use and it can't be changed. Right now I'm trying trouble passing the values I scan in from the above function into my function for adding the complex numbers. This is my code:

#include <stdio.h>
#include <math.h>
#include<string.h>

typedef struct Complex_ {
    double RealPart;
    double ImagPart;
} Complex;

Complex read_complex(void);
Complex add_complex(Complex z1, Complex z2);
Complex mul_complex(Complex z1, Complex z2);

int main(void) {
    char ent[50];
    Complex user1, user2;

    printf("Enter Add for addition, Mult for multiplication, MA for magnitude and angle, or Exit to quit: ");
    scanf("%s", ent);


    if (ent[0] == 'A') {
        read_complex();
        add_complex(user1, user2);
    }
    else if (ent[0] == 'M' && ent[1] == 'u') {
        read_complex();
        mul_complex(user1, user2);
    }
    else if (ent[0] == 'M' && ent[1] == 'A') {
        read_complex();
    }
    else {
    
    }

    return(0);
}

Complex read_complex(void) {
    Complex* user1;
    Complex* user2;

    printf("Enter first complex number: ");
    scanf("%lf %lf", &user1->RealPart, &user1->ImagPart);
    printf("Enter the second complex number: ");
    scanf("%lf %lf", &user2->RealPart, &user2->ImagPart);

    return;
}

Complex add_complex(Complex z1, Complex z2) {
    Complex z3;

    z3.RealPart = z1.RealPart   z2.RealPart;
    z3.ImagPart = z1.ImagPart   z2.ImagPart;

    printf("(%lf   %lfi)   (%lf   %lfi) = %lf   %lfi", z1.RealPart, z1.ImagPart, z2.RealPart, z2.ImagPart, z3.RealPart, z3.ImagPart);;

    return(z3);
} 

Complex mul_complex(Complex z1, Complex z2) {
    Complex z3;

    z3.RealPart = z1.RealPart * z2.RealPart;
    z3.ImagPart = z1.ImagPart * z2.ImagPart;

    return(z3);
}

(Large parts of the code are incomplete right now because I'm just trying to figure out the adding part). The current problem I'm having is that when I run the code, I get an error saying the user1 and user2 variables are uninitialized, and I don't know how to initialize struct variables.

CodePudding user response:

You seem to have a misunderstanding of how pointers work.

The way you've written read_complex, you seem to think that the pointers user1 and user2 in this function automatically refer to the variables of the same name in the main function. That's not how C works.

What you actually have is two uninitialized pointers inside of read_complex that you then attempt to dereference via the -> operator. You're also not returning anything from this function even though it's declared to do so.

This function is supposed to return a single copy of a Complex, so you should create one locally, read the values into it, then return it. The return value should then be assigned to a local variable. So this also means you need to call this function for each Complex you read.

Also, you may want to review how to multiply complex numbers.

CodePudding user response:

This is the function prototype I was given: Complex read_complex(void) This is the prototype I have to use and it can't be changed.

read_complex() returns 1 object of type Complex. Use it to read 1, not 2, complex numbers.

Let use use that function to read and also create another function that calls it to read 2 complex numbers.

// Receive the address of 2 objects, 
// so this code knows where to save the result.
void read_complex2(Complex *a, Complex *b) {
  printf("Enter first complex number: ");
  *a = read_complex();
  printf("Enter the second complex number: ");
  *b = read_complex();
}

read_complex()

read_complex() becomes simple: read 2 double. Check results.

Complex read_complex(void) {
  Complex x;
  if (scanf("%lf %lf", &x.RealPart, &x.ImagPart) != 2) {
    fprintf(stderr, "Invalid input.\n");
    exit(EXIT_FAILURE);
  }
  return x;
}

main()

Adjust like:

if (ent[0] == 'A') {
  //read_complex();
  read_complex2(&user1, &user2);// Pass in addresses of objects.
  add_complex(user1, user2);
  ...

CodePudding user response:

Consider this function:

Complex read_complex(void) {
    Complex* user1;
    Complex* user2;

    printf("Enter first complex number: ");
    scanf("%lf %lf", &user1->RealPart, &user1->ImagPart);
    printf("Enter the second complex number: ");
    scanf("%lf %lf", &user2->RealPart, &user2->ImagPart);

    return;
}

You declare two pointers to Complex, and then access their fields. But you haven't actually allocated anything for those pointers to point to. You'd want to dynamically allocate them, or non-dynamically allocate them.

Complex *user1 = malloc(sizeof(Complex));
Complex user1;

Your other issue is that you don't return anything. You probably only wanted to read one Complex number at a time, and then return it by value based on your function signature.

  • Related