Well, the program asks for an integer input, then assigns the modulus (remainder) of a/50 to b.
Then it prints out the value of b and waits for you to input a new value for a, then exits without doing anything else.
Lets say you put in a value of 127 to be assigned to a, now if you do 127/50 the answer is 2 and the remainder is 27. So it will print out 27.
Let's say you enter a value of 20. 20/50 = 0 with a remainder of 20.
If you commented your code or output something a little more meaningful with your cout statements you'd be able to understand it better.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
// This program will accept an integer value and assign it to iDividend,
// then the program will perform a modulus division of iDividend / DIVISOR and assign
// the result to iRemainder. Next output the quotient and remainder then
// wait for the user to press a key before exiting the program.
#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc, char** argv) {
// declare a long variable iDividend, and a short variable Remainder, and
// assign them a value of zero, so that they begin life from a known state.
// Otherwise there's no telling what kind of junk they could be holding
// It just depends on what was laying around in that memory space
// when you ran your program.
long iDividend = 0;
short iRemainder = 0; // will never be larger than 49 so a short is enough
// You could also declare those longs as shorts if memory is a
// concern, but longs will handle a larger range of values and is
// usually worth the extra memory.
// Also, you may as well define a const short as your DIVISOR, rather than
// use a literal number.
const short DIVISOR = 50; // a short is all the space we need here since this
// number will not change.
// Why not use ints?
// shorts are guaranteed to be 2 bytes
// in size, whereas longs are 4 bytes in size.
// and ints can be 2 or 4, there is no guarantee.
// So to make sure you don't get caught by any gotchas
// use shorts and longs rather than ints, then you know
// what kind of memory your variables are consuming.
// To make sure we are speaking the same language..
// In division we use these terms to describe each of
// the components:
// a / b = c where a is the Dividend, b is the Divisor,
// and c is the Quotient
// assign the value to iDividend ('a' renamed),
// assign iRemainder ('b' renamed) to the
// result of iDividend % DIVISOR
// % is the modulus operator and what it means is
// give me the remainder when dividing the two numbers.
// such as 10 % 3
// In this case 10 / 3 = 3 with a remainder of 1
// so 10 % 3 = 1. Cool huh?
cout << "Enter an integer (whole number): ";
cin >> iDividend, iRemainder = iDividend % DIVISOR;
// output a meaningful response
cout << iDividend << " / " << DIVISOR << " = " << iDividend / DIVISOR << ", R = "
<< iRemainder << endl;
// wait for the user to enter a value
cout << "Press any key to exit...";
// ignore any newline already buffered
cin.ignore(0,'\n');
// get the key press
getch();
// and exit (0 indicates success)
return 0;
}
| |
This produces the following output:
Enter an integer (whole number): 723
723 / 50 = 14, R = 23
Press any key to exit...
Process returned 0 (0x0) execution time : 32.702 s
Press any key to continue.
|
I hope this helps you out. It's way more commented than you normally would, but I wanted to be sure you understood what was going on.
Also, you would normally do some error checking to make sure the input is valid, such a checking that the value entered is in fact an integer and not a float, for example.
Edit: A good supply of comments, variable names that describe their purpose, proper indentation, and some white-space will go a long way toward making your source code more readable.