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

1. /* Example: StructuresDirect in CodeBlocks 2. structures as function argument

ID: 3698689 • Letter: 1

Question

1. /* Example: StructuresDirect in CodeBlocks

2. structures as function arguments and return values

3. Direct instances using names */

4.

5. #include

6. #include

7.

8. struct cplx {

9. double real; /* real part */

10. double imag; /* imaginary part */

11. };

12.

13. struct cplx add(struct cplx a, struct cplx b); /* function prototype */

14.

15.

16. int main(void)

17. {

18. struct cplx x, y, z;

19. time_t rawtime = time(NULL);

20.

21.

22. x.real = 2.5;

23. x.imag = 5.0;

24. y.real = 3.2;

25. y.imag = -1.7;

26.

27.

28. z = add(x, y);

29. printf("Direct Instance z = %4.2f + %4.2f j ", z.real, z.imag);

30. printf("Today is %s", ctime(&rawtime));

31. return 0;

32. }

33.

34.

35. struct cplx add(struct cplx a, struct cplx b)

36. {

37. struct cplx c = a; /* can initialise an auto struct variable */

38.

39. c.real += b.real;

40. c.imag += b.imag;

41. return c; /* can return a struct value */

42. }

2. Which lines form the prototype for a structure

3. What is the tag name of the structure

4. Which lines create instances of the structure, what are the structure names?

5. Which lines initialize the member elements of the structures? How?

6. Which line is the prototype for a function with structure parameters

7. Which lines are the function definition with structure parameters

8. How are the member elements of a named (direct instance) structure dereferenced (how are the values changed)?

9. How is the addition of two structures accomplished?

Explanation / Answer

2. Which lines form the prototype for a structure
=> 8,9,10,11 lines
8. struct cplx {
9. double real; /* real part */
10. double imag; /* imaginary part */
11. };

3. What is the tag name of the structure
=> tag name is cplx

4. Which lines create instances of the structure, what are the structure names?
=>
line 18. struct cplx x, y, z;
names are x,y and z

line 37. struct cplx c = a;
names is x


5. Which lines initialize the member elements of the structures? How?
=> lines 22, 23, 24, 25 initialize the member elements
22. x.real = 2.5;
23. x.imag = 5.0;
24. y.real = 3.2;
25. y.imag = -1.7;

6. Which line is the prototype for a function with structure parameters
=> line 13 gives the prototype of structure

7. Which lines are the function definition with structure parameters
=> line 35 defines the function by using structure as parameter
35. struct cplx add(struct cplx a, struct cplx b)

8. How are the member elements of a named (direct instance) structure dereferenced (how are the values changed)?
=> by using setters for member variables to change their values to desired
numbers

9. How is the addition of two structures accomplished?
=> It is accomplished by adding real values of compelex numbers
and imaginary vlaues of both complex numbers. Since this is a complex number structre