Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6885715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:40:08+00:00 2026-05-27T05:40:08+00:00

I’ve got a 2D array of int’s in a console c# code (7X3 ‘cells’).

  • 0

I’ve got a 2D array of int’s in a console c# code (7X3 ‘cells’). The moment I create it, it is initialized with zeros.

I’ve got to change the value of only five ‘cells’ into 1, without changing the value of other ‘cells’. I know it is a basic thing, but, as an absolute rookie in C#, I can’t get it to work, no matter what cell I use. Can you help me?

Thanks in advance.

(Note: As I am a frequent user/contributer, I know it looks like a classic “Do my homework” question and I apologize, but as I’m really stuck on this weenie part (and the rest of my project is OK), I’d appreciate the help).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:40:09+00:00Added an answer on May 27, 2026 at 5:40 am

    Here are two ways of solving this issue, the first one; called BruteForceRandomImplementation is easy to implement and understand but quite slow once you attempt to mark a very large number of locations with 1s due to it’s brute-force like usage of Random till it fills enough locations.

        /// <summary>
        /// This method uses a random based algorithm to create a two-dimensional array of [height, width] 
        /// with exactly locationsToFind locations set to 1. 
        /// </summary>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="locationsToFind"></param>
        /// <returns></returns>
        public int[,] BruteForceRandomImplementation(int height, int width, int locationsToFind)
        {
            var random = new Random();
            locationsToFind = LimitLocationsToFindToMaxLocations(height, width, locationsToFind);
    
            // Create our two-dimensional array.
            var map = new int[height, width];
    
            int locationsFound = 0;
    
            // Randomly set positons to 1 untill we have set locationsToFind locations to 1. 
            while (locationsFound < locationsToFind)
            {
                // Get a random Y location - limit the max value to our height - 1. 
                var randomY = random.Next(height);
                // Get a random X location - limit the max value to our width - 1. 
                var randomX = random.Next(width);
    
                // Find another random location if this location is already set to 1. 
                if (map[randomY, randomX] == 1)
                    continue;
    
                // Otherwise set our location to 1 and increment the number of locations we've found.
                map[randomY, randomX] = 1;
                locationsFound += 1;
            }
    
            return map;
        }
    

    With the following helper method to keep our locationsToFind range sane:

        /// <summary>
        /// Limits the locationsToFind variable to the maximum available locations. This avoids attempting to 
        /// mark more locations than are available for our width and height. 
        /// </summary>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="locationsToFind"></param>
        /// <returns></returns>
        public int LimitLocationsToFindToMaxLocations(int height, int width, int locationsToFind)
        {
            return Math.Min(locationsToFind, height * width);
        }
    

    My second implementation called ShuffleImplementation is a lot faster when you are marking large numbers of unique locations. It creates a one-dimensional array, fills this with enough 1s to satisify your needs then shuffles this array using the Fisher-Yates shuffle to finally proceed to expand this one-dimensional array into a two-dimensional one:

        /// <summary>
        /// This method uses a shuffle based algorithm to create a two-dimensional array of [height, width] 
        /// with exactly locationsToFind locations set to 1. 
        /// </summary>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="locationsToFind"></param>
        /// <returns></returns>
        public int[,] ShuffleImplementation(int height, int width, int locationsToFind)
        {
            locationsToFind = LimitLocationsToFindToMaxLocations(height, width, locationsToFind);
            // Create a 1 dimensional array large enough to contain all our values. 
            var array = new int[height * width];
    
            // Set locationToFind locations to 1. 
            for (int location = 0; location < locationsToFind; location++)
                array[location] = 1;
    
            // Shuffle our array.
            Shuffle(array);
    
            // Now let's create our two-dimensional array.
            var map = new int[height, width];
    
            int index = 0;
    
            // Expand our one-dimensional array into a two-dimensional one. 
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    map[y, x] = array[index];
                    index++;
                }
            }
    
            return map;
        }
    
        /// <summary>
        /// Shuffles a one-dimensional array using the Fisher-Yates shuffle. 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arr"></param>
        public static void Shuffle<T>(T[] array)
        {
            var random = new Random();
    
            for (int i = array.Length - 1; i > 0; i--)
            {
                int n = random.Next(i + 1);
                Swap(ref array[i], ref array[n]);
            }
        }
    
        /// <summary>
        /// Swaps two values around. 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="valueA"></param>
        /// <param name="valueB"></param>
        public static void Swap<T>(ref T valueA, ref T valueB)
        {
            T tempValue = valueA;
            valueA = valueB;
            valueB = tempValue;
        }
    

    Usage:

    var map = BruteForceRandomImplementation(7, 3, 5);
    

    Or:

    var map = ShuffleImplementation(7, 3, 5);
    

    Depending on which one you want to use. For a good example of the performance difference between the two, try:

            int width = 1000;
            int height = 1000;
            int locationsToFind = (width * height) - 1;
    
            var stopwatch = new Stopwatch();
    
            stopwatch.Start();
            BruteForceRandomImplementation(height, width, locationsToFind);
            stopwatch.Stop();
    
            Console.WriteLine(string.Format("BruteForceRandomImplementation took {0}ms", stopwatch.ElapsedMilliseconds));
    
            stopwatch.Restart();
            ShuffleImplementation(height, width, locationsToFind);
            stopwatch.Stop();
    
            Console.WriteLine(string.Format("ShuffleImplementation took {0}ms", stopwatch.ElapsedMilliseconds));
    

    On my laptop BruteForceRandomImplementation took 1205ms and ShuffleImplementation took 67ms or nearly 18x faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
I'm trying to create an if statement in PHP that prevents a single post
I have this code to decode numeric html entities to the UTF8 equivalent character.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.