Page 189 - ARM Based Microcontroller Projects Using MBED
P. 189

8.13  PROJECT 10—LOOP EXECUTION TIMES               175
              /********************************************************************

                                          MULTIPLICATION
                                          ==============
              This program generates two random numbers between 1 and 100. The
              product of these numbers is shown but the result is not shown, and
              the user is given 20 seconds to calculate the result. After this
              time the result is shown so that the user can check with his/her
              own result.
              Author: Dogan Ibrahim
              Date  : August 2018
              File  : Multiply
              *********************************************************************/
              #include "mbed.h"
              Serial MyPC(USBTX, USBRX);

              //
              // Clear the screen
              //
              void clrscr()
              {
                  char clrscr[] = {0x1B, '[', '2' , 'J',0};
                  MyPC.printf(clrscr);
              }

              //
              // Home the cursor
              //
              void homescr()
              {
                  char homescr[] = {0x1B, '[' , 'H' , 0};
                  MyPC.printf(homescr);
              }


              int main()
              {
                 int Number1, Number2;

                 while(1)
                 {
                      clrscr();                                        // Clear the screen
                      homescr();                                       // Home the cursor
                      Number1 = rand() % 100 + 1;                      // First number
                      Number2 = rand() % 100 + 1;                      // Second number
                      MyPC.printf("\n\r%d X %d = ", Number1, Number2); // Display the product
                      wait(20.0);                                      // Wait 20 seconds
                      MyPC.printf("%d\n", Number1 * Number2);          // Display result
                      wait(5.0);                                       // Wait 5 seconds
                  }
              }
            FIG. 8.35  Program listing.
   184   185   186   187   188   189   190   191   192   193   194