Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need some help making Makefiles and complex.h (second) In the Makefile, you sh

ID: 3807645 • Letter: I

Question

I need some help making Makefiles and complex.h (second)

In the Makefile, you should compile "complex.c" and "main.c" into object files separately, and make sure the object file depends on "complex.h" as well. In other words, if you modify "complex.h", both "complex.c" and "main.c" will be compiled again if you type "make". The final executable "complex" depends on linking "main.o" and "complex.o".

Second, in general, global variables and constants,structure definitions, and function prototypes are defined in the header file for inclusions. In "complex.h", you should have structure type definition for "complex_t" and function prototypes for "scan_complex", print_complex", "add_complex", "subtract_complex", "multiply_complex", "divide_complex", and "abs_complex".

This is the code:

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

/* User-defined complex number type */
typedef struct {
double real, imag;
} complex_t;
int scan_complex(complex_t *c);
void print_complex(complex_t c);
complex_t add_complex(complex_t c1, complex_t c2);
complex_t subtract_complex(complex_t c1, complex_t c2);
complex_t multiply_complex(complex_t c1, complex_t c2);
complex_t divide_complex(complex_t c1, complex_t c2);
complex_t abs_complex(complex_t c);

/* Driver */
int
main(void)
{
complex_t com1, com2;
/* Gets two complex numbers */
printf("Enter the real and imaginary parts of a complex number ");
printf("separated by a space> ");
scan_complex(&com1);
printf("Enter a second complex number> ");
scan_complex(&com2);

/* Forms and displays the sum */
printf(" ");
print_complex(com1);
printf(" + ");
print_complex(com2);
printf(" = ");
print_complex(add_complex(com1, com2));

/* Forms and displays the difference */
printf(" ");
print_complex(com1);
printf(" - ");
print_complex(com2);
printf(" = ");
print_complex(subtract_complex(com1, com2));

/* Forms and displays the absolute value of the first number */
printf(" |");
print_complex(com1);
printf("| = ");
print_complex(abs_complex(com1));
printf(" ");

return (0);
}

/*
* Complex number input function returns standard scanning error code
* 1 => valid scan, 0 => error, negative EOF value => end of file
*/
int
scan_complex(complex_t *c) /* output - address of complex variable to
fill */
{
int status;

status = scanf("%lf%lf", &c->real, &c->imag);
if (status == 2)
status = 1;
else if (status != EOF)
status = 0;

return (status);
}
/*
79. * Complex output function displays value as (a + bi) or (a - bi),
80. * dropping a or b if they round to 0 unless both round to 0
81. */
void
print_complex(complex_t c) /* input - complex number to display */
{
double a, b;
char sign;

a = c.real;
b = c.imag;

printf("(");

if (fabs(a) < .005 && fabs(b) < .005) {
printf("%.2f", 0.0);
} else if (fabs(b) < .005) {
printf("%.2f", a);
} else if (fabs(a) < .005) {
printf("%.2fi", b);
} else {
if (b < 0)
sign = '-';
else
sign = '+';
printf("%.2f %c %.2fi", a, sign, fabs(b));
}

printf(")");
}

/*
* Returns sum of complex values c1 and c2
*/
complex_t
add_complex(complex_t c1, complex_t c2) /* input - values to add */
{
complex_t csum;

csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;
return (csum);
}

/*
* Returns difference c1 - c2
*/
complex_t
subtract_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdiff;
cdiff.real = c1.real - c2.real;
cdiff.imag = c1.imag - c2.imag;

return (cdiff);
}

/* ** Stub **
138. * Returns product of complex values c1 and c2
139. */
complex_t
multiply_complex(complex_t c1, complex_t c2) /* input parameters */
{
printf("Function multiply_complex returning first argument ");
return (c1);
}
. /* ** Stub **
148. * Returns quotient of complex values (c1 / c2)
149. */
complex_t
divide_complex(complex_t c1, complex_t c2) /* input parameters */
{
printf("Function divide_complex returning first argument ");
return (c1);
}

/*
158. * Returns absolute value of complex number c
159. */
complex_t
abs_complex(complex_t c) /* input parameter */
{
complex_t cabs;

cabs.real = sqrt(c.real * c.real + c.imag * c.imag);
cabs.imag = 0;

return (cabs);
}

Explanation / Answer

I have separated your code into 3 files complex.h, complex.c and main.c. The following is your code and makefile for it.

After copying it to a directory, just give the command "make", it will compile and produce the binary "complex"

complex.h


