Resolved Find the smallest integer that is not in a random array

firstcnuk

New member
Joined
Mar 26, 2022
Messages
3
Programming Experience
Beginner
I am a beginner of C#. I have a question below that needs help. The question is: to find the smallest integer that is not in a random array. For example, the smallest integer would be 2 for an array as [1,3,5,9].

For this question, the steps are:

1) Create a random list of integer numbers falling between 0 and 10000. The user can specify how many numbers to be generated in this range - let's put this in a variable N. So if N = 4, then 4 numbers to be generated that fall between 0 and 10000.
2) introduce the list of random numbers into an array.
3) Based on the array, to find the smallest integer that is not in the array.

This question may be quite easy for C# experts. Sorry I just started. Thanks for your help.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,712
Location
Chesapeake, VA
Programming Experience
10+
Your question sounds more like you want us to do your homework for you. If that is not your intent, what specific question do you have which is keeping you from writing the code for your assignment? Very few teachers will spring and assign like this on their students without first laying the fundamentals that you would need to be successful.

First step is to come up with a plan. Do you have a plan?

Next step is to describe the steps to execute that plan. Imagine that you have to tell a 5 year old over the phone to execute your plan. How would you write/describe those steps so that he or she can understand and do the steps for you?

The end result of the above steps will be the outline for your code.
 

firstcnuk

New member
Joined
Mar 26, 2022
Messages
3
Programming Experience
Beginner
I've made the code as below as a starting point. But it fails to show anything? Not sure why?
C#:
using System;
 
public class Program
{
    public static void Main(string[] args)
    {
        int N = 10;
        int[] array = new int[N];
        Random rnd = new Random();
        for (int i = 0; i < N; i++)
        {
            array[i] = rnd.Next(10000);
        }
        for (int j = 0; j < N; j++)
        {
            Console.Write(array[j])
        }
    }
}
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,712
Location
Chesapeake, VA
Programming Experience
10+
Assuming you picked a Console project in Visual Studio, (and that you have an older version of Visual Studio) that's simply because the Console window closes so quickly after the program ends.

Your options are:
- press Ctrl-F5 to run without the debugger; or
- go into the debugger Options to tell VS to keep the console open after ringing (assuming you have VS2022); or
- add a line of code to wait for more user input before the end of your Main() method (ex. Console.ReadLine(); )
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,712
Location
Chesapeake, VA
Programming Experience
10+
I thought that the semicolon was just removed by the forum software when you originally posted without code tags. Bad guess on my part.

It would have helped if you told us you were getting a compilation error.
 
Top Bottom