Lab Exercise 10: Arrays


The purpose of this lab is to help you understand how to create and use arrays. The main concepts include defining and initializing arrays, passing array elements to functions and assigning function results to array elements. The code will also illustrate how to print out elements of an array to the Simulator UART1 I/O window.

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

File Download
Windows Linux Mac OSX
Project and Source Files



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 Lab10.X folder.

Select Open Project OpenProjectButton.png.


Open C Source File


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

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


Edit Source File

In this lab, some of the code has been placed in a separate file to keep things simple where you need to make your edits. Specifically, the printArray() and print2dArray() functions have been placed in the file PrintArray.c.

All of the edits in this lab will be made in Lab10.c

Create two initialized arrays with 10 elements each named array1 and array2 (you may use the pre-defined constant ARRAY_SIZE as part of the array declaration). The arrays should be initialized with the following values:

  • array1: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • array2: 9, 8, 7, 6 ,5, 4, 3, 2, 1, 0

Note: the elements are all of type int.

Pass the two arrays you declared above (array1 and array2) to the function add_function() (see its definition below). Store the result of the function in the array result[]. The idea here is to add each corresponding element of array1 and array2 and store the result in result[]. In other words, add the first element of each array and store it in the first element of result[], then add the second elements, and so on. Take advantage of the counter variable i to make this happen.



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.



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.


Here you have learned how to create and use arrays. Frequently, arrays are manipulated within loops where the same operation needs to be carried out on a series of related variables, as we have seen here. The array index may be a variable, which gives us tremendous flexibility for accessing arrays based on some conditions we set up in our programs.
Arrays may also be used in a lookup table-like scheme, where a variable is used as an offset into the table (via the array index) to retrieve a particular value from the list (array values).
Arrays are also used to implement text strings in systems incorporating sophisticated user interfaces with some sort of text-capable display (LCD, VFD, PC interface, etc.).

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.