#ifndef COMPLEX_H_
#define COMPLEX_H_

/* User-defined complex number type */

typedef struct {
double real, imag;
} complex_t;

int scan_complex(complex_t *c);

void print_complex(complex_t c);

complex_t add_complex(complex_t c1, complex_t c2);

complex_t subtract_complex(complex_t c1, complex_t c2);

complex_t multiply_complex(complex_t c1, complex_t c2);

complex_t divide_complex(complex_t c1, complex_t c2);

complex_t abs_complex(complex_t c);

#endif

complex.c

#include <stdio.h>
#include <math.h>
#include "complex.h"

/*
* Complex number input function returns standard scanning error code
* 1 => valid scan, 0 => error, negative EOF value => end of file
*/
int
scan_complex(complex_t *c) /* output - address of complex variable to
fill */
{
int status;
status = scanf("%lf%lf", &c->real, &c->imag);
if (status == 2)
status = 1;
else if (status != EOF)
status = 0;
return (status);
}
/*
79. * Complex output function displays value as (a + bi) or (a - bi),
80. * dropping a or b if they round to 0 unless both round to 0
81. */
void
print_complex(complex_t c) /* input - complex number to display */
{
double a, b;
char sign;
a = c.real;
b = c.imag;
printf("(");
if (fabs(a) < .005 && fabs(b) < .005) {
printf("%.2f", 0.0);
} else if (fabs(b) < .005) {
printf("%.2f", a);
} else if (fabs(a) < .005) {
printf("%.2fi", b);
} else {
if (b < 0)
sign = '-';
else
sign = '+';
printf("%.2f %c %.2fi", a, sign, fabs(b));
}
printf(")");
}
/*
* Returns sum of complex values c1 and c2
*/
complex_t
add_complex(complex_t c1, complex_t c2) /* input - values to add */
{
complex_t csum;
csum.real = c1.real + c2.real;
csum.imag = c1.imag + c2.imag;
return (csum);
}
/*
* Returns difference c1 - c2
*/
complex_t
subtract_complex(complex_t c1, complex_t c2) /* input parameters */
{
complex_t cdiff;
cdiff.real = c1.real - c2.real;
cdiff.imag = c1.imag - c2.imag;
return (cdiff);
}
/* ** Stub **
138. * Returns product of complex values c1 and c2
139. */
complex_t
multiply_complex(complex_t c1, complex_t c2) /* input parameters */
{
printf("Function multiply_complex returning first argument ");
return (c1);
}
/* ** Stub **
148. * Returns quotient of complex values (c1 / c2)
149. */
complex_t
divide_complex(complex_t c1, complex_t c2) /* input parameters */
{
printf("Function divide_complex returning first argument ");
return (c1);
}
/*
158. * Returns absolute value of complex number c
159. */
complex_t
abs_complex(complex_t c) /* input parameter */
{
complex_t cabs;
cabs.real = sqrt(c.real * c.real + c.imag * c.imag);
cabs.imag = 0;

return (cabs);
}

main.c

#include "complex.h"
#include<stdio.h>
#include<math.h>

/* Driver */
int main(void)
{
complex_t com1, com2;
/* Gets two complex numbers */
printf("Enter the real and imaginary parts of a complex number ");
printf("separated by a space> ");
scan_complex(&com1);
printf("Enter a second complex number> ");
scan_complex(&com2);
/* Forms and displays the sum */
printf(" ");
print_complex(com1);
printf(" + ");
print_complex(com2);
printf(" = ");
print_complex(add_complex(com1, com2));
/* Forms and displays the difference */
printf(" ");
print_complex(com1);
printf(" - ");
print_complex(com2);
printf(" = ");
print_complex(subtract_complex(com1, com2));
/* Forms and displays the absolute value of the first number */
printf(" |");
print_complex(com1);
printf("| = ");
print_complex(abs_complex(com1));
printf(" ");
return (0);
}

makefile: //save it in the name of "makefile" only

all: main.o complex.o
gcc -o complex complex.o main.o -lm

complex.o:
gcc -c -o complex.o complex.c

main.o: complex.o
gcc -c -o main.o main.c

output:

Enter the real and imaginary parts of a complex number
separated by a space> 2 3
Enter a second complex number> 4 5

(2.00 + 3.00i) + (4.00 + 5.00i) = (6.00 + 8.00i)

(2.00 + 3.00i) - (4.00 + 5.00i) = (-2.00 - 2.00i)

|(2.00 + 3.00i)| = (3.61)