Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My professor wants us to mirror this image using loops, decision making statements... etc. The only thing I do not get is how I managed to duplicate the word 'Buzz' under 'BuzzLightYear' when it should not be there at all.

What he wants us to do:
https://i.stack.imgur.com/Vcp77.png[^]

My code:

int variable = 25;
while (variable <= 50)
{
if (variable % 2 == 0 && variable % 3 == 0 && variable % 7 == 0)
{
Console.Write("BuzzLightYear\n");
}
if (variable % 2 == 0)
{
Console.Write("Buzz\n");
}
if (variable % 3 == 0 && variable % 2 != 0 && variable % 7 != 0)
{
Console.Write("Light\n");
}
if (variable % 7 == 0 && variable % 2 != 0 && variable % 3 != 0)
{
Console.Write("Year\n");
}
if (variable % 2 != 0 && variable % 3 != 0 && variable % 7 != 0)
{
Console.WriteLine(variable);
}
++variable;
}
Console.Write("\nPress any key to continue to infinity and beyond...");
Console.ReadKey();

What I have tried:

If anyone would be kind enough to help, thanks!
Posted
Updated 14-Nov-20 0:21am
v2

The only thing I do not get is how I managed to duplicate the word 'Buzz' under 'BuzzLightYear' when it should not be there at all.
if (variable % 2 == 0 && variable % 3 == 0 && variable % 7 == 0)
{
    Console.Write("BuzzLightYear\n");
}
if (variable % 2 == 0)
{
    Console.Write("Buzz\n");
}
When the value is 42, it prints "BuzzLightYear; then, it executes the next test, and, since 42 % 2 == 0, it prints "Buzz." It's doing what you tell it to do !

I wonder if your "professor" is serious in giving you such a code-mess.

Think about how you can (easily) reduce the complexity by:

1. using the fact that every number between 25 and 49 is either odd or even.

I'm not going to write your code for you, but, here's an outline you can start to work with:
int variable = 25;
bool isMod2 = false; 
bool isMod3 = false;

while (variable <= 50)
{
    isMod2 = variable % 2 == 0;
    isMod3 = variable % 3 == 0;

    if (isMod2)
    {
        if (isMod3)
        {

        }
        else
        {
            
        }
    }
    else
    {
        // we know isMod3 is 'true here
        // we know the case of both mod 2 and 3 == true has been handled
    }
}
Think about how you can reduce the number of tests for the "rarest" case, when the variable mod 7 is 0.
 
Share this answer
 
All you need to do is make all the tests after the first one to be alternatives by adding else to the if statements, and replacing the last one with else. That way, only one set of conditions will be executed for each iteration of the loop.
C#
int variable = 25;
while (variable <= 50)
{
    if (variable % 2 == 0 && variable % 3 == 0 && variable % 7 == 0)
    {
        Console.Write("BuzzLightYear\n");
    }
    else if (variable % 2 == 0)
    {
        Console.Write("Buzz\n");
    }
    else if (variable % 3 == 0 && variable % 2 != 0 && variable % 7 != 0)
    {
        Console.Write("Light\n");
    }
    else if (variable % 7 == 0 && variable % 2 != 0 && variable % 3 != 0)
    {
        Console.Write("Year\n");
    }
    else
    {
        Console.WriteLine(variable);
    }
    ++variable;
}
Console.Write("\nPress any key to continue to infinity and beyond...");
Console.ReadKey();
 
Share this answer
 

First off, analyse the problem. What is going on here? Well the decision about what to print is being made on the relationships between 2,3,7 and a variable. What is the relationship? It is based on whether or not 2,3,7 are factors of the variable, more accurately, it is the pattern of the isFactor status for each of the numbers 2,3,7 with regard to the variable. There are combinations of 3 bool values that determine what is printed, these can be represented by a truth table e.g. true,true,true false,true false etc.

There are various ways of coding this. I would use a method that returns a value tuple(bool,bool,bool)

C#
public static (bool,bool,bool)AreTwoThreeSevenFactors(int target)
        {
            (bool is2, bool is3, bool is7) results;
          ...

Then I would plug the results into a switch statement that returns the answer as a string.
C#
private static string ResultsToString((bool, bool, bool) status,int target)
        {
            return status switch
            {
                (true, true, true) => "BuzzLightYear",
         ...

The whole thing can be coded without a single if statement. Have a go. Tip : use the discard value for the 'Buzz' option (true,_,_)

 
Share this answer
 
v2
Quote:
What he wants us to do:
https://i.stack.imgur.com/Vcp77.png

When you ask for help, it is a good idea to explain in what the result is wrong.
Giving a piece of code we can run is a good idea too.
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C#
int variable = 25;
while (variable <= 50)
{
    if (variable % 2 == 0 && variable % 3 == 0 && variable % 7 == 0)
    {
        Console.Write("BuzzLightYear\n");
    }
    if (variable % 2 == 0)
    {
        Console.Write("Buzz\n");
    }
    if (variable % 3 == 0 && variable % 2 != 0 && variable % 7 != 0)
    {
        Console.Write("Light\n");
    }
    if (variable % 7 == 0 && variable % 2 != 0 && variable % 3 != 0)
    {
        Console.Write("Year\n");
    }
    if (variable % 2 != 0 && variable % 3 != 0 && variable % 7 != 0)
    {
        Console.WriteLine(variable);
    }
    ++variable;
}
Console.Write("\nPress any key to continue to infinity and beyond...");
Console.ReadKey();

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
v2
Comments
BillWoodruff 14-Nov-20 4:09am    
Voted #1

"Giving a piece of code we can run is a good idea too."

The OP's code compiles and runs. All you've done is add indentation: you could easily have edited the OP's post, or left a comment.

You wrote: "a good idea to explain in what the result is wrong.":

The OP did say what was "wrong: "The only thing I do not get is how I managed to duplicate the word 'Buzz' under 'BuzzLightYear' when it should not be there at all."

Do you really need rep-points this bad ?
Patrice T 14-Nov-20 4:52am    
Hi Bill,
Thanks for commenting your downvote, at least you have courage to give an explanation, not many do.
The "(how do I avoid repeating a statement when I need a certain one)" was not clear to me.
A dump of actual result with a comment on wrong lines would have been easier to cope with (to me).
The word "mirror" was not clear to me either. May be a too long nite for me (5AM when I wrote my answer).

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