1

I'm beginner in C#. i was tasked to fill array with randomly generated numbers and check if there similar ones and change them with new random number. But this new generated random number should be checked against existing array. i need help with that last task.

here is my code :

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            Random r = new Random();
            int[] x = new int[20];
              for (int i=0; i < 20; i++)
            {
                x[i] = r.Next(100);
               Console.Write(x[i]+" "); //printing original array
            }
              Console.WriteLine();

              Console.WriteLine("New array is : ");
              for (int k = 0; k < 20;k++ )   
              {
                  for (int n = k + 1; n < 20;n++ )
                  {
                      if (x[n]==x[k])            //checking if there duplicated ones
                      {
                          Console.Write("New number is: ");
                          x[k]= r.Next(100);                          
                      }
                  }
                      Console.WriteLine(x[k]);

              }
                  Console.ReadKey();


        }
    }
}

thanks ahead

1
  • The array has a Contains method- you could look into that Commented May 4, 2014 at 6:39

2 Answers 2

2

There is no need for those kind of long calculations, you can simply make a do while loop in the beginning to create random numbers until there the array does not contain a number like the one created, like this:

Random r = new Random();
int[] x = new int[20];
for (int i = 0; i < x.Length; i++)
{
     do
     {
         x[i] = r.Next(100);

     } while (x.Contains(x[i])); // loops until the array does not contain an number like the one that was just created

     Console.Write(x[i] + " ");
}

Or if you want to continue doing it your way, instead of writing nested for loops you can make one loop which has only one condition in it with array.Contains(element) method. This method returns true if there is an element which is the same as the one given in the parenthesis, and false if there isn't.

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

1 Comment

When using languages like C#, it is better to use methods already provided.
1

@Lynx answer works perfectly but I would like to give an alternative which doesnt use LINQ: x.Contains(...)

    static void Main(string[] args)
    {
        Random r = new Random();
        int[] x = new int[20];
        for (int i = 0; i < 20; i++)
        {
            x[i] = r.Next(100);
            Console.Write(x[i] + " ");
        }

        Console.WriteLine("New array is : ");
        for (int k = 0; k < 20; k++)
        {
            for (int n = k + 1; n < 20; n++)
            {
                if (x[n] == x[k])
                {
                    int newNumber;

                    //Keeps generating a number which doesnt exists 
                    //in the array already
                    do
                    {
                        newNumber = r.Next();
                    } while (contains(x, newNumber));
                    //Done generating a new number.



                    Console.Write("New number is: ");
                    x[k] = newNumber;
                }
            }
            Console.WriteLine(x[k]);
        }
    }

    /// <summary>
    /// Returns true if the given array contains the given number
    /// </summary>
    /// <param name="array">The array to check</param>
    /// <param name="number">The number to check</param>
    /// <returns>bool True if number exists in array</returns>
    static bool contains(int[] array, int number)
    {
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] == number)
            {
                return true; //Returns true if number already exists
            }
        }
        return false;
    }

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.