Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I want to fill jagged array using for loop in C#.In this jagged array we would have 768 rows with 9 elements in each row. I want to fill each row with random number.for example my first row would be

row 1 12 18 19 158 147 145 147 155 15
row2 14 17 18 15 19 16 114 15 4

thank you.
Posted
Comments
Shanu2rick 12-Jan-13 1:40am    
what have you tried?
Show the code, if you, please?

And the issue is? Tried?

You need two for loops and then just get random numbers to add to each array element. Try out.
 
Share this answer
 
First, if you know the number of coulmns and rows and they are fixed it is not jagged, it is just 2 dimentional array.


second, this is actually easy, just see the sample code here (random range: 0, 1000)

C#
public static void Main(string[] atgs)
       {
           //row 1 12 18 19 158 147 145 147 155 15
           //row2 14 17 18 15 19 16 114 15 4

           const int rows = 768;
           const int columns = 9;
           int[,] ai = new int[rows, columns];

           Random r = new Random();

           for (int i = 0; i < rows; ++i)
           {
               for (int j = 0; j < columns; ++j)
               {
                   ai[i, j] = r.Next(0, 1000);
               }
           }


       }
 
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