Share via


Max and Min program do while loop

Question

Thursday, November 14, 2013 5:07 PM

In my computing class we're doing loops, which I already know (being programming for about a year a now), but I can't for my life get this program to work. (We're doing this in Delphi but I can easily translate the code from c# to delphi)

I've done this before using arrays and it was fairly simple, but this program I've been given requires the user to enter any amount of numbers until -1 is entered and then it displays the max and min. I've got the max working, but as you would expect it will always display -1 as the min and I cannot at all find a fix for this. It's really annoying me as because my friend's sat next program was almost, if not all, identical to mine and his worked. (It's really embarrassing as well as in theory I should be able to do this) 

int nums;
            int max = 0;
            int min = 0;

            Console.WriteLine("Enter any number (Enter -1 to stop)");

            do
            {
                nums = Convert.ToInt32(Console.ReadLine());

                if (max > nums)
                    max = nums;
                if (min < nums)
                    min = nums;

            } while (nums != -1);

            Console.WriteLine(max + " " + min);

All replies (12)

Thursday, November 14, 2013 5:56 PM ✅Answered | 1 vote

Hi DJLad16;

Two issues, you had a > where you needed < and the other way around. Also you needed to make sure -1 was not entered before testing for min and max.

do
{
    nums = Convert.ToInt32(Console.ReadLine());
    if ( nums != -1 ) 
    {
        if (max < nums)
            max = nums;
        if (min > nums)
            min = nums;
    }
} while (nums != -1);

  

Fernando (MCSD)

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


Friday, November 15, 2013 7:55 AM ✅Answered

I think you cannot directly use the input for the while condition. Instead try soemthing like this:

bool Canceled;
int min = Int.MaxValue;
int max = Int.MinValue;

do{
  Canceled = false;
  string input = Console.ReadLine();

  if(input.Trim() != "-1"){
    //This is a number that should be considered.
    //Parse it and process it
    int number = Convert.ToInt32(input);
    if(min > number)
      min = number;
    if(max < number)
      max = number;
  }
  else{
    //Cancelation was requested
    //Just make certain the loop is exited
    Canceled = true;
  }
}while(!Canceled);

Console.WriteLine(max + " " + min);

I used part of Reed's answer for this.

Using a specialised bool varraible makes the code a lot easier to read and understand.
I also changed the check for cancelation to a String Check. That way you can easily change that to a Char or different String (afaik DOS uses Ctrl+C as marker for "end of user entry phase").

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Thursday, November 14, 2013 6:10 PM

Ah, didn't even realize that I was doing it the wrong way round. That seemed to fix the -1 problem but now it only outputs 0 on the min for some reason


Thursday, November 14, 2013 6:21 PM

The last statement is this

Console.WriteLine(max + " " + min);

it is outputting both the max and min values. The two original if statements are still being executed as long as the response is not -1 so they both should still be displaying a value. This does work on my system.

  

Fernando (MCSD)

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


Thursday, November 14, 2013 6:23 PM

Ah, didn't even realize that I was doing it the wrong way round. That seemed to fix the -1 problem but now it only outputs 0 on the min for some reason

You need to set the default value of "min" to a very large number, not 0.  I recommend starting with:

int nums;
int max = int.MinValue;
int min = int.MaxValue;

That way, your first number will be smaller than min, and get it set appropriately.

Reed Copsey, Jr. - http://reedcopsey.com - If a post answers your question, please click Mark As Answer on that post. If you find a post helpful, please click Vote as Helpful.


Thursday, November 14, 2013 6:53 PM

I actually thought about that but I didn't know how to do it deplhi so I didn't do it. Now I just realized I have something called the internet. Thanks anyway :D


Thursday, November 14, 2013 8:27 PM

If I am not mistaken it is the following in Delphi :

var max, min : Integer;
min := Low(Integer);
max := High(Integer);

   

Fernando (MCSD)

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


Friday, November 15, 2013 1:59 PM

You code would fail if the smalest number entered is say, positive 5. Or realy any positive number. Even 0 would be larger then -1. If you entered 65536, 100 and 11, the smalest number would be -1.

In the same way, if one entered -65536, -100 and -11, the biggest number would given as 0.

Both cases are obviously wrong. Initialising with min/max is just the cleanest way here.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Monday, November 18, 2013 9:14 AM

You are right, it should be like this:

int nums;
int max = -1;
int min = -1;

Console.WriteLine("Enter any number (Enter -1 to stop)");

do
{
    nums = Convert.ToInt32(Console.ReadLine());

    if (nums != -1)
    {
        if (max < nums || max == -1)
            max = nums;
        if (min > nums || min == -1)
            min = nums;
    }

} while (nums != -1);

Console.WriteLine(max + " " + min);

Monday, November 18, 2013 10:45 AM

You code still fails if the highest number is below -1. Or the lowest higher then -1.

Just stop trying. There is no way to "improve" upon the Min = MaxValue, Max = MinValue approach. All you get is code that will not reliably work.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Monday, November 18, 2013 11:19 AM

Ok thanks. I'VE JUST STOPPED TRYING!

But did you try it or you just think it won't work? 

Give me the values that it fails!!!!


Monday, November 18, 2013 1:52 PM

My bad. I overlooked that || min == -1, wich will always write the first valid entry if it is still on default.

Then it would indeed work.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.