nikhil
nikhil

Reputation: 1

Copying element from larger array to smaller sized array in C#

I have an n x n array of size 420 and I want to copy the content of this array into another array of size 20 x 20

I have already tried the following code where smallarray is of 20x20 size and largearray is of 420x420 size.

I am programming in C#, so it is giving me exception as "index out of bound"

for(int i=0;i<20;i++)
   {
     for(int j=0;j<20;i++)
     smallarray[i][j]=largearray[i][j]
   }

Upvotes: 1

Views: 474

Answers (2)

user3598756
user3598756

Reputation: 29421

try following this syntax

        int[,] largearray = new int[420, 420];
        int[,] smallarray = new int[20, 20];

        // your code for arrays initialization
        // ...

        for (int i = 0; i < 20; i++)
            for (int k = 0; k < 20; k++)
                smallarray[i, k] = largearray[i, k];

Upvotes: 0

Kote
Kote

Reputation: 2256

Looks like, there is a problem in third line.
Probably it's just a typo. for(int j=0;j<20;i++)
You increments i in both loops. Replace i++ with j++ in second loop and it will work fine.

Upvotes: 2

Related Questions