Discussion Locked

C language - Struct

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

How to do this question:
#include

/* A complex number to be represented by a structure of two members */
struct cmplx {
int real;
int imaginary;
};

/*The purpose of the main() is to test the correctness of the function Complex_Add()*/
void main(void)
{
cmplx source = { 3, 4 };
cmplx addem = { 5, 6 };

Complex_Add( ____________ , ____________ ); /* Fill in the blanks */

printf( ?The solution: \n?);

printf( ?The real-part is %d\n?, __________________ ); /* Fill in the blanks */

printf( ?The imaginary-part is %d?, _________________ ); /* Fill in the blanks */

}

void Complex_Add( cmplx *first, cmplx *second)
{
/* Write your code here.
Add the complex number (pointed to by) second to that of first, and store
the result in first. */

}

5 replies

by jay eckles

Well, I imagine the call to Complex_Add would be something like
Complex_Add( &source, &addem ) ;
the printf's would be something like
printf( "The real-part is %d\n" source.real ) ;

Can't help you with the Complex_Add because I don't know how to add complex numbers. If it's as simple as adding the real parts and adding the imaginary parts, well then I guess it would be pretty simple.

Good luck.

Jay Eckles
www.jayeckles.com

by bojidar alexandrov

#include

/* A complex number to be represented by a structure of two members */
struct cmplx {
int real;
int imaginary;
};

void Complex_Add( cmplx *first, cmplx *second)
{
/* Write your code here.
Add the complex number (pointed to by) second to that of first, and store
the result in first. */

first->real = first->real + second->real;
first->imaginary = (first->imaginary+second->imaginary);
}

/*The purpose of the main() is to test the correctness of the function Complex_Add()*/
void main(void)
{
cmplx source = { 3, 4 };
cmplx addem = { 5, 6 };

Complex_Add( &source, &addem ); /* Fill in the blanks */

printf( "The solution: \n");

printf( "The real-part is %d\n", source.real); /* Fill in the blanks */

printf( "The imaginary-part is %d", source.imaginary); /* Fill in the blanks */

}

This discussion is closed to new replies.