Nested Loop Challenge 1.
Here is the program that we used for a simple odometer with just two digits.
main() { int ones int tens int km
= 0; = 0; = 0;
// Initialize variables // For an odometer
for (tens=0 ; tens<10 ; tens=tens+1) { for (ones=0 ; ones<10 ; ones=ones+1) { km = 10*tens + ones; } }
// Outer loop counts tens of km
while(1);
// Stop here when you get to 99 km
// Inner loop counts km // Total number of km traveled
}
2.
The challenge was to modify the program to also use hundreds, thousands, and tenthousands variables to allow the odometer to count up to 99999. Additionally, make it so that when the odometer rolls over from 99999, it starts over at 00000 and begins counting again.
3.
The program on the next page accomplishes this task, but it is only one way that the program could be done.
Page 1 of 3
#include <msp430.h> #define
DEVELOPMENT
main() { unsigned unsigned unsigned unsigned unsigned unsigned WDTCTL
long long long long long long
ones tens huns thou tnth km
= = = = = =
0x5A80
// Stop the watchdog timer
0; 0; 0; 0; 0; 0;
// // // // // //
= DEVELOPMENT;
Ones digit Tens digit Hundreds digit Thousands digit (1K) Ten thousands digit (10K) Total number of kilometers traveled
// You will learn more about this in Section 7 // It is not strictly needed for a general C // program, but it is necessary for the MSP430
while(1) { for (tnth=0 ; tnth<10 ; tnth=tnth+1) { for (thou=0 ; thou<10 ; thou=thou+1) { for (huns=0 ; huns<10 ; huns=huns+1) { for (tens=0 ; tens<10 ; tens=tens+1) { for (ones=0 ; ones<10 ; ones=ones+1) {
// Outer loop counts 10K digit of km
// Outer loop counts 1K digit of km
// Outer loop counts hundredsns of km
// Outer loop counts tens of km
// Inner loop counts km
km = 10000*tnth + 1000*thou + 100*huns + 10*tens + ones; // Total number of km }// end ones digit loop }// end tens digit loop }// end hundreds digit loop }// end thousands digit loop }//end ten thousands digit loop km = 0; }//end while(1)
}// end main()
Page 2 of 3
All tutorials and software examples included herewith are intended solely for educational purposes. The material is provided in an “as is” condition. Any express or implied warranties, including, but not limited to the implied warranties of merchantability and fitness for particular purposes are disclaimed. The software examples are self-contained low-level programs that typically demonstrate a single peripheral function or device feature in a highly concise manner. Therefore, the code may rely on the device's power-on default register values and settings such as the clock configuration and care must be taken when combining code from several examples to avoid potential side effects. Additionally, the tutorials and software examples should not be considered for use in life support devices or systems or mission critical devices or systems. In no event shall the owner or contributors to the tutorials and software be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
Page 3 of 3