Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have a problem with my while-do loop. I'm trying to set my code for counting math games, all math operations are fine except for division. There is a problem with division, such that I would like the division to be residual, so I came up with one method which is given below. It is all in WPF Application.

What I have tried:

C#
Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, maxValue);
if (firstNumber < secondNumber)
{
    secondNumber = number.Next(1, firstNumber);
    do
    { 
        secondNumber++;
    }
    while (firstNumber % secondNumber != 0);
}
else
{
    do
    {
        secondNumber++;
    }
    while (firstNumber % secondNumber != 0);
}

total = firstNumber / secondNumber;

It seems to work well so far but sometimes the value secondNumber = -1 appears. How is this possible when I have everything set to positive values?

Thank you for any advice
Posted
Updated 23-May-21 23:18pm
v2

Signed integers in .NET are stored using Two's Complement:
Signed number representations - Wikipedia[^]

If you keep incrementing a positive integer past its maximum value, it will become negative.

If you want to throw an exception if that happens, you need to either test the value before incrementing, or execute your code in a checked context.
checked keyword - C# Reference | Microsoft Docs[^]
Checked and Unchecked - C# Reference | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 15170612 24-May-21 5:05am    
Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, maxValue);
if (firstNumber < secondNumber)
{
secondNumber = number.Next(1, firstNumber);
do
{
secondNumber++;
if (secondNumber<0)
{
secondNumber = 1;
}
}
while (firstNumber % secondNumber != 0);
}
else
{
do
{
secondNumber++;
if (secondNumber<0)
{
secondNumber = 1;
}
}
while (firstNumber % secondNumber != 0);
}

total = firstNumber / secondNumber;


is this also a possibility?
Richard Deeming 24-May-21 5:26am    
That should also work.
Member 15170612 24-May-21 5:34am    
Thank you!
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C#
Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, maxValue);
if (firstNumber < secondNumber)
{
	secondNumber = number.Next(1, firstNumber);
	do {
		secondNumber++;
	}
	while (firstNumber % secondNumber != 0);
}
else
{
	do {
		secondNumber++;
	}
	while (firstNumber % secondNumber != 0);
}

total = firstNumber / secondNumber;

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
C#
do {
    secondNumber++;
}
while (firstNumber % secondNumber != 0);

This loop start from the principle that secondNumber is not an integer divisor of firstnumber, which is weird.
How do you handle firstNumber=1 ?
It is easier to do it the other way arround:
C#
int total  = number.Next(1, maxValue);
int secondNumber = number.Next(1, maxValue);
int firstNumber = total * secondNumber;
 
Share this answer
 
Comments
Member 15170612 24-May-21 5:16am    
I did not write that I would like the numbers to be single digits, so a solution with multiplication is not possible, but thank you
You are incrementing secondNumber before you do the division, so the number could already have exceeded firstNumber. Change the loops to straight while:
C#
if (firstNumber < secondNumber)
{
    secondNumber = number.Next(1, firstNumber);
    while (firstNumber % secondNumber != 0)
    { 
        secondNumber++;
    }
}
else
{
    while (firstNumber % secondNumber != 0)
    {
        secondNumber++;
    }
}
 
Share this answer
 
Comments
Member 15170612 24-May-21 5:31am    
doesn't it make an endless loop from which it doesn't even "jump out"?
Richard MacCutchan 24-May-21 5:36am    
No (that is what happened with your version), because in both cases the loop will start with secondNumber less than, or equal to firstNumber. So if it is less, and the division yields a remainder, then secondNumber will be incremented, and the test will be executed again. But as soon as the two numbers are equal the modulo function will return a zero residue and the loop will stop. You can test it by setting the two numbers to values of your choice.
Member 15170612 24-May-21 6:57am    
if I cut it short for this, it gives me all the numbers.


secondNumber = number.Next(1, firstNumber);

while (firstNumber % secondNumber != 0) ;
{
secondNumber++;
}
total = firstNumber / secondNumber;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900