I am trying to do complex vector addition and dot product using structures in C for a project. I have my code written, however, while it is compiling without a problem, once i run my program it stops working. I have other parts to the program but this is only the relevant part. I am also trying to do matrix addition and multiplication with complex numbers. I think I can modify the others if I can get these working. Any help at all would be appreciated. Thanks, I appreciate it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define PI 3.14159265359
typedef struct complex
{
double re;
double im;
}complex;
typedef struct vect_complex
{
double *re;
double *im;
}vect_complex;
typedef struct mat_complex
{
//int i=0,j=0;
double re[100][100];
double im[100][100];
}mat_complex;
void vector_add(vect_complex a[4], vect_complex b[4], vect_complex c[4])
{
int i;
for(i=0; i<3 ;i++);
{
c->re[i] = a->re[i] + b->re[i];
c->im[i] = a->im[i] + b->im[i];
}
printf("Vector addition = (%f + %f*j)i + (%f +%f*j)j + (%f + %f*j)k\n\n",c- >re[0],c->im[0],c->re[1],c->im[1],c->re[2],c->im[2]);
}
void addmx(mat_complex *a, mat_complex *b, mat_complex *c)
{
int i, j;
for ( i = 0 ; i < '\0' ; i++ )
{
for ( j = 0 ; j < '\0' ; j++ )
{
c->re[i][j] = a->re[i][j] + b->re[i][j];
c->im[i][j] = a->im[i][j] + b->im[i][j];
}
}
printf("***Matrix Addition***\n");
for ( i = 0 ; i < '\0' ; i++ )
{
for ( j = 0 ; j < '\0' ; j++ )
{
printf("(%f + %f*j) ", c->re[i][j],c->im[i][j]);
}
printf("\n");
}
}
int main()
{
vect_complex aaa;
vect_complex bbb;
*aaa.re = 5;
*aaa.im = 4;
*bbb.re = 3;
*bbb.im = 2;
vect_complex ccc;
vector_add(&aaa, &bbb, &ccc);
vector_dot_prod(&aaa, &bbb, &ccc);
return 0;
}
There are a number of issues to deal with here.
1. vect_complex
Perhaps you’d like your
vect_complexto hold a fixed number of elements, in which case it should be defined:Alternatively, you can keep your current definition but you will need to allocate new arrays every time you use the structure:
2. vector_add
This function should be taking references to
vect_complexas inputs, not arrays of complex vectors. This implementation assumes the definition ofvect_complexI gave above.3. addmx
I’m not sure what the
'\0's are doing here. Change it to