Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 11-Jan-13 19:26pm
Comments
Shanu2rick at 12-Jan-13 1:40am
   
what have you tried?
Show the code, if you, please?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

And the issue is? Tried?
 
You need two for loops and then just get random numbers to add to each array element. Try out.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
 
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);
               }
           }
 

       }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 358
1 Sergey Alexandrovich Kryukov 239
2 MT_ 175
3 Yogesh Kumar Tyagi 165
4 ChauhanAjay 156
0 Sergey Alexandrovich Kryukov 6,205
1 OriginalGriff 5,333
2 CPallini 2,473
3 Richard MacCutchan 1,617
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140814.1 | Last Updated 12 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100