1
\$\begingroup\$

I want to check in my if and else if statement if the inputs form a square or a rectangle, now my if and else if statements seems to be unreadable, now I'm thinking of ways to improve it.

  /* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/    
  static void Main(string[] args)
  {
     Console.Write("Input first: ");
     int first = int.Parse(Console.ReadLine());

     Console.Write("Input second: ");
     int second = int.Parse(Console.ReadLine());

     Console.Write("Input third: ");
     int third = int.Parse(Console.ReadLine());

     Console.Write("Input fourth: ");
     int fourth = int.Parse(Console.ReadLine());

     // all sides are equal
     // logic for if and else-if, needs codereview for improvement
     if (first == second && first == third && first == fourth && second == third && second == fourth && third == fourth)
     {
        Console.WriteLine("Square");
     }
     else if (first != second && first == third && first != fourth && second != third && second == fourth )
     {
        Console.WriteLine("Rectangle");
     }
     else
     {
        Console.WriteLine("Not a square nor a rectangle");
     }

     Console.ReadKey();
  }
\$\endgroup\$
8
  • 3
    \$\begingroup\$ Why have tags of linq and functional-programming? You aren't using either of them. As a side note you can simplify your square if statement no need to check the if 2nd = 3rd & 4th and 3rd = 4th since you checked the 1st equal to all sides the others ones are redundant \$\endgroup\$ – CharlesNRice Oct 22 '18 at 2:46
  • 4
    \$\begingroup\$ All 4 sides can have the same length and still not be a square: see rhombus. \$\endgroup\$ – chux - Reinstate Monica Oct 22 '18 at 8:07
  • 13
    \$\begingroup\$ Is first a length or an angle measure? If angles, the code lacks info to distinguish square from rectangle. \$\endgroup\$ – chux - Reinstate Monica Oct 22 '18 at 8:09
  • 2
    \$\begingroup\$ @chux: And if first is a length, the code lacks info to distinguish square from rhombus. \$\endgroup\$ – Eric Duminil Oct 22 '18 at 13:07
  • 4
    \$\begingroup\$ given the comment at the top of the code, it would appear that the intent was for angles to be input, in which case the figure is a rectangle or square if and only if all four inputs are 90 degrees (assuming degrees as the unit); however the comparisons are then done as if sides were input, which is inconsistent; inputting sides only cannot unambiguously determine rectangleness/squareness from a rhombus as already mentioned, you need at least one of the angles... \$\endgroup\$ – twalberg Oct 22 '18 at 18:54
11
\$\begingroup\$

Simplify the if by just doing

if (first == second && second == third && third == fourth)
{....}

You said the inputs are degrees, now it’s been awhile since geometry but squares and rectangles are determined by length of side, not angle degree. Both are 4 sided objects so every angle is the same (unless you’re talking about something like a trapezoid, rhombus).

\$\endgroup\$
4
  • 2
    \$\begingroup\$ For completeness, this optimizes the Square check. Not the Rectangle. \$\endgroup\$ – Mast Oct 22 '18 at 5:02
  • 7
    \$\begingroup\$ And they all need to be equal to 90. \$\endgroup\$ – Eric Duminil Oct 22 '18 at 8:19
  • 1
    \$\begingroup\$ This answer is bad, as is the question. You need angles as well as lengths of sides. At least three angles are needed at 90 degrees, for it to be a (maybe square) rectangle. If at least three sides are of equal length, it is a square rectangle. Otherwise it is a not-square rectangle. \$\endgroup\$ – nl-x Oct 23 '18 at 9:09
  • \$\begingroup\$ Rectangle and squares are specific types of 4 sided parallelograms each with two sets of parallel lines. In a rectangle one set has a longer length than the other set and for squares both sets have the same length. There are other types of 4 sided parallelograms where you do need the angles. For example an obtuse parallelogram has two parallel sides (let's say top and bottom) but one side is longer than the other and the other two sides are not parallel and the angles at the top corners will be > 90. For the classic rectangle and square that everyone pictures you do not need the angles. \$\endgroup\$ – Sean Oct 23 '18 at 12:45
9
\$\begingroup\$
  1. Remove comments that make lies. (Some goorus claim that it is a reason to never make any comments at all -after code changes they will eventually always start to lie)

         /* input are angles in degrees, 1st side is parallel to 3rd side, 2nd side is parallel to 4th side*/
    
  2. Square is just a special case of the rectangle. Using this property you can simplify your if statement by:

         if (first == third && second == fourth)
         {
            if(first == second)
            {
               Console.WriteLine("Square");
            }
            else
            {
               Console.WriteLine("Rectangle");
            }
         }
         else
         {
            Console.WriteLine("Not a square nor a rectangle");
         }
    
  3. Optionally you can generalize gathering input:

         int[] sideLengths = new int[4];
         for(int sideNumber = 0; sideNumber < 4; ++sideNumber)
         {
            Console.WriteLine($"Input {sideNumber+1}: ");
            sideLengths[sideNumber] = int.Parse(Console.ReadLine());
         }
    
  4. Optionally you can validate user input (if it is an integer, etc...). After applying 3rd step it would be easy

\$\endgroup\$
3
  • 6
    \$\begingroup\$ I think more input is needed. You cannot determine if a polygon is a square, a rectangle or something else from the lengths or the angles alone. \$\endgroup\$ – Eric Duminil Oct 22 '18 at 9:57
  • \$\begingroup\$ @EricDuminil You are quite right but I didn't intend to cover this topic by my answer. I wanted to focus on how to clean existing implementation. \$\endgroup\$ – mpasko256 Oct 22 '18 at 10:40
  • 4
    \$\begingroup\$ I just think it's not very important to write a clean implementation if the specs are so incomplete that it's not possible to answer the question. OP should provide more information first. \$\endgroup\$ – Eric Duminil Oct 22 '18 at 13:08
6
\$\begingroup\$

I agree with @mpasko256, try to be generic when you can, specific when you have to.

However, your code is misleading and need some improvements:

  • As others said, your comments are misleading: you play with lengths here (or you should) not degrees.
  • If you give your program to your beloved grandmother, she'll not understand what to do. Give some (basic) information to your user (Purpose, what to type, ...)
  • If we remove your comments (misleading, as i said), we have to mentally do some gymnastics to understand 'first', 'second', 'third' and 'fourth'. Why just not naming them 'top', 'right', 'bottom' and 'left', that's more expressive.
  • try to splits functionalities into reusable functions

Then, you can try to train your skills and enhancing your programe:

  • asking the number of sides
  • asking the degrees between sides
  • etc
\$\endgroup\$
0
\$\begingroup\$

This code is not correct. You cannot tell if the shape is a square, by only assessing the angles. All squares and rectangles have four angles of 90 degrees. And a square is a rectangle.

You will need At least two angles (both need to be 90 degrees) AND the length of three sides (all three need to be the same) to determine if the shape is a square. (And off course the confirmation that there are only four angles in total. I assume that you take this implicitly.)

For now, you can only determine if it is a rectangle. And you don't even need all four corners for it. If three out of four angles are 90 degrees, the fourth one will be 90 degrees as well.

if (first == 90 && second == 90 && third == 90)
    Console.WriteLine("Rectangle. Maybe even a square");
else
    Console.WriteLine("Not a square nor a rectangle");
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.