P
P
PurgaBot2020-11-09 20:39:58
C++ / C#
PurgaBot, 2020-11-09 20:39:58

Create a 2D array, populate it and randomly select a row and column and delete. To do this, a new array is created. C# I don't understand the error?

Write a program that creates and initializes a two-dimensional numeric array.
Then a row and a column are removed from this array (a new array is created in which one row and one column are removed compared to the original one). The index of the deleted row and the index of the managed row are determined using a random number generator. Here is my code:

int a, b; 
 int value = 1; 
 Console.Write("Введите кол-во строк "); 
 a = Int32.Parse(Console.ReadLine()); 
 Console.Write("Введите кол-во столбцов "); 
 b = Int32.Parse(Console.ReadLine()); 
 Random rnd = new Random(); 
 int[,] numbs = new int[a, b]; 
 int row = rnd.Next(numbs.GetLength8(0)+1); 
 int col = rnd.Next(numbs.GetLength(1)+1); 
 for (int i = 0;i<a;i++) 
 { 
 for (int j = 0; j < b; j++) 
 { 
 numbs[i, j] = value; 
 Console.Write(numbs[i, j] + " "); 
 value++; 
 } 
 Console.WriteLine(); 
 } 
 
 Console.WriteLine("Удаляются столбец {0} и строка {1} ", col, row); 
 
 int v, q; 
 int[,] tvt = new int[(a - 1), (b - 1)]; 
 for (int i = 0; i < numbs.GetLength(0); i++) 
 { 
 if (i < row) v = i; 
 else v = (i - 1); 
 
 for (int j = 0; j < numbs.GetLength(1); j++) 
 { 
 if (j < col) q = j; 
 else q = (j - 1); 
 
 } 
 } 
 
 numbs = tvt; 
 Console.WriteLine(); 
 for (int i = 0; i < numbs.GetLength(0); i++) 
 { 
 for (int j = 0; j< numbs.GetLength(1); j++) 
 { 
 Console.Write(numbs[i,j] + " "); 
 } 
 Console.WriteLine(); 
 }


Screenshot with an error:
https://habrastorage.org/webt/5f/a9/7e/5fa97e814c2...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-11-09
@freeExec

You have random for deleted elements, it generates numbers more than allowed. Those. you have only 2 columns, and you are going to delete the column with index 2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question