Fill two-dimensional arrangement with the main diagonal with -1

-2

I have a 5 x 5 matrix which I fill with random numbers that are between 1 and 9 but I would like the main diagonal of the matrix to be filled only with (-1)

    
asked by Duvca 29.05.2018 в 04:11
source

1 answer

0

You can try this way:

 mat = new int[5,5];
 Random rnd = new Random();

 for(int f = 0; f < 5; f++) 
 {
     for(int c = 0; c < 5; c++) 
     {
         if(f == c){

                mat[f, c] = -1;

          }else{

                mat[f, c] = rnd.Next(1, 10);
         }
      }
   }
    
answered by 29.05.2018 в 05:30