Alright, I finished this sooner than I thought. The solution does indeed involve modulus. I don't know if there is another way to do it without modulus given the limitations, but your welcome to explore that! Basically what I did was convert Coder777's for loop into a single while loop.
Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
using namespace std;
int main()
{
int startNum, digitCount = 0;
cout << "Hello player. Please input a number from 0-9." << endl;
cin >> startNum;
while(digitCount < 100)
{
if((digitCount > 0) && ((digitCount%10)==0))
{
cout << endl;
++startNum;
}
cout << (digitCount + startNum) %10;
digitCount++;
}
return 0;
}
| |
Modulus is not the easiest thing to logically use. I had to use some paper and write out exactly what happens just to understand how this works, so here it is:
|
cout << (digitCount + startNum) %10;
| |
(This is the key line thats spitting out your numbers)
The first row is unaltered by modulus 10 (%10) because anything less than ten will just have a remainder of itself.
I.E. 6%10, 10 doesn't go into 6 at all, and therefore there is a remainder of 6 and so it prints 6.
So for the first row we have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 for digitcount.
Sample calculations will be:
Let 5 be our startnum.
(0+5) % 10 = 5.
(1+5) % 10 = 6.
(2+5) % 10 = 7.
.etc
The reason we use
is because when you get above a total of 9 it will start cycling through 0-9.
I.E.
Let startnum= 5.
(4+5) % 10 = 9.
(5+5) % 10 = 0.
(6+5) % 10 = 1.
Fast forwarding digitcount...
(76+5) % 10 = 1.
This next line of code serves a purpose of first, making sure the first row remains unchanged, and secondly, starting a new line and adding 1 after every 10 digits.
1 2 3 4 5
|
if((digitCount > 0) && ((digitCount%10)==0))
{
cout << endl;
++startNum;
}
| |
digitCount > 0 makes it so that your first number will not get the ++startNum found inside the if statement, because your first number has a digitcount = 0 and therefore 0%10 = 0. If the digitCount > 0 was not there, it would skip a line and add 1 for your very first number.
digitCount%10==0 just means, that after every 10 digits, it should perform the following actions. Because 10%10 =0, 20%10=0, 30%10=0 .etc
The actions are pretty straight forward. After 10 digits it ends the line, and then adds 1 to the first digit of the new line.