6

I'm trying to find the maximum and minimum numbers out of 2 integers that a user has inputted. Firstly i have converted the string to int, then went to put them into an array so i can manipulate them. I think i'm getting stuck when it comes to assigning variables to an array. But i couldn't see any examples of arrays with variables assigned to them, which is probably where i'm going wrong.

    private void button1_Click(object sender, EventArgs e)
    {
       string txtbxnum1 = Int32.Parse(num1);
       string txtbxnum2 = Int32.Parse(num2);

       int[] numbers = new int[2] {0,1};
       int numbers [0] = num1;
       int numbers [1] = num2;

       int maximumNumber = Max.numbers();
       int minimumNumber = Min.numbers();
       MessageBox.Show (maximumNumber.Text);
    }

I would be glad of any help or direction.

2
  • 1
    Do the numbers have to go into an array? Otherwise it's one statement: maximumNumber = Math.Max(txtbxnum1, txtbxnum2); Commented Oct 24, 2012 at 21:35
  • 2
    This code is all wrong,it seems like you should read first few chapters of some introductory c# book. Commented Oct 24, 2012 at 21:36

6 Answers 6

7

If all you have is two numbers, you do not need an array: System.Math provides functions to find the smaller and the larger of two numbers, called Math.Max and Math.Min.

// Int32.Parse takes a string, and returns an int, not a string:
int n1 = Int32.Parse(num1);
int n2 = Int32.Parse(num2);
// Math.Min and Math.Max functions pick the min and max
int min = Math.Min(n1, n2);
int max = Math.Max(n1, n2);
// Show both numbers in a message box in one go using String.Format:
MessageBox.Show(string.Format("Min:{0} Max:{1}", min, max));
Sign up to request clarification or add additional context in comments.

Comments

2

A little bit messed up syntax. Your code is not C# language valid code.

You have to do something like this:

var numbers = new int[]{0,1,567,4,-5,0,67....};

and max/min is simply like

var maximum = numbers.Max();
var minimum = numbers.Min();

Comments

2

You should be calling Math.Min and Math.Max both of which accept two integers as arguments.

Let me know if that is not sufficient detail.

Comments

1

I don't quite understand your interaction with the TextBoxes and strange parsing and then setting to a string, but assuming num1 and num2 are integers that the user entered

private void button1_Click(object sender, EventArgs e)
{
    int maximumNumber = Math.Max(num1, num2);
    int minimumNumber = Math.Min(num1, num2);

    MessageBox.Show (maximumNumber);
}

1 Comment

private void button1_Click(object sender, EventArgs e) { int num1 = Convert.ToInt32(txtbxnum1.Text); int num2 = Convert.ToInt32(txtbxnum1.Text); int maximumNumber = Math.Max(num1, num2); int minimumNumber = Math.Min(num1, num2); MessageBox.Show(maximumNumber.ToString()); }
0
int maximumNumber = Math.Max(numbers[0],numbers[1]);
int minimumNumber = Math.Min(numbers[0],numbers[1]);

MessageBox.Show(maximumNumber + " " is the largest and " + minimumNumber + " is the smallest");

That said you shouldn't really access the array values like that, but it'll work for a beginner.

Comments

0

There are a few mistakes in your code.

string txtbxnum1 = Int32.Parse(num1);

Int32.Parse takes in a string and returns an int. However, you're trying to assign it to a string. It should be

int txtbxnum1 = Int32.Parse(num1);

Assigning an array like this:

int[] numbers = new int[2] {0,1};

simply creates a new array that can hold two integers and prefills them with the values 0 and 1. This isn't what you want to do. As far as I can tell, you don't even need to use arrays here unless you're using it somewhere else in your code.

You can find the Max and Min values by using methods in the Math class.

int minimumValue = Math.Min(txtbxnum1,txtbxnum2);
int maximumValue = Math.Max(txtbxnum1,txtbxnum2);

You can find out more about the Math class on MSDN.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.