Answer the question
In order to leave comments, you need to log in
How to correctly move data from one array to another?
There are two arrays, one of which is smaller than the other, for example, an array of numbers 100x100 and an array of the same type 10x10.
When a certain event is triggered, numbers from a large array should be transferred to a small one, but with an offset, for example, by 5 "cells". Roughly speaking, a small array moves along a larger one.
For clarity:
Green arrows indicate the conditional direction of "movement" of a small array.
UPD. The small array must "move" both to the right and to the left, both up and down.
Answer the question
In order to leave comments, you need to log in
int[,] big = new int[100, 100];
int[,] small = new int[10, 10];
int m = n = 0;
// заполнение массива big
.....
.....
// пример: событие onClick
if (small.Length == 0)
{
// вызываем процедуру передачи
}
else
{
// очищаем массив
// вызываем процедуру передачи
}
// процедура передачи из одного массива в другой
for (int i = 0; i < small.Length; i++)
{
for (int j = 0; j < small.Length; j++)
{
small[i][j] = big[i+n][j+m]
}
}
n += 5;
m += 5;
// Вместо переменных можно использовать параметры метода
int leftBorderIndex = ...; // Координаты угла квадрата
int topBorderIndex = ...;
int leftShift = ...; // Сдвиг влево
int topShift = ...; // Сдвиг вправо
int[100,100] largeArr= ...;
int[10,10] smallArr = ...;
int cornerLeft = leftBorderIndex + leftShift; // Координаты лево-верхнего угла квадрата, откуда копируем данные
int cornerTop = topBorderIndex + topShift; // Вставить проверки на выход из границ большого массива
for(int row = cornerTop, i=0; row<cornerTop+10;row++, i++) // Проверить инициализацию и инкремент двойных команд
{
for(int col = cornerLeft, j=0; col<cornerLeft+10; col++,j++)
{
smallArr[i,j] = largeArr[row,col];
}
}
To begin with, write this in pseudocode with checking the boundary conditions of the statements.
Try to imagine it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question