L
L
lucky_e32015-07-26 10:42:38
Arrays
lucky_e3, 2015-07-26 10:42:38

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:
82af9d4345b04cdf84241cb9e18b7aed.png
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

3 answer(s)
H
HASANN, 2015-07-26
@HASANN

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;

O
Oxoron, 2015-07-26
@Oxoron

// Вместо переменных можно использовать параметры метода
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];
    }
}

In the forehead - something like this (wrote without a compiler, there may be errors). To speed up it is necessary to work with columns and rows - to transfer their processing to separate threads.

M
MrDywar Pichugin, 2015-07-26
@Dywar

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 question

Ask a Question

731 491 924 answers to any question