locked
random number array RRS feed

  • Question

  • Hello All,

    I am trying to generate random numbers i an array and be able to track the first occurrence of a certain number but so far my code just cannot seem to get to be able to access the generated arrays to determine the index of the first occurrence of the number 7 in this case. Any help will be appreciated;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

     

    namespace Project2

    {

        public class SearchArray

        {

            public static void Main()

            {

                SearchArray searchArray = new SearchArray();

                int numberOfElements = 20;

                int[] randomNumsArray = searchArray.OccurenceResults(numberOfElements);

                Console.WriteLine("Number\tFrequency");

                for (int i = 0; i <= 9; i++)

                    Console.WriteLine("{0}\t{1}", i, randomNumsArray[i]);

                Console.WriteLine("Number of the first occurence of number 7, if any, is at index {0}", Array.IndexOf(randomNumsArray, '7', 0));            

            }

     

            private Random random = new Random();

            public int GenerateRandomNums()

            {

                int randumNum = random.Next(0, 10);

                return randumNum;

            }

     

            public int[] OccurenceResults(int numberOfElements)

            {

                int[] result = new int[20];

                for (int i = 0; i < numberOfElements; i++)

                    result[GenerateRandomNums()]++;

                return result;

            }

        }

    }

    Monday, October 3, 2011 3:40 AM

Answers

  • Your problem lies here:

    Array.IndexOf(randomNumsArray, '7', 0)
    

    You put the number with ' before and after.

    That causes the code to search for a char.

    But in your array there are no chars, only ints.

    Just delete these ' and the code will run.

     

    Noam B.

     



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Tuesday, October 4, 2011 11:55 AM
    • Marked as answer by Martin_Xie Wednesday, October 19, 2011 10:06 AM
    Monday, October 3, 2011 2:09 PM
  • BTW - you have a logical problem in your code.

    You use the random number as the index of the element in the array to set.

    But, since you create a random number between 0 - 9, you'll never set the elements in the array from position (index) of 10!

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Edited by Noam B Monday, October 3, 2011 3:08 PM
    • Proposed as answer by Noam B Tuesday, October 4, 2011 11:55 AM
    • Marked as answer by Martin_Xie Wednesday, October 19, 2011 10:06 AM
    Monday, October 3, 2011 3:08 PM

All replies

  • Hi,

    you don't need to create the instance of "SearchArray" in this case and you can call the public method the class directly. and call indexof method as

    Array.IndexOf(randomNumsArray, 7);
    


    Regards,

    Hassan


    Hope is one of the best things, so I hope that your answer lies here.
    • Proposed as answer by Kishorkna Monday, October 3, 2011 6:29 AM
    Monday, October 3, 2011 4:40 AM
  • It's quite possible that 7 never comes up in the random algorithm.
    Monday, October 3, 2011 5:11 AM
  • Hi,

    you don't need to create the instance of "SearchArray" in this case and you can call the public method the class directly. and call indexof method as

     

    Array.IndexOf(randomNumsArray, 7);
    

     


    Regards,

    Hassan


    Hope is one of the best things, so I hope that your answer lies here.

    When I remove the SearchArray instance and modify the ( int[] randomNumsArray = OccurenceResults(numberOfElements); ), I get the following error;

     

    An object reference is required for the non-static field, method, or property 'Project2.SearchArray.OccurenceResults(int)'

    Or am I misreading your suggestion?

     


    • Edited by techiegz Monday, October 3, 2011 10:34 AM
    Monday, October 3, 2011 10:29 AM
  • It's quite possible that 7 never comes up in the random algorithm.
    Thanx for the responses guys. Yes, it is quite possible that 7 does not show up in the randomly generated numbers, however, whether it show up or not I get the same -1 result. It seems I am unable to access the array elements/reference.
    • Edited by techiegz Monday, October 3, 2011 10:36 AM
    Monday, October 3, 2011 10:34 AM
  • To call a method using the class name, mark the method as static.
    Monday, October 3, 2011 11:32 AM
  • Can you post the SearchArray class?

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Monday, October 3, 2011 12:32 PM
  • Can you post the SearchArray class?

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    I have posted all the code there is at the beginning of this thread.
    Monday, October 3, 2011 1:51 PM
  • Your problem lies here:

    Array.IndexOf(randomNumsArray, '7', 0)
    

    You put the number with ' before and after.

    That causes the code to search for a char.

    But in your array there are no chars, only ints.

    Just delete these ' and the code will run.

     

    Noam B.

     



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Tuesday, October 4, 2011 11:55 AM
    • Marked as answer by Martin_Xie Wednesday, October 19, 2011 10:06 AM
    Monday, October 3, 2011 2:09 PM
  • Your problem lies here:

     

    Array.IndexOf(randomNumsArray, '7', 0)
    

     

    You put the number with ' before and after.

    That causes the code to search for a char.

    But in your array there are no chars, only ints.

    Just delete these ' and the code will run.

     

    Noam B.

     



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Yes, it runs but the issue is that regardless if 7 shows up in the random numbers, the result is -1, as though either 7 never occured or it cannot access the array elements.
    Monday, October 3, 2011 2:56 PM
  • Well, it works fine on my machine.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Monday, October 3, 2011 3:03 PM
  • BTW - you have a logical problem in your code.

    You use the random number as the index of the element in the array to set.

    But, since you create a random number between 0 - 9, you'll never set the elements in the array from position (index) of 10!

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Edited by Noam B Monday, October 3, 2011 3:08 PM
    • Proposed as answer by Noam B Tuesday, October 4, 2011 11:55 AM
    • Marked as answer by Martin_Xie Wednesday, October 19, 2011 10:06 AM
    Monday, October 3, 2011 3:08 PM