# If else statement error?

• ### Question

• I'm having a problem with this code because when I enter the integers in 1, 2, 3, 4 and the output for displaying it high 2 low would come out  as 4, 3, 2, 2.  Same for Low 2 high, it'll be 2, 2, 3, 4.  Can somebody help me on this?  Thanks a bunch..

class DataFun
{
int Int1;
int Int2;
int Int3;
int Int4;
int High;
int Low;
int Med;
int Bot;
int Hild;

{
Console.WriteLine("Enter 1st integer:");
High = Int1;
Console.WriteLine("Enter 2nd integer:");
if (Int2 > High)
{
Hild = High;
High = Int2;
Med = Hild;
}
else
Med = Int2;
Console.WriteLine("Enter 3rd integer:");

if (Int3 > High)
{

Hild = High;
High = Int3;
Low = Med;
Med = Hild;

}
else
if (Int3 > Med)
{
Hild = Med;
Med = Int3;
Low = Hild;
}
else
Low = Int3;
Console.WriteLine("Enter 4th integer:");
if (Int4 > High)
{
Hild = High;
High = Int4;
Low = Med;
Bot = Low;
Med = Hild;

}
else
if (Int4 > Med)
{
Hild = Med;
Med = Int4;
Bot = Low;
Low = Hild;
}
else
if (Int4 > Low)
{
Hild = Low;
Low = Int4;
Bot = Hild;
}
else
Bot = Int4;
}

public void DisplayInOrderEntered()
{
Console.WriteLine("Order Entered:  " + Int1 + ", " + Int2 + ", " + Int3 + ", " + Int4 + "\n");
}

public void DisplayLow2High()
{
Console.WriteLine("Low 2 High:  " + Bot + ", " + Low + ", " + Med + ", " + High + "\n");
}
public void DisplayHigh2Low()
{
Console.WriteLine("High 2 Low:  " + High + ", " + Med + ", " + Low + ", " + Bot + "\n");
}

public void DisplayAsEntered()
{
Console.WriteLine("As Entered:  " + Int1 + ", " + Int2 + ", " + Int3 + ", " + Int4 + "\n");
}

public void DisplayReverse()
{
Console.WriteLine("As Reversed:  " + Int4 + ", " + Int3 + ", " + Int2 + ", " + Int1 + "\n");
}
}
}

Tuesday, December 18, 2012 5:55 PM

• I would rewrite the code, for readability purposes. And use a sort algorithm like bubble sort:

```    class DataFun
{
int Int1;
int Int2;
int Int3;
int Int4;

int High;
int Low;
int Med;
int Bot;
{
Console.WriteLine("Enter 1st integer:");
Console.WriteLine("Enter 2nd integer:");
Console.WriteLine("Enter 3rd integer:");
Console.WriteLine("Enter 4th integer:");
High = Int1;
Low = Int2;
Med = Int3;
Bot = Int4;
}
public void SortNumbers()
{
int dummy;
bool swappedValues = true;
while (swappedValues)
{
swappedValues = false;
if (Bot > Low)
{
dummy = Low;
Low = Bot;
Bot = dummy;
swappedValues = true;
}
if (Low > Med)
{
dummy = Med;
Med = Low;
Low = dummy;
swappedValues = true;
}
if (Med > High)
{
dummy = High;
High = Med;
Med = dummy;
swappedValues = true;
}
}
}```

• Proposed as answer by Tuesday, December 18, 2012 7:23 PM
• Marked as answer by Wednesday, December 26, 2012 9:28 AM
Tuesday, December 18, 2012 6:19 PM

### All replies

• I would rewrite the code, for readability purposes. And use a sort algorithm like bubble sort:

```    class DataFun
{
int Int1;
int Int2;
int Int3;
int Int4;

int High;
int Low;
int Med;
int Bot;
{
Console.WriteLine("Enter 1st integer:");
Console.WriteLine("Enter 2nd integer:");
Console.WriteLine("Enter 3rd integer:");
Console.WriteLine("Enter 4th integer:");
High = Int1;
Low = Int2;
Med = Int3;
Bot = Int4;
}
public void SortNumbers()
{
int dummy;
bool swappedValues = true;
while (swappedValues)
{
swappedValues = false;
if (Bot > Low)
{
dummy = Low;
Low = Bot;
Bot = dummy;
swappedValues = true;
}
if (Low > Med)
{
dummy = Med;
Med = Low;
Low = dummy;
swappedValues = true;
}
if (Med > High)
{
dummy = High;
High = Med;
Med = dummy;
swappedValues = true;
}
}
}```

• Proposed as answer by Tuesday, December 18, 2012 7:23 PM
• Marked as answer by Wednesday, December 26, 2012 9:28 AM
Tuesday, December 18, 2012 6:19 PM
• Unless this is for a homework assignment, there really isn't any reason to implement bubble sorting.  Array and list sorting is a solved problem.  Just use the built-in sort methods.

Tuesday, December 18, 2012 6:46 PM
• Every developer should be familiar with the sort algorithms and have implemented some of them..

Tuesday, December 18, 2012 7:28 PM
• Just a note; instead of

```int Int1;
int Int2;
int Int3;
int Int4;

int High;
int Low;
int Med;
int Bot;```

you can use a shorter syntax:

```int Int1, Int2, Int3, Int4,
High, Low, Med, Bot;```

Also, I don't see any point on making a class such as the one you did. One more thing: fields should not, by convention, start with an uppercase letter.

"Penso, logo existo" - René Descartes
"A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
"Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

João Miguel

Tuesday, December 18, 2012 7:31 PM
• this is an HW assignment.

The DataFun class will have 5 methods LoadNumbers(), DisplayHighToLow() and DisplayLowToHigh().

DisplayHighToLow() will display the 4 numbers descending from high to low.

DisplayLowToHigh() will display the 4 numbers ascending from low to high.

DisplayAsEntered() will display the 4 numbers in the same order as entered.

DisplayReversed() will display the 4 numbers in the reverse order as entered.

Here is what you console should look like when complete:

My Name:            Joe Student

Course Name:    Introduction to Object Oriented Programming

Instructor:           John Doe

Assignment:       Project 3

Today’s Date:     11/11/11

Enter integer 1: 55

Enter integer 2: 2

Enter integer 3: 400

Enter integer 4: 16

The integers from low to high:  2, 16, 55, and 400.

The integers from high to low: 400, 55, 16, and 2.

The integers as entered: 55, 2, 400, and 16.

The integers in reverse order: 16, 400, 2, and 55.

The integers come out in correct order with any other integers but when I type in 1, 2, 3, 4, this is when the order comes out from low to high 2, 2, 3, 4 and from high to low 4, 3, 2, 2.  I don't know why it does this.  It is like 1 is obsolete or something.  This is with the coding that I have in the top post.

Tuesday, December 18, 2012 7:43 PM
• I don't mind helping with homework because you have at least made an attempt.

Your problem seems to be in the if((Int4 > High) section.  You need to swap the lines

Low = Med and Bot = Low

However ... imagine if you had been asked to do this for 100 numbers.  You would be writing code until doomsday (which happens on Friday :-).  Maybe it will come in a leter lesson but, really, sorting is a solved problem and the way you are doing it is not the answer.

Paul Linton

Tuesday, December 18, 2012 9:11 PM
• Sure.  You can be familiar with them, but don't reinvent the wheel, either.
Tuesday, December 18, 2012 9:47 PM