7 Replies - 1763 Views - Last Post: 03 February 2013 - 07:53 AM Rate Topic: -----

#1 harold12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-February 13

find minimum value in random number array?

Posted 03 February 2013 - 07:09 AM

Hey

This is my code for my array,the array works and fills it with 10 random numbers but i am having great difficulty trying to find the minimum value in the array.

My array code is
Random random = new Random();

            int[] numbers = new int[10];

            for (int i = 1; i < numbers.Length; i++)
            {
                numbers[i] = random.Next(1, 19);

                Console.WriteLine(numbers[i]);
            }




I have tried various methods and none seen to work. I am struggling to understand why this particular method doesn't work.

int min = numbers.Min();


Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: find minimum value in random number array?

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:22 AM

I don't see why .Min() wouldn't work. What output are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 harold12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-February 13

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:29 AM

I am just getting 0

instead of using the function is there another way of hard coding i can use?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:32 AM

View Postharold12345, on 03 February 2013 - 08:09 AM, said:

I am struggling to understand why this particular method doesn't work.

int min = numbers.Min();


Any help would be greatly appreciated.



Let's see your complete code where you are actually using this call. Because that should work.
Was This Post Helpful? 0
  • +
  • -

#5 harold12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-February 13

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:39 AM

okay i have put the complete code here, its a visual studio 2010 project, in the console.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();

            int[] numbers = new int[10];

            for (int i = 1; i < numbers.Length; i++)
            {
                numbers[i] = random.Next(1, 19);

                Console.WriteLine(numbers[i]);
            }

            Console.ReadLine();

            // 8. Find the minimun of the array in task 7 and print it

            int min = numbers.Min();
            Console.WriteLine("the mimimum value is: " + min);
            Console.ReadLine();
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:47 AM

*
POPULAR

You are starting your loop from 1, whereas the array starts from 0, so the first item has a default value of 0 - and this is the minimum that you keep seeing.

Start your loop from 0

for (int i = 0; i < numbers.Length; i++)

This post has been edited by andrewsw: 03 February 2013 - 07:49 AM

Was This Post Helpful? 6
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:52 AM

Andrew for the win!! The crowd goes wild
:bananaman: :taz:
{Best I could do since the +1 button is broken at the moment}

This post has been edited by tlhIn`toq: 03 February 2013 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 harold12345   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-February 13

Re: find minimum value in random number array?

Posted 03 February 2013 - 07:53 AM

ahhhhhh, thanks a lot for your help work perfectly now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1