Click here to Skip to main content
15,032,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm trying to write a condition in a while-loop and it still doesn't work for me, or rather numbers that don't match the condition always pass. Does anyone have advice on how to write a condition to work? I try to achieve that there is a non - residential division and at the same time that the result is positive and no later than 10...

What I have tried:

EDIT:
C#
private int maxValueDivision = 100;
private int maxValue = 10;

int secondNumb = number.Next(1, maxValue);
int firstNumb = number.Next(1, maxValueDivision);
 while (firstNumb % secondNumb != 0 &&  firstNumb / secondNumb < 10 )
                {
                    secondNumb++;
                }
total = firstNumb / secondNumb;


EDIT2:
C#
private int maxValueDivision = 100;
private int maxValue = 10;

int secondNumb = number.Next(1, maxValue);
int firstNumb = number.Next(1, maxValueDivision);
 while (firstNumb % secondNumb != 0 &&  firstNumb / secondNumb < 10 )
                {
                    firstNumb++;
                    if(firstNumb>100)
                    {
                        firstNumb = number.Next(1, maxValueDivision);
                    }
                }
total = firstNumb / secondNumb;

How do I ensure that secondNumb is always less than 10 but still maintains a residual division?

Thank you for any advice
Posted
Updated 28-Jun-21 2:21am
v3
Comments
Ralf Meier 28-Jun-21 5:14am
   
Perhaps it is easier if you try to describe what you want to achieve ...
Member 15170612 28-Jun-21 5:16am
   
You have it in question, look at the last sentence...
Ralf Meier 28-Jun-21 5:21am
   
I asked you to describe what should happen - as you see by the answer of OriginalGriff your code itself makes no real sense.
So what should happen ?
Member 15170612 28-Jun-21 6:07am
   
I would like the while-loop to end as soon as the division of firstNumb and secondNumb has no remainder, at the same time the value of the result of their division would not be higher than 10 and at the same time the value of secondNumb is not higher than 10.

Well, first off, this part will never be true:
C#
while (... && (firstNumb / secondNumb < 0 || ...))
Because your random numbers will always be 1 or greater: so 0 isn't possible, much less negative numbers!
So the condition you are evaluating is:
C#
while (firstNumb % secondNumb != 0 && firstNumb / secondNumb > 10)
But ... your second condition will rarely match either:
C#
while (... && firstNumb / secondNumb > 10)
Because firstNumb is 1 .. 99, and secondNumb is 1 .. 9.
So unless firstNumb is high, and secondNumb is low, the result of the division is never going to be greater than 10.
In fact, if secondNum is 5 for example, then firstNumb has to be 55 or higher for the condition to be true. For 6, that's 66+; for 9 there is only one value of firstNumb that will give you true: 99!

You have stacked the odds against you in getting that loop to go round at all!

Quote:
Do you mean, generate a secondNumb and then add firstNumb ++ in a while-loop?


You could, but ... why use a loop at all?
When you use a loop, your code become non-deterministic in that you do not know how long it will take to generate a valid value.
The way I'd do it is to generate secondNumb, and use that as and index into an array of arrays. Each "inner array" contains only the "valid values" for that index:
1: 
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, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
2: 
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 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, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
3: 
3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 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, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
4: 
4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 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, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
5: 
5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
6: 
6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
7: 
7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
8: 
8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
9: 
9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99

Then generate a second random number which selects one of them.

That way, your code has no loops, only generates valid values, and is time deterministic.
   
v2
Comments
Member 15170612 28-Jun-21 5:25am
   
I added a negative value there because if I add secondNumb ++ it is able to get into negative values, I made a mistake with a small number of the second number which I did not realize but I still don't know how to write it to be just a small multiplier . Thank you.
OriginalGriff 28-Jun-21 5:50am
   
You are going to have to explain what you expect the code to do, I have no idea from "faulty" code!
Member 15170612 28-Jun-21 5:57am
   
