C++: modulus

Author: Ronald S. Holland
at
Total Application Works

ronholland@sumtotalz.com





To contact us



[
HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript c#

© 2002 - 2016 All Rights Reserved Total Application Works


  • Tell a friend about this site (copy and paste the following HTML page into a file (e.g., using Notepad).)
    <html>
    <head>
    <title>Example of a link </title>
    </head>
    <body>
    <a href="http://sumtotalz.com/TotalAppsWorks/CPP/CPP_Modulus.html">
    Programming Tutorials: modulus </a>
    </table>
    </body>
    </html>
    Source code


Read Me First

The Modulus.zip file contains the following files:

  1. Modulus_1.c++ - this file contains the main method code and is the entry point when the program is loaded into memory,
  2. Modulus_2.c++ - this file contains the main method code and is the entry point when the program is loaded into memory,
  3. CPP_Modulus.html - is the tutorial that describes how to build the Modulus programs
    • Modulus_1.cpp
    • Modulus_2.cpp.
  4. Modulus_1.vcproj - Basically the .csproj file contains the list of files in your project, plus the references to system assemblies etc. It contains everything you need to build your project.
  5. Modulus_2.vcproj - Basically the .csproj file contains the list of files in your project, plus the references to system assemblies etc. It contains everything you need to build your project.
  6. Modulus_1.SLN
  7. Modulus_2.SLN
This application requires Microsoft c++ Visual Studio to run.

To run any of the projects:

  1. Go to the folder where you downloaded the Modulus.zip compressed file
  2. Extract the files
  3. Right clck on Modulus_1.vcproj or Modulus_2.vcproj - click on the version of Microsoft studio you have
  4. When Microsoft studio IDE is started
  5. Click on the Debug menu
  6. Click on the Start without Debugging menu
    • the Modulus_1 or Modulus_2 project will begin to execute

Introduction

This tutorial assumes that you have some prior programming experience. It is not intended to be a comprehensive discussion on c++ or programming. In this tutorial, we will briefly discuss variables, as we ease into c++. c++ as a programming language is widespread and can be found on nearly all computers.

C++ modulus operator

What is the modulus operator? The modulo operation finds the remainder of division of one number by another number e.g., 6/5 = 1. Consider two positive numbers, where a is the dividend and n is the divisor e.g., a/n, a modulo n (abbreviated as a mod n = b), where b is the remainder. For example, the expression "9 mod 8" would evaluate to 1 because 9 divided by 8 results in a remainder of 1, whereas, "4 mod 2" results in a remainder of 0. Consider the examples in Figure 1 below.

 

  1. 5 % 2 = 1
  2. 99 % 3 = 0
  3. 145 % 3 = 1
  4. 9 % 7 = 2
  5. 12 % 7 = 5
  6. 33 % 13 = 7
  7. 45 % 9 = 0
  8. 25 % 7 = 4
  9. 17 % 13 = 4
Figure 1: Examples using the modulus function


In Figure 1, we show some examples using the modulus function. There are several simple problems that divide one number by another: what's left over when we complete the division is the remainder. The answer is easy to compute: divide 11 by 4 and take the remainder: 3. How can we compute this in a programming language like C or C++? The c++ language provides a built-in function, the modulus operator ('%'), that provides a remainder that results from performing integer division. Some of the uses for the modulus function are discussed in the next section.

The modulus operator is useful in a variety of circumstances

How is the modulus operator is useful? It is commonly used to take a randomly generated number and reduce that number to a random number on a smaller range. For example, the following figures shows the frequency a number appears when a die is rolled 100 times.

 

#include <iostream>

int main() {
   
   int roll ;
   const int arraySize = 100;
   int face, frequency[ arraySize ] = { 0 };

   srand( time( 0 ) );

   for ( roll = 1; roll <= 100; roll++ )       
      ++frequency[ 1 + rand() % 6 ];                                        

   cout << "Face" << setw( 13 ) << "Frequency" << endl;

   // ignore element 0 in the frequency array
   for ( face = 1; face < 7; face++ )  
      cout << setw( 4 ) << face
           << setw( 13 ) << frequency[ face ] << endl;


   return 0 ;

}
 
Output
 
Face    Frequency
   1           15
   2           10
   3           18
   4           13
   5           14
   6           17

Figure 2: Frequency of die rolls


In Figure 2, we show in a random roll of a die, the frequency of the numbers rolled. In the next example, we take two numbers entered by the user and then compute the remainder.

 

#include <iostream>

using namespace std;

int main() {
         
    int num , num2;

    do {
       cout << "Enter an integer dividend" << endl ;
       cin >> num;

       cout << "Enter an integer divisor" << endl ;
       cin >> num2;

       cout << num << " % " << num2 << " equals " << 
                ( num % num2) << endl;

       cout << "Enter an -1 to quit" << endl ;
       cin >> num ;
       if( num == -1 )
          break ;
   }
   while (num != -1 ) ;

   return 0;
}
 
Figure 3: Example II using the modulus function


Conclusions

The purpose of this tutorial was to:

  • Introduce you to the modulus function by
    • Showing some examples using modulus operator
    • Showing how the modulus operator is useful in a variety of circumstances
  • Giving you fundamentals of the modulus operator so that you can confidently use this operator on your own.





HOME] Consulting Design Maintenance Project Testing Training Turnkey Java C++ SQL HTML JavaScript c#


To contact us

© 2002 - 2016 All Rights Reserved Total Application Works