Academic Program

Lab Exercise 15: Arrays of Structures

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.

Software Tools

Tool  About Installers
Installation
Instructions
 Windows  Linux  Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC16
C Compiler

Exercise Files

File Download
Installation
Instructions
 Windows  Linux  Mac OSX
Project and Source Files

Procedure

Open the Project

Start MPLAB® X IDE, then click on the Open Project Main_Open_Project.png 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 OpenProjectButton.png.

 

 

Open C Source File

Lab15.png

 

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

 

Edit Source File

STEP 1:

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

STEP 2:

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

Lab15Flow.png

 

Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt Debug_Pause.png button. This will stop execution so that we may examine the results.

Results

Lab15Results.png

 

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 1: 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
STEP 2: has us performing 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.

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Debug_Finish_Debugger_Session.png button.

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

Close the Project.

 

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.