I would like the while-loop to end as soon as the division of firstNumb and secondNumb has no remainder, at the same time the value of the result of their division would not be higher than 10 and at the same time the value of secondNumb is not higher than 10.
OriginalGriff 28-Jun-21 6:21am
   
So ...
while (!(c1 && c2 && c3))
then?
Member 15170612 28-Jun-21 6:26am
   
while (firstNumb % secondNumb != 0 && firstNumb / secondNumb > 10 && secondNumb>10) Make it sense?
OriginalGriff 28-Jun-21 7:17am
   
It does, but ... secondNumb can never be greater than 10, or indeed greater than 9!
Instead of looping so much, why not generate secondNumb and then use that to create a "suitable" value for firstNumb?
Member 15170612 28-Jun-21 7:37am
   
Do you mean, generate a secondNumb and then add firstNumb ++ in a while-loop?
OriginalGriff 28-Jun-21 8:30am
   
Answer updated.
Member 15170612 28-Jun-21 8:22am
   
I improved question
Member 15170612 28-Jun-21 5:35am
   
I didn't notice that I had a reverse sign
Member 15170612 29-Jun-21 5:11am
   
I haven't seen that yet, wouldn't there be any advice on how to do it?
OriginalGriff 29-Jun-21 5:34am
   
How would you do it manually?
If you had each set of "valid values" on a separate page of a book perhaps?
Member 15170612 29-Jun-21 5:42am
   
string[] numb = {1,2,3,...}; something like this
OriginalGriff 29-Jun-21 6:00am
   
No, ignore computers, and think about how you would do it manually. (You're getting hung up on implementation instead of thinking about design).

I give you a book with 9 pages.
Each page has a set of numbers which are valid for that page number.
How do you pick two valid numbers?
Member 15170612 29-Jun-21 6:12am
   
I read the rows and columns on a certain page one by oneI read the rows and columns on a certain page one by one. If I understood the question.
OriginalGriff 29-Jun-21 6:48am
   
You are still thinking "Implementation".

What do you know about the book?
Hint: each page has a page number. What do you think that could represent?
Bigger hint: You only have two numbers you are interested in getting, don't you?
Member 15170612 29-Jun-21 7:15am
   
Ahhhh, you think the page number is the number I'm looking for as "secondNumb" and the number on the page matches the number "firstNumb" that meets the conditions.
OriginalGriff 29-Jun-21 7:39am
   
Yay!
I knew you'd get it.

So you randomly pick a page number, turn to the appropriate page, and randomly pick a number from however many are on it.

No loops, no testing needed.

Make sense?
Member 15170612 30-Jun-21 2:53am
   
It makes sense, I just don't know how to add numbers to the array and then randomly select from it...
OriginalGriff 30-Jun-21 3:45am
   
Well, create a jagged array, and set it to constant values:
            int[][] data = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6, 7 } };
            foreach (int[] page in data)
                {
                foreach (int x in page)
                    {
                    Console.Write($"{x}, ");
                    }
                Console.WriteLine();
                }
Member 15170612 30-Jun-21 4:54am
   
Thank you very much for your advice and patience!
OriginalGriff 30-Jun-21 5:24am
   
You're welcome - we are always willing to take time with people who want to learn.

With lazy students who want their homework done for them, it's another story altogether! :laugh:
Member 15170612 30-Jun-21 3:39am
   
It can be done as:
int[] numb1 = { 1, 2, 3};
int[] numb2 = { 1, 2, 3 };
if(secondNumb==1)
{
int random1= number.Next(0, numb1.Length);
firstNumb = random1;
}
total = firstNumb / secondNumb;
Quote:
How to write a while-loop condition?

Basically, exactly as you did, but anything depend on the values of variables.
Chage your code to :
C#
int secondNumb = number.Next(1, maxValue);
int firstNumb = number.Next(1, maxValueDivision);
// here print secondNumb
// here print firstNumb
// here print firstNumb % secondNumb
// here print firstNumb / secondNumb
 while (firstNumb % secondNumb != 0 && (firstNumb / secondNumb < 0 || firstNumb / secondNumb > 10))

And see if values are within range

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

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