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