Lab Exercise 15: Arrays of Structures

Last modified by Microchip on 2023/11/09 09:21

Objective


This lab introduces you to the syntax used to work with arrays of structures. For this exercise, we have defined a structure to hold the real and imaginary parts of a complex number. You will then need to modify the real and imaginary parts of each element of an array of complex numbers.

Reference Materials

Exercise Files

Procedure

Open the Project

Start MPLAB® X IDE, then click on the Open Project MPLAB X IDE Open Project Icon icon on the main toolbar

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab15.X folder.

Select Open Project MPLAB X IDE Open Project Icon.


Open C Source File

Open the Lab15.c source file from the project tree by double-clicking on its icon.

This will bring up Lab15.c in a window to edit.

Lab15.png


Edit the Source File
Multiply the real (re) part of each array element by 10. (HINT: Use *=)

Multiply the imaginary (im) part of each array element by 5 (HINT: Use *=) 

Arrays of Structures Flowchart

Back to Top


Debug Project

Once you finish writing the code:

Click on the Debug Project MPLAB X Debug Project Icon button. This will build and send the program to the simulator.

Click on the Continue 

MPLAB X IDE Continue Icon

button. This begins the simulation. Wait for the UART 1 Output window to finish printing.

Click on the Halt 

MPLAB X IDE Pause Icon

button. This will stop execution so that we may examine the results.

Back to Top


Verify Results

Lab Results
After successfully building and running your code, you should see the following in the UART1 Output window:

Code Analysis

Note: Line numbers correspond to those in the provided solution file.

Line 12-15

This is the type declaration for the complex number structure. There are two members, representing the real (re) and imaginary (im) parts, and the type name is complex.

Line 20

This is a variable declaration. We are creating an array of structures of type complex. The array has three elements, and we are initializing the array as we are declaring it. Note that the syntax we use to initialize the array is essentially the same as initializing a multidimensional array. So, in a more algebraic form, our array now looks like this:
x[0] = 1.1 + j1.2
x[1] = 2.1 + j2.2
x[2] = 3.1 + j3.2

Line 48

Step 3a: At this point, we are now inside the loop, and will be accessing a complex number on each pass. STEP 1 has us multiplying the real part of the current array element by 10. The easiest way to do this is to use the *= operator. The structure member may be accessed by using the dot notation with the array variable and its index:

x[i].re *= 10;

Line 55

We perform a similar operation to step 1, but this time, we will be multiplying the imaginary part by 5:

x[i].im *= 5;

Line 60

This line prints out the recently modified complex numbers in an algebraic format:

printf("%f + j%f\n", x[i].re, x[i].im);

Since the complex number’s members are both floating point types, we need to use %f as our placeholder/formatting characters in the printf() control string. Then, the arguments used are the same as those used on Line 48 and Line 55 to specify a structure member that is part of an array element.

Back to Top


End Debug Session

End the Simulation Session by clicking the Finish Debugger Session MPLAB X IDE Finish Debugger Session Icon button.

Clear out the UART 1 Output window (Ctrl + L).
Close the Project.

Back to Top


Conclusions

  • Arrays of structures allow groups of related structures to be referenced by a common name.
  • Individual structures may be referenced by the array index.
  • Individual structure members may be referenced by the dot notation, in conjunction with the array name and index.

Back to Top