8

The program first asks the user for the number of elements to be stored in the array, then asks for the numbers.

This is my code

    static void Main(string[] args)
    {
        Console.Write("How many numbers do you want to store in array: ");
        int n = Convert.ToInt32(Console.ReadLine());
        int[] numbers = new int[n];
        int min = numbers[0];
        int max = numbers[0];

        for (int i = 0; i < n; i++)
        {
            Console.Write("Enter number {0}:  ", i+1);
            numbers[i] = Convert.ToInt32(Console.ReadLine());
        }

        for (int i = 0; i < n; i++)
        {
            if (min > numbers[i]) min = numbers[i];
            if (max < numbers[i]) max = numbers[i];
        }
        Console.WriteLine("The minimum is: {0}", min);
        Console.WriteLine("The maximum is: {0}", max);
        Console.ReadKey();
    }
  }
}

But minimum value is always 0, why is that?

1
  • 2
    min by default is 0 - and 0 will never be greater than any number the user enters unless they enter a negative number. Commented Nov 16, 2013 at 15:15

5 Answers 5

21

Besides on your problem, you can use Enumerable.Min and Enumerable.Max methods like;

int[] numbers = new int[]{1, 2, 3 ,4};
Console.WriteLine(numbers.Min()); //1
Console.WriteLine(numbers.Max()); //4

Don't forget to add System.Linq namespace.

Sign up to request clarification or add additional context in comments.

6 Comments

@Rawling When I look at OP's code, I don't think efficiently is a subject here. And I don't see any reason to use these methods instead of iterating and comparing numbers which easyly can be mistaken if the OP was a starter about it.
"less efficiently" was an afterthought, it's the mindset of "you don't need to understand how to calculate this, or to be able to find mistakes in your code, just call LINQ!"
@Rawling No, I never say that you don't need to understand how to calculate stuff because we both know it is most important than thing for OP obviously.
@Rawling If he was a first year CS major, then yes, it is important to understand these things at a lower level. But it is also equally important to understand the principle of abstraction and how to writing reliable, readable code. The patterns of high level abstractions like Linq suggest good conventions for code decoupling. It is easy and intuitive to write a Max/Min function. The problem is having to much going on in one place.
May you elaborate on how this works? I am not familiar with the Enumerable Class. It is very nice to have this functionality.
|
7

Your issue is that you're initializing min and max to numbers[0] before numbers is populated, so they're both getting set to zero. This is wrong both for min (in case all your numbers are positive) and for max (in case all your numbers are negative).

If you move the block

int min = numbers[0];
int max = numbers[0];

to after the block

for (int i = 0; i < n; i++)
{
    Console.Write("Enter number {0}:  ", i+1);
    numbers[i] = Convert.ToInt32(Console.ReadLine());
}

then min and max will both be initialized to the first input number, which is fine. In fact you can then restrict your for loop to just check the subsequent numbers:

for (int i = 1; i < n; i++)
    ....

Just make sure the user's value of n is greater than zero!

2 Comments

+1 for the right way to explain OP what is exact problem is.
@Rawling Please don't :P
2

You are initializing min to 0.

Try following

int min = Int32.MaxValue;

Also In case you are accepting negative values as input, you should initialize max to minimum integer value.

int max = Int32.MinValue;

Comments

2

This code will help you in very simple way without adding any loops and conditions

int[] arr={1,1,2,4,5,1,2,1};
Array.Sort(arr);
Console.WriteLine("Min no: "+arr[0]);
Console.WriteLine("Max no: "+arr[arr.Length-1]);

Comments

0
        for (int i = 0; i < vector.GetLength(0); i++)
        {
            if (i == 0)
            {
                VectorMinMax[1]= vector[i];
                VectorMinMax[0]= vector[i];
            }
            else {
                if (VectorMinMax[1] > vector[i]) VectorMinMax[1] = vector[i];
                if (VectorMinMax[0] < vector[i]) VectorMinMax[0] = vector[i];
            }

        }

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.