# Thread: Complex numbers and C structures

1. ## Complex numbers and C structures

hello, i just began studying C structures and i got an exercice at the first days so i'm still not familiar with it. can anyone plz help.

The problem is as following:. Represent complex numbers )
• Implement all 4 functions (absolute value, addition, multiplication, negation)
• Do this in two different ways, using:
- Arrays(how can i do this?)
- Structures

I hope someone can help me with that

2. How about you suggest something, then we can comment on it. This will teach you a lot more than one of us giving you an immediate answer.

--
Mats

3. Normally classes have some kind of instruction leading up to an assignment, or they have a text-book that you can read to learn how to use the material.

I would suggest that you start by looking at your notes or reading the book. That's a great place to get started. Then once you have your program written, this forum exists to help you debug it. However, we cannot help you until you have tried to solve the problem on your own.

4. well i tried to do the addition fuction with typedef
Code:
```#include<stdio.h>

struct Complex {
float real;
float img;
};

Complex add_complex( Complex z1, Complex z2 ) {
Complex z3;
z3.real = z1.real + z2.real;
z3.img  = z1.img  + z2.img;
return z3;
}

int main() {
Complex z1, z2, z3;

printf( "Enter the first complex number \n");
printf( "Enter the real part " );
scanf( "&#37;f", &z1.real );
printf( "Enter the imaginary part " );
scanf( "%f", &z1.img );

printf( "Enter the second complex number \n");
printf( "Enter the real part " );
scanf( "%f", &z2.real);
printf( "Enter the imaginary part " );
scanf( "%f", &z2.img );

z3 = add_complex( z1, z2 );
//show the sum
printf( "sum: %f + i * %f", z3.real, z3.img );

getchar();
return 0;
}```

5. Well, you need a typedef, if that's what you mean. struct Complex just defines a new type called "struct Complex" -- if you want Complex, that's where the the typedef comes in. But I don't see anything (else) wrong here.

6. And how is that working out.

Technically, by the way, that code shouldn't compile in C since it uses the C++ "skip struct when you use it" mechanism. You can achieve the same thing in valid C by writing
Code:
```typedef struct tagComplex {
float real;
float img;
} Complex;```
[Technically, you don't have to add "tag" (or anything else) to the struct name, but it makes it more clear what is happening.

--
Mats

7. yes it runs normally, however i don't know how to do it with arrays if this questions can be answered plz help

8. My best guess would be that your instructor wants you to use

float complex[2];

for an array implementation of complex numbers.

9. A complex number is two numbers, right. How would you store two numbers as an array?

--
Mats

10. i see that make thing visual for me thanks i'll get to work now thanks

11. hello guys i got this exercice in which i need help. This program should fill two arrays made of information about students and employee. i have to sort alphabetically each of them by name and last name, still i have to check that no name should be repeated. In this program i tried to store the information in a table string"std" and in the same time check for any repeated name but i have problem with the sorting and when i run this it shuts down as the size ins't recognized even if i specified it.
Code:
```#include <stdio.h>
#include <conio.h>
#include <string.h>
#define SIZE 10;
typedef struct {
char *firstname;
char *lastname;
}student;

typedef struct {
char *lastname;
char *firstname;
float salary;
}employee;
void fill_1(student []);
void fill_s(student);
void fill_2(employee *);
void fill_e(employee []);
int search(student [],student );
int main()
{
student s[10];
student std;
fill_1(s);

}

void fill_e(employee *e)
{
scanf("&#37;s",&e->firstname);
scanf("%s",&e->lastname);
scanf("%f",&e->salary);

}

void fill_s(student *s)
{
scanf("%s",&s->firstname);
scanf("%s",&s->lastname);

}

void fill_1(student s[])
{
int i=0,test;
student std;
fill_s(&std);
s[0]=std;

do{
fill_s(&std);
if(search(s,std)==1) printf("no");
else s[i+1]=std;

i++;
printf("entrez 0,1");
scanf("%d", &test);

}while((i<10)&&(test==1));

}

void fill_2(employee e[])
{
int i=0,test;
do
{
fill_e(&e[i]);
printf("if you want to add another employee press 1 else press 0");
scanf ("%d",&test);
i=i+1;
}while((i<10)&&(test==1));

}
int search(student s[],student std)
{

for(int i=0;i<10;i++)
{
if ((strcmp(s[i].firstname,std.firstname)==0) && (strcmp(s[i].lastname,std.lastname)==0))
return 1;

} return 0;}```

12. i get many errors for sure

13. #define SIZE 10;
without the ;. Or else it will put everywhere it sees SIZE, 10; which will give you errors ofc.

EDIT:
You use scanf() wrongly in this code:
Code:
```void fill_s(student *s)
{
scanf("&#37;s",&s->firstname);
scanf("%s",&s->lastname);

}```
You dont need the &. Jst s->something. You declare s as a pointer to a student structure. That is why you use correctly the -> symbol. The & is wrong. It is used to give the address of a variable (rarely used with a pointer).

Post the errors if you want, I don't see anything else with a quick look, so if you still have errors say so to look the code more thoroughly. And run-time erros or it doesn't compile?

14. Really? I get none (compile errors).
But there are a couple of things you really should look into/change and add.
First of, scanf to read strings is unsafe. Do it properly or not at all. See my signature.