Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
For this program i've asked the user to input the array size then its filled with numbers in chronological order until the array is full. I Want to then put this array into the grid. I was wondering if it's possible to start the input of the array at a certain point, say its was the numbers 1, 2, 3, 4, 5, 6, 7, 8, 9 10. Would i be able to start the input at a certain point like [0,2], so basically not use the first slots to make the grid like;

[] [] [1] [2] [3]
[4] [5] [6] [7] [8]
[9] [10] [] [] []


i was wondering if and how i would be able to do this Thank you in advance!

C#
class Program
{
      static void Main(string[] args)
      {

        int Height = 4;
        int Width = 5;

        int[,] grid = new int[Height, Width];

        Console.Write("Input Number: ");
        int number = int.Parse(Console.ReadLine());
        int[] InputNumber = new int[number];
        var randomNumbers = Enumerable.Range(1, number).ToArray();

        /*
        [0,0] [0,1] [0,2] [0,3] [0,4]
        [1,0] [1,1] [1,2] [1,3] [1,4] 
        [2,0] [2,1] [2,2] [2,3] [2,4] 
        [3,0] [3,1] [3,2] [3,3] [3,4]*/

    }
}


What I have tried:

i'm new to c# programming and i've tried just about everything, spending so many hours in the process trying to figure it out myself as well as researching,however i can't find anything.
Posted
Updated 1-Dec-16 7:54am
v2
Comments
PIEBALDconsult 29-Nov-16 20:14pm    
Yes. You can.
How would you do it with a deck of cards?
preety sunita 30-Nov-16 6:08am    
if don not want to use first some slots then initialize them with some unique number like -999 or 0. Because i think u cant left it as blank if its integer array.
gggustafson 1-Dec-16 0:06am    
This is not only not the answer but it is wrong in so many other ways!!!
preety sunita 5-Dec-16 7:19am    
okies..

1 solution

I made this example, hope it helps:

C#
static void Main(string[] args)
{

    int Height = 4;
    int Width = 5;

    Console.Write("Height: ");
    Height = int.Parse(Console.ReadLine());

    Console.Write("Width: ");
    Width = int.Parse(Console.ReadLine());

    int[,] grid = new int[Height, Width];

    //TEst
    PrintGrid(grid, Height, Width);
    //Console.ReadLine();

    //Console.Write("Input Number: ");
    Console.Write("Input Number of Start SLOT between 1 and " + (Height * Width) + ": ");
    int startSlot = int.Parse(Console.ReadLine());

    Console.Write("Input Number. How many SLOT to fill between 1 and " + ((Height * Width) -startSlot+1) + ": ");
    int numberSlot = int.Parse(Console.ReadLine());

    ///int[] InputNumber = new int[numberSlot];
    //var randomNumbers = Enumerable.Range(1, number).ToArray();
    //grid[0, 2] = randomNumbers[0];//Put direct in the position

    //Randoms numbers to insert
    int[] numbers = new int[numberSlot];
    Random randNum = new Random();
    for (int i = 0; i < numberSlot; i++)
    {
        numbers[i] = randNum.Next(1, (Height * Width));
    }

    //Insert from the start position
    int pos = 0;
    int numberIndex = 0;
    for (int i = 0; i < Height; i++)
    {
        for (int j = 0; j < Width; j++)
        {
            //ACTUAL POSITION
            pos++;
            if (pos == startSlot || pos > startSlot) {
                if (numberIndex < numberSlot) {
                    grid[i, j] = numbers[numberIndex];
                    numberIndex++;
                }
            }
        }
    }

    PrintGrid(grid, Height, Width);
    Console.ReadLine();
    /*
    [0,0] [0,1] [0,2] [0,3] [0,4]
    [1,0] [1,1] [1,2] [1,3] [1,4]
    [2,0] [2,1] [2,2] [2,3] [2,4]
    [3,0] [3,1] [3,2] [3,3] [3,4]*/

}

static void PrintGrid(int[,] grid,  int h,int w) {
    Console.Write("\nPrint Grid\n");
    for (int i = 0; i< h; i++) {
        for (int j = 0; j < w; j++) {
            Console.Write( (grid[i, j]).ToString() + " ");
        }
        Console.Write("\n");
